aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: f3921c63d8e7c91cbb51e21186ecfc0e3ed3653c (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a github wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  252. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  253. A [punctuation character](@) is an [ASCII
  254. punctuation character] or anything in
  255. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  256. ## Tabs
  257. Tabs in lines are not expanded to [spaces]. However,
  258. in contexts where whitespace helps to define block structure,
  259. tabs behave as if they were replaced by spaces with a tab stop
  260. of 4 characters.
  261. Thus, for example, a tab can be used instead of four spaces
  262. in an indented code block. (Note, however, that internal
  263. tabs are passed through as literal tabs, not expanded to
  264. spaces.)
  265. ```````````````````````````````` example
  266. →foo→baz→→bim
  267. .
  268. <pre><code>foo→baz→→bim
  269. </code></pre>
  270. ````````````````````````````````
  271. ```````````````````````````````` example
  272. →foo→baz→→bim
  273. .
  274. <pre><code>foo→baz→→bim
  275. </code></pre>
  276. ````````````````````````````````
  277. ```````````````````````````````` example
  278. a→a
  279. ὐ→a
  280. .
  281. <pre><code>a→a
  282. ὐ→a
  283. </code></pre>
  284. ````````````````````````````````
  285. In the following example, a continuation paragraph of a list
  286. item is indented with a tab; this has exactly the same effect
  287. as indentation with four spaces would:
  288. ```````````````````````````````` example
  289. - foo
  290. →bar
  291. .
  292. <ul>
  293. <li>
  294. <p>foo</p>
  295. <p>bar</p>
  296. </li>
  297. </ul>
  298. ````````````````````````````````
  299. ```````````````````````````````` example
  300. - foo
  301. →→bar
  302. .
  303. <ul>
  304. <li>
  305. <p>foo</p>
  306. <pre><code> bar
  307. </code></pre>
  308. </li>
  309. </ul>
  310. ````````````````````````````````
  311. Normally the `>` that begins a block quote may be followed
  312. optionally by a space, which is not considered part of the
  313. content. In the following case `>` is followed by a tab,
  314. which is treated as if it were expanded into three spaces.
  315. Since one of these spaces is considered part of the
  316. delimiter, `foo` is considered to be indented six spaces
  317. inside the block quote context, so we get an indented
  318. code block starting with two spaces.
  319. ```````````````````````````````` example
  320. >→→foo
  321. .
  322. <blockquote>
  323. <pre><code> foo
  324. </code></pre>
  325. </blockquote>
  326. ````````````````````````````````
  327. ```````````````````````````````` example
  328. -→→foo
  329. .
  330. <ul>
  331. <li>
  332. <pre><code> foo
  333. </code></pre>
  334. </li>
  335. </ul>
  336. ````````````````````````````````
  337. ```````````````````````````````` example
  338. foo
  339. →bar
  340. .
  341. <pre><code>foo
  342. bar
  343. </code></pre>
  344. ````````````````````````````````
  345. ```````````````````````````````` example
  346. - foo
  347. - bar
  348. → - baz
  349. .
  350. <ul>
  351. <li>foo
  352. <ul>
  353. <li>bar
  354. <ul>
  355. <li>baz</li>
  356. </ul>
  357. </li>
  358. </ul>
  359. </li>
  360. </ul>
  361. ````````````````````````````````
  362. ```````````````````````````````` example
  363. #→Foo
  364. .
  365. <h1>Foo</h1>
  366. ````````````````````````````````
  367. ```````````````````````````````` example
  368. *→*→*→
  369. .
  370. <hr />
  371. ````````````````````````````````
  372. ## Insecure characters
  373. For security reasons, the Unicode character `U+0000` must be replaced
  374. with the REPLACEMENT CHARACTER (`U+FFFD`).
  375. # Blocks and inlines
  376. We can think of a document as a sequence of
  377. [blocks](@)---structural elements like paragraphs, block
  378. quotations, lists, headings, rules, and code blocks. Some blocks (like
  379. block quotes and list items) contain other blocks; others (like
  380. headings and paragraphs) contain [inline](@) content---text,
  381. links, emphasized text, images, code spans, and so on.
  382. ## Precedence
  383. Indicators of block structure always take precedence over indicators
  384. of inline structure. So, for example, the following is a list with
  385. two items, not a list with one item containing a code span:
  386. ```````````````````````````````` example
  387. - `one
  388. - two`
  389. .
  390. <ul>
  391. <li>`one</li>
  392. <li>two`</li>
  393. </ul>
  394. ````````````````````````````````
  395. This means that parsing can proceed in two steps: first, the block
  396. structure of the document can be discerned; second, text lines inside
  397. paragraphs, headings, and other block constructs can be parsed for inline
  398. structure. The second step requires information about link reference
  399. definitions that will be available only at the end of the first
  400. step. Note that the first step requires processing lines in sequence,
  401. but the second can be parallelized, since the inline parsing of
  402. one block element does not affect the inline parsing of any other.
  403. ## Container blocks and leaf blocks
  404. We can divide blocks into two types:
  405. [container blocks](@),
  406. which can contain other blocks, and [leaf blocks](@),
  407. which cannot.
  408. # Leaf blocks
  409. This section describes the different kinds of leaf block that make up a
  410. Markdown document.
  411. ## Thematic breaks
  412. A line consisting of 0-3 spaces of indentation, followed by a sequence
  413. of three or more matching `-`, `_`, or `*` characters, each followed
  414. optionally by any number of spaces or tabs, forms a
  415. [thematic break](@).
  416. ```````````````````````````````` example
  417. ***
  418. ---
  419. ___
  420. .
  421. <hr />
  422. <hr />
  423. <hr />
  424. ````````````````````````````````
  425. Wrong characters:
  426. ```````````````````````````````` example
  427. +++
  428. .
  429. <p>+++</p>
  430. ````````````````````````````````
  431. ```````````````````````````````` example
  432. ===
  433. .
  434. <p>===</p>
  435. ````````````````````````````````
  436. Not enough characters:
  437. ```````````````````````````````` example
  438. --
  439. **
  440. __
  441. .
  442. <p>--
  443. **
  444. __</p>
  445. ````````````````````````````````
  446. One to three spaces indent are allowed:
  447. ```````````````````````````````` example
  448. ***
  449. ***
  450. ***
  451. .
  452. <hr />
  453. <hr />
  454. <hr />
  455. ````````````````````````````````
  456. Four spaces is too many:
  457. ```````````````````````````````` example
  458. ***
  459. .
  460. <pre><code>***
  461. </code></pre>
  462. ````````````````````````````````
  463. ```````````````````````````````` example
  464. Foo
  465. ***
  466. .
  467. <p>Foo
  468. ***</p>
  469. ````````````````````````````````
  470. More than three characters may be used:
  471. ```````````````````````````````` example
  472. _____________________________________
  473. .
  474. <hr />
  475. ````````````````````````````````
  476. Spaces are allowed between the characters:
  477. ```````````````````````````````` example
  478. - - -
  479. .
  480. <hr />
  481. ````````````````````````````````
  482. ```````````````````````````````` example
  483. ** * ** * ** * **
  484. .
  485. <hr />
  486. ````````````````````````````````
  487. ```````````````````````````````` example
  488. - - - -
  489. .
  490. <hr />
  491. ````````````````````````````````
  492. Spaces are allowed at the end:
  493. ```````````````````````````````` example
  494. - - - -
  495. .
  496. <hr />
  497. ````````````````````````````````
  498. However, no other characters may occur in the line:
  499. ```````````````````````````````` example
  500. _ _ _ _ a
  501. a------
  502. ---a---
  503. .
  504. <p>_ _ _ _ a</p>
  505. <p>a------</p>
  506. <p>---a---</p>
  507. ````````````````````````````````
  508. It is required that all of the [non-whitespace characters] be the same.
  509. So, this is not a thematic break:
  510. ```````````````````````````````` example
  511. *-*
  512. .
  513. <p><em>-</em></p>
  514. ````````````````````````````````
  515. Thematic breaks do not need blank lines before or after:
  516. ```````````````````````````````` example
  517. - foo
  518. ***
  519. - bar
  520. .
  521. <ul>
  522. <li>foo</li>
  523. </ul>
  524. <hr />
  525. <ul>
  526. <li>bar</li>
  527. </ul>
  528. ````````````````````````````````
  529. Thematic breaks can interrupt a paragraph:
  530. ```````````````````````````````` example
  531. Foo
  532. ***
  533. bar
  534. .
  535. <p>Foo</p>
  536. <hr />
  537. <p>bar</p>
  538. ````````````````````````````````
  539. If a line of dashes that meets the above conditions for being a
  540. thematic break could also be interpreted as the underline of a [setext
  541. heading], the interpretation as a
  542. [setext heading] takes precedence. Thus, for example,
  543. this is a setext heading, not a paragraph followed by a thematic break:
  544. ```````````````````````````````` example
  545. Foo
  546. ---
  547. bar
  548. .
  549. <h2>Foo</h2>
  550. <p>bar</p>
  551. ````````````````````````````````
  552. When both a thematic break and a list item are possible
  553. interpretations of a line, the thematic break takes precedence:
  554. ```````````````````````````````` example
  555. * Foo
  556. * * *
  557. * Bar
  558. .
  559. <ul>
  560. <li>Foo</li>
  561. </ul>
  562. <hr />
  563. <ul>
  564. <li>Bar</li>
  565. </ul>
  566. ````````````````````````````````
  567. If you want a thematic break in a list item, use a different bullet:
  568. ```````````````````````````````` example
  569. - Foo
  570. - * * *
  571. .
  572. <ul>
  573. <li>Foo</li>
  574. <li>
  575. <hr />
  576. </li>
  577. </ul>
  578. ````````````````````````````````
  579. ## ATX headings
  580. An [ATX heading](@)
  581. consists of a string of characters, parsed as inline content, between an
  582. opening sequence of 1--6 unescaped `#` characters and an optional
  583. closing sequence of any number of unescaped `#` characters.
  584. The opening sequence of `#` characters must be followed by a
  585. [space] or by the end of line. The optional closing sequence of `#`s must be
  586. preceded by a [space] and may be followed by spaces only. The opening
  587. `#` character may be indented 0-3 spaces. The raw contents of the
  588. heading are stripped of leading and trailing spaces before being parsed
  589. as inline content. The heading level is equal to the number of `#`
  590. characters in the opening sequence.
  591. Simple headings:
  592. ```````````````````````````````` example
  593. # foo
  594. ## foo
  595. ### foo
  596. #### foo
  597. ##### foo
  598. ###### foo
  599. .
  600. <h1>foo</h1>
  601. <h2>foo</h2>
  602. <h3>foo</h3>
  603. <h4>foo</h4>
  604. <h5>foo</h5>
  605. <h6>foo</h6>
  606. ````````````````````````````````
  607. More than six `#` characters is not a heading:
  608. ```````````````````````````````` example
  609. ####### foo
  610. .
  611. <p>####### foo</p>
  612. ````````````````````````````````
  613. At least one space is required between the `#` characters and the
  614. heading's contents, unless the heading is empty. Note that many
  615. implementations currently do not require the space. However, the
  616. space was required by the
  617. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  618. and it helps prevent things like the following from being parsed as
  619. headings:
  620. ```````````````````````````````` example
  621. #5 bolt
  622. #hashtag
  623. .
  624. <p>#5 bolt</p>
  625. <p>#hashtag</p>
  626. ````````````````````````````````
  627. This is not a heading, because the first `#` is escaped:
  628. ```````````````````````````````` example
  629. \## foo
  630. .
  631. <p>## foo</p>
  632. ````````````````````````````````
  633. Contents are parsed as inlines:
  634. ```````````````````````````````` example
  635. # foo *bar* \*baz\*
  636. .
  637. <h1>foo <em>bar</em> *baz*</h1>
  638. ````````````````````````````````
  639. Leading and trailing blanks are ignored in parsing inline content:
  640. ```````````````````````````````` example
  641. # foo
  642. .
  643. <h1>foo</h1>
  644. ````````````````````````````````
  645. One to three spaces indentation are allowed:
  646. ```````````````````````````````` example
  647. ### foo
  648. ## foo
  649. # foo
  650. .
  651. <h3>foo</h3>
  652. <h2>foo</h2>
  653. <h1>foo</h1>
  654. ````````````````````````````````
  655. Four spaces are too much:
  656. ```````````````````````````````` example
  657. # foo
  658. .
  659. <pre><code># foo
  660. </code></pre>
  661. ````````````````````````````````
  662. ```````````````````````````````` example
  663. foo
  664. # bar
  665. .
  666. <p>foo
  667. # bar</p>
  668. ````````````````````````````````
  669. A closing sequence of `#` characters is optional:
  670. ```````````````````````````````` example
  671. ## foo ##
  672. ### bar ###
  673. .
  674. <h2>foo</h2>
  675. <h3>bar</h3>
  676. ````````````````````````````````
  677. It need not be the same length as the opening sequence:
  678. ```````````````````````````````` example
  679. # foo ##################################
  680. ##### foo ##
  681. .
  682. <h1>foo</h1>
  683. <h5>foo</h5>
  684. ````````````````````````````````
  685. Spaces are allowed after the closing sequence:
  686. ```````````````````````````````` example
  687. ### foo ###
  688. .
  689. <h3>foo</h3>
  690. ````````````````````````````````
  691. A sequence of `#` characters with anything but [spaces] following it
  692. is not a closing sequence, but counts as part of the contents of the
  693. heading:
  694. ```````````````````````````````` example
  695. ### foo ### b
  696. .
  697. <h3>foo ### b</h3>
  698. ````````````````````````````````
  699. The closing sequence must be preceded by a space:
  700. ```````````````````````````````` example
  701. # foo#
  702. .
  703. <h1>foo#</h1>
  704. ````````````````````````````````
  705. Backslash-escaped `#` characters do not count as part
  706. of the closing sequence:
  707. ```````````````````````````````` example
  708. ### foo \###
  709. ## foo #\##
  710. # foo \#
  711. .
  712. <h3>foo ###</h3>
  713. <h2>foo ###</h2>
  714. <h1>foo #</h1>
  715. ````````````````````````````````
  716. ATX headings need not be separated from surrounding content by blank
  717. lines, and they can interrupt paragraphs:
  718. ```````````````````````````````` example
  719. ****
  720. ## foo
  721. ****
  722. .
  723. <hr />
  724. <h2>foo</h2>
  725. <hr />
  726. ````````````````````````````````
  727. ```````````````````````````````` example
  728. Foo bar
  729. # baz
  730. Bar foo
  731. .
  732. <p>Foo bar</p>
  733. <h1>baz</h1>
  734. <p>Bar foo</p>
  735. ````````````````````````````````
  736. ATX headings can be empty:
  737. ```````````````````````````````` example
  738. ##
  739. #
  740. ### ###
  741. .
  742. <h2></h2>
  743. <h1></h1>
  744. <h3></h3>
  745. ````````````````````````````````
  746. ## Setext headings
  747. A [setext heading](@) consists of one or more
  748. lines of text, each containing at least one [non-whitespace
  749. character], with no more than 3 spaces indentation, followed by
  750. a [setext heading underline]. The lines of text must be such
  751. that, were they not followed by the setext heading underline,
  752. they would be interpreted as a paragraph: they cannot be
  753. interpretable as a [code fence], [ATX heading][ATX headings],
  754. [block quote][block quotes], [thematic break][thematic breaks],
  755. [list item][list items], or [HTML block][HTML blocks].
  756. A [setext heading underline](@) is a sequence of
  757. `=` characters or a sequence of `-` characters, with no more than 3
  758. spaces indentation and any number of trailing spaces. If a line
  759. containing a single `-` can be interpreted as an
  760. empty [list items], it should be interpreted this way
  761. and not as a [setext heading underline].
  762. The heading is a level 1 heading if `=` characters are used in
  763. the [setext heading underline], and a level 2 heading if `-`
  764. characters are used. The contents of the heading are the result
  765. of parsing the preceding lines of text as CommonMark inline
  766. content.
  767. In general, a setext heading need not be preceded or followed by a
  768. blank line. However, it cannot interrupt a paragraph, so when a
  769. setext heading comes after a paragraph, a blank line is needed between
  770. them.
  771. Simple examples:
  772. ```````````````````````````````` example
  773. Foo *bar*
  774. =========
  775. Foo *bar*
  776. ---------
  777. .
  778. <h1>Foo <em>bar</em></h1>
  779. <h2>Foo <em>bar</em></h2>
  780. ````````````````````````````````
  781. The content of the header may span more than one line:
  782. ```````````````````````````````` example
  783. Foo *bar
  784. baz*
  785. ====
  786. .
  787. <h1>Foo <em>bar
  788. baz</em></h1>
  789. ````````````````````````````````
  790. The underlining can be any length:
  791. ```````````````````````````````` example
  792. Foo
  793. -------------------------
  794. Foo
  795. =
  796. .
  797. <h2>Foo</h2>
  798. <h1>Foo</h1>
  799. ````````````````````````````````
  800. The heading content can be indented up to three spaces, and need
  801. not line up with the underlining:
  802. ```````````````````````````````` example
  803. Foo
  804. ---
  805. Foo
  806. -----
  807. Foo
  808. ===
  809. .
  810. <h2>Foo</h2>
  811. <h2>Foo</h2>
  812. <h1>Foo</h1>
  813. ````````````````````````````````
  814. Four spaces indent is too much:
  815. ```````````````````````````````` example
  816. Foo
  817. ---
  818. Foo
  819. ---
  820. .
  821. <pre><code>Foo
  822. ---
  823. Foo
  824. </code></pre>
  825. <hr />
  826. ````````````````````````````````
  827. The setext heading underline can be indented up to three spaces, and
  828. may have trailing spaces:
  829. ```````````````````````````````` example
  830. Foo
  831. ----
  832. .
  833. <h2>Foo</h2>
  834. ````````````````````````````````
  835. Four spaces is too much:
  836. ```````````````````````````````` example
  837. Foo
  838. ---
  839. .
  840. <p>Foo
  841. ---</p>
  842. ````````````````````````````````
  843. The setext heading underline cannot contain internal spaces:
  844. ```````````````````````````````` example
  845. Foo
  846. = =
  847. Foo
  848. --- -
  849. .
  850. <p>Foo
  851. = =</p>
  852. <p>Foo</p>
  853. <hr />
  854. ````````````````````````````````
  855. Trailing spaces in the content line do not cause a line break:
  856. ```````````````````````````````` example
  857. Foo
  858. -----
  859. .
  860. <h2>Foo</h2>
  861. ````````````````````````````````
  862. Nor does a backslash at the end:
  863. ```````````````````````````````` example
  864. Foo\
  865. ----
  866. .
  867. <h2>Foo\</h2>
  868. ````````````````````````````````
  869. Since indicators of block structure take precedence over
  870. indicators of inline structure, the following are setext headings:
  871. ```````````````````````````````` example
  872. `Foo
  873. ----
  874. `
  875. <a title="a lot
  876. ---
  877. of dashes"/>
  878. .
  879. <h2>`Foo</h2>
  880. <p>`</p>
  881. <h2>&lt;a title=&quot;a lot</h2>
  882. <p>of dashes&quot;/&gt;</p>
  883. ````````````````````````````````
  884. The setext heading underline cannot be a [lazy continuation
  885. line] in a list item or block quote:
  886. ```````````````````````````````` example
  887. > Foo
  888. ---
  889. .
  890. <blockquote>
  891. <p>Foo</p>
  892. </blockquote>
  893. <hr />
  894. ````````````````````````````````
  895. ```````````````````````````````` example
  896. > foo
  897. bar
  898. ===
  899. .
  900. <blockquote>
  901. <p>foo
  902. bar
  903. ===</p>
  904. </blockquote>
  905. ````````````````````````````````
  906. ```````````````````````````````` example
  907. - Foo
  908. ---
  909. .
  910. <ul>
  911. <li>Foo</li>
  912. </ul>
  913. <hr />
  914. ````````````````````````````````
  915. A blank line is needed between a paragraph and a following
  916. setext heading, since otherwise the paragraph becomes part
  917. of the heading's content:
  918. ```````````````````````````````` example
  919. Foo
  920. Bar
  921. ---
  922. .
  923. <h2>Foo
  924. Bar</h2>
  925. ````````````````````````````````
  926. But in general a blank line is not required before or after
  927. setext headings:
  928. ```````````````````````````````` example
  929. ---
  930. Foo
  931. ---
  932. Bar
  933. ---
  934. Baz
  935. .
  936. <hr />
  937. <h2>Foo</h2>
  938. <h2>Bar</h2>
  939. <p>Baz</p>
  940. ````````````````````````````````
  941. Setext headings cannot be empty:
  942. ```````````````````````````````` example
  943. ====
  944. .
  945. <p>====</p>
  946. ````````````````````````````````
  947. Setext heading text lines must not be interpretable as block
  948. constructs other than paragraphs. So, the line of dashes
  949. in these examples gets interpreted as a thematic break:
  950. ```````````````````````````````` example
  951. ---
  952. ---
  953. .
  954. <hr />
  955. <hr />
  956. ````````````````````````````````
  957. ```````````````````````````````` example
  958. - foo
  959. -----
  960. .
  961. <ul>
  962. <li>foo</li>
  963. </ul>
  964. <hr />
  965. ````````````````````````````````
  966. ```````````````````````````````` example
  967. foo
  968. ---
  969. .
  970. <pre><code>foo
  971. </code></pre>
  972. <hr />
  973. ````````````````````````````````
  974. ```````````````````````````````` example
  975. > foo
  976. -----
  977. .
  978. <blockquote>
  979. <p>foo</p>
  980. </blockquote>
  981. <hr />
  982. ````````````````````````````````
  983. If you want a heading with `> foo` as its literal text, you can
  984. use backslash escapes:
  985. ```````````````````````````````` example
  986. \> foo
  987. ------
  988. .
  989. <h2>&gt; foo</h2>
  990. ````````````````````````````````
  991. **Compatibility note:** Most existing Markdown implementations
  992. do not allow the text of setext headings to span multiple lines.
  993. But there is no consensus about how to interpret
  994. ``` markdown
  995. Foo
  996. bar
  997. ---
  998. baz
  999. ```
  1000. One can find four different interpretations:
  1001. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1002. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1003. 3. paragraph "Foo bar --- baz"
  1004. 4. heading "Foo bar", paragraph "baz"
  1005. We find interpretation 4 most natural, and interpretation 4
  1006. increases the expressive power of CommonMark, by allowing
  1007. multiline headings. Authors who want interpretation 1 can
  1008. put a blank line after the first paragraph:
  1009. ```````````````````````````````` example
  1010. Foo
  1011. bar
  1012. ---
  1013. baz
  1014. .
  1015. <p>Foo</p>
  1016. <h2>bar</h2>
  1017. <p>baz</p>
  1018. ````````````````````````````````
  1019. Authors who want interpretation 2 can put blank lines around
  1020. the thematic break,
  1021. ```````````````````````````````` example
  1022. Foo
  1023. bar
  1024. ---
  1025. baz
  1026. .
  1027. <p>Foo
  1028. bar</p>
  1029. <hr />
  1030. <p>baz</p>
  1031. ````````````````````````````````
  1032. or use a thematic break that cannot count as a [setext heading
  1033. underline], such as
  1034. ```````````````````````````````` example
  1035. Foo
  1036. bar
  1037. * * *
  1038. baz
  1039. .
  1040. <p>Foo
  1041. bar</p>
  1042. <hr />
  1043. <p>baz</p>
  1044. ````````````````````````````````
  1045. Authors who want interpretation 3 can use backslash escapes:
  1046. ```````````````````````````````` example
  1047. Foo
  1048. bar
  1049. \---
  1050. baz
  1051. .
  1052. <p>Foo
  1053. bar
  1054. ---
  1055. baz</p>
  1056. ````````````````````````````````
  1057. ## Indented code blocks
  1058. An [indented code block](@) is composed of one or more
  1059. [indented chunks] separated by blank lines.
  1060. An [indented chunk](@) is a sequence of non-blank lines,
  1061. each indented four or more spaces. The contents of the code block are
  1062. the literal contents of the lines, including trailing
  1063. [line endings], minus four spaces of indentation.
  1064. An indented code block has no [info string].
  1065. An indented code block cannot interrupt a paragraph, so there must be
  1066. a blank line between a paragraph and a following indented code block.
  1067. (A blank line is not needed, however, between a code block and a following
  1068. paragraph.)
  1069. ```````````````````````````````` example
  1070. a simple
  1071. indented code block
  1072. .
  1073. <pre><code>a simple
  1074. indented code block
  1075. </code></pre>
  1076. ````````````````````````````````
  1077. If there is any ambiguity between an interpretation of indentation
  1078. as a code block and as indicating that material belongs to a [list
  1079. item][list items], the list item interpretation takes precedence:
  1080. ```````````````````````````````` example
  1081. - foo
  1082. bar
  1083. .
  1084. <ul>
  1085. <li>
  1086. <p>foo</p>
  1087. <p>bar</p>
  1088. </li>
  1089. </ul>
  1090. ````````````````````````````````
  1091. ```````````````````````````````` example
  1092. 1. foo
  1093. - bar
  1094. .
  1095. <ol>
  1096. <li>
  1097. <p>foo</p>
  1098. <ul>
  1099. <li>bar</li>
  1100. </ul>
  1101. </li>
  1102. </ol>
  1103. ````````````````````````````````
  1104. The contents of a code block are literal text, and do not get parsed
  1105. as Markdown:
  1106. ```````````````````````````````` example
  1107. <a/>
  1108. *hi*
  1109. - one
  1110. .
  1111. <pre><code>&lt;a/&gt;
  1112. *hi*
  1113. - one
  1114. </code></pre>
  1115. ````````````````````````````````
  1116. Here we have three chunks separated by blank lines:
  1117. ```````````````````````````````` example
  1118. chunk1
  1119. chunk2
  1120. chunk3
  1121. .
  1122. <pre><code>chunk1
  1123. chunk2
  1124. chunk3
  1125. </code></pre>
  1126. ````````````````````````````````
  1127. Any initial spaces beyond four will be included in the content, even
  1128. in interior blank lines:
  1129. ```````````````````````````````` example
  1130. chunk1
  1131. chunk2
  1132. .
  1133. <pre><code>chunk1
  1134. chunk2
  1135. </code></pre>
  1136. ````````````````````````````````
  1137. An indented code block cannot interrupt a paragraph. (This
  1138. allows hanging indents and the like.)
  1139. ```````````````````````````````` example
  1140. Foo
  1141. bar
  1142. .
  1143. <p>Foo
  1144. bar</p>
  1145. ````````````````````````````````
  1146. However, any non-blank line with fewer than four leading spaces ends
  1147. the code block immediately. So a paragraph may occur immediately
  1148. after indented code:
  1149. ```````````````````````````````` example
  1150. foo
  1151. bar
  1152. .
  1153. <pre><code>foo
  1154. </code></pre>
  1155. <p>bar</p>
  1156. ````````````````````````````````
  1157. And indented code can occur immediately before and after other kinds of
  1158. blocks:
  1159. ```````````````````````````````` example
  1160. # Heading
  1161. foo
  1162. Heading
  1163. ------
  1164. foo
  1165. ----
  1166. .
  1167. <h1>Heading</h1>
  1168. <pre><code>foo
  1169. </code></pre>
  1170. <h2>Heading</h2>
  1171. <pre><code>foo
  1172. </code></pre>
  1173. <hr />
  1174. ````````````````````````````````
  1175. The first line can be indented more than four spaces:
  1176. ```````````````````````````````` example
  1177. foo
  1178. bar
  1179. .
  1180. <pre><code> foo
  1181. bar
  1182. </code></pre>
  1183. ````````````````````````````````
  1184. Blank lines preceding or following an indented code block
  1185. are not included in it:
  1186. ```````````````````````````````` example
  1187. foo
  1188. .
  1189. <pre><code>foo
  1190. </code></pre>
  1191. ````````````````````````````````
  1192. Trailing spaces are included in the code block's content:
  1193. ```````````````````````````````` example
  1194. foo
  1195. .
  1196. <pre><code>foo
  1197. </code></pre>
  1198. ````````````````````````````````
  1199. ## Fenced code blocks
  1200. A [code fence](@) is a sequence
  1201. of at least three consecutive backtick characters (`` ` ``) or
  1202. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1203. A [fenced code block](@)
  1204. begins with a code fence, indented no more than three spaces.
  1205. The line with the opening code fence may optionally contain some text
  1206. following the code fence; this is trimmed of leading and trailing
  1207. whitespace and called the [info string](@). If the [info string] comes
  1208. after a backtick fence, it may not contain any backtick
  1209. characters. (The reason for this restriction is that otherwise
  1210. some inline code would be incorrectly interpreted as the
  1211. beginning of a fenced code block.)
  1212. The content of the code block consists of all subsequent lines, until
  1213. a closing [code fence] of the same type as the code block
  1214. began with (backticks or tildes), and with at least as many backticks
  1215. or tildes as the opening code fence. If the leading code fence is
  1216. indented N spaces, then up to N spaces of indentation are removed from
  1217. each line of the content (if present). (If a content line is not
  1218. indented, it is preserved unchanged. If it is indented less than N
  1219. spaces, all of the indentation is removed.)
  1220. The closing code fence may be indented up to three spaces, and may be
  1221. followed only by spaces, which are ignored. If the end of the
  1222. containing block (or document) is reached and no closing code fence
  1223. has been found, the code block contains all of the lines after the
  1224. opening code fence until the end of the containing block (or
  1225. document). (An alternative spec would require backtracking in the
  1226. event that a closing code fence is not found. But this makes parsing
  1227. much less efficient, and there seems to be no real down side to the
  1228. behavior described here.)
  1229. A fenced code block may interrupt a paragraph, and does not require
  1230. a blank line either before or after.
  1231. The content of a code fence is treated as literal text, not parsed
  1232. as inlines. The first word of the [info string] is typically used to
  1233. specify the language of the code sample, and rendered in the `class`
  1234. attribute of the `code` tag. However, this spec does not mandate any
  1235. particular treatment of the [info string].
  1236. Here is a simple example with backticks:
  1237. ```````````````````````````````` example
  1238. ```
  1239. <
  1240. >
  1241. ```
  1242. .
  1243. <pre><code>&lt;
  1244. &gt;
  1245. </code></pre>
  1246. ````````````````````````````````
  1247. With tildes:
  1248. ```````````````````````````````` example
  1249. ~~~
  1250. <
  1251. >
  1252. ~~~
  1253. .
  1254. <pre><code>&lt;
  1255. &gt;
  1256. </code></pre>
  1257. ````````````````````````````````
  1258. Fewer than three backticks is not enough:
  1259. ```````````````````````````````` example
  1260. ``
  1261. foo
  1262. ``
  1263. .
  1264. <p><code>foo</code></p>
  1265. ````````````````````````````````
  1266. The closing code fence must use the same character as the opening
  1267. fence:
  1268. ```````````````````````````````` example
  1269. ```
  1270. aaa
  1271. ~~~
  1272. ```
  1273. .
  1274. <pre><code>aaa
  1275. ~~~
  1276. </code></pre>
  1277. ````````````````````````````````
  1278. ```````````````````````````````` example
  1279. ~~~
  1280. aaa
  1281. ```
  1282. ~~~
  1283. .
  1284. <pre><code>aaa
  1285. ```
  1286. </code></pre>
  1287. ````````````````````````````````
  1288. The closing code fence must be at least as long as the opening fence:
  1289. ```````````````````````````````` example
  1290. ````
  1291. aaa
  1292. ```
  1293. ``````
  1294. .
  1295. <pre><code>aaa
  1296. ```
  1297. </code></pre>
  1298. ````````````````````````````````
  1299. ```````````````````````````````` example
  1300. ~~~~
  1301. aaa
  1302. ~~~
  1303. ~~~~
  1304. .
  1305. <pre><code>aaa
  1306. ~~~
  1307. </code></pre>
  1308. ````````````````````````````````
  1309. Unclosed code blocks are closed by the end of the document
  1310. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1311. ```````````````````````````````` example
  1312. ```
  1313. .
  1314. <pre><code></code></pre>
  1315. ````````````````````````````````
  1316. ```````````````````````````````` example
  1317. `````
  1318. ```
  1319. aaa
  1320. .
  1321. <pre><code>
  1322. ```
  1323. aaa
  1324. </code></pre>
  1325. ````````````````````````````````
  1326. ```````````````````````````````` example
  1327. > ```
  1328. > aaa
  1329. bbb
  1330. .
  1331. <blockquote>
  1332. <pre><code>aaa
  1333. </code></pre>
  1334. </blockquote>
  1335. <p>bbb</p>
  1336. ````````````````````````````````
  1337. A code block can have all empty lines as its content:
  1338. ```````````````````````````````` example
  1339. ```
  1340. ```
  1341. .
  1342. <pre><code>
  1343. </code></pre>
  1344. ````````````````````````````````
  1345. A code block can be empty:
  1346. ```````````````````````````````` example
  1347. ```
  1348. ```
  1349. .
  1350. <pre><code></code></pre>
  1351. ````````````````````````````````
  1352. Fences can be indented. If the opening fence is indented,
  1353. content lines will have equivalent opening indentation removed,
  1354. if present:
  1355. ```````````````````````````````` example
  1356. ```
  1357. aaa
  1358. aaa
  1359. ```
  1360. .
  1361. <pre><code>aaa
  1362. aaa
  1363. </code></pre>
  1364. ````````````````````````````````
  1365. ```````````````````````````````` example
  1366. ```
  1367. aaa
  1368. aaa
  1369. aaa
  1370. ```
  1371. .
  1372. <pre><code>aaa
  1373. aaa
  1374. aaa
  1375. </code></pre>
  1376. ````````````````````````````````
  1377. ```````````````````````````````` example
  1378. ```
  1379. aaa
  1380. aaa
  1381. aaa
  1382. ```
  1383. .
  1384. <pre><code>aaa
  1385. aaa
  1386. aaa
  1387. </code></pre>
  1388. ````````````````````````````````
  1389. Four spaces indentation produces an indented code block:
  1390. ```````````````````````````````` example
  1391. ```
  1392. aaa
  1393. ```
  1394. .
  1395. <pre><code>```
  1396. aaa
  1397. ```
  1398. </code></pre>
  1399. ````````````````````````````````
  1400. Closing fences may be indented by 0-3 spaces, and their indentation
  1401. need not match that of the opening fence:
  1402. ```````````````````````````````` example
  1403. ```
  1404. aaa
  1405. ```
  1406. .
  1407. <pre><code>aaa
  1408. </code></pre>
  1409. ````````````````````````````````
  1410. ```````````````````````````````` example
  1411. ```
  1412. aaa
  1413. ```
  1414. .
  1415. <pre><code>aaa
  1416. </code></pre>
  1417. ````````````````````````````````
  1418. This is not a closing fence, because it is indented 4 spaces:
  1419. ```````````````````````````````` example
  1420. ```
  1421. aaa
  1422. ```
  1423. .
  1424. <pre><code>aaa
  1425. ```
  1426. </code></pre>
  1427. ````````````````````````````````
  1428. Code fences (opening and closing) cannot contain internal spaces:
  1429. ```````````````````````````````` example
  1430. ``` ```
  1431. aaa
  1432. .
  1433. <p><code></code>
  1434. aaa</p>
  1435. ````````````````````````````````
  1436. ```````````````````````````````` example
  1437. ~~~~~~
  1438. aaa
  1439. ~~~ ~~
  1440. .
  1441. <pre><code>aaa
  1442. ~~~ ~~
  1443. </code></pre>
  1444. ````````````````````````````````
  1445. Fenced code blocks can interrupt paragraphs, and can be followed
  1446. directly by paragraphs, without a blank line between:
  1447. ```````````````````````````````` example
  1448. foo
  1449. ```
  1450. bar
  1451. ```
  1452. baz
  1453. .
  1454. <p>foo</p>
  1455. <pre><code>bar
  1456. </code></pre>
  1457. <p>baz</p>
  1458. ````````````````````````````````
  1459. Other blocks can also occur before and after fenced code blocks
  1460. without an intervening blank line:
  1461. ```````````````````````````````` example
  1462. foo
  1463. ---
  1464. ~~~
  1465. bar
  1466. ~~~
  1467. # baz
  1468. .
  1469. <h2>foo</h2>
  1470. <pre><code>bar
  1471. </code></pre>
  1472. <h1>baz</h1>
  1473. ````````````````````````````````
  1474. An [info string] can be provided after the opening code fence.
  1475. Opening and closing spaces will be stripped, and the first word, prefixed
  1476. with `language-`, is used as the value for the `class` attribute of the
  1477. `code` element within the enclosing `pre` element.
  1478. ```````````````````````````````` example
  1479. ```ruby
  1480. def foo(x)
  1481. return 3
  1482. end
  1483. ```
  1484. .
  1485. <pre><code class="language-ruby">def foo(x)
  1486. return 3
  1487. end
  1488. </code></pre>
  1489. ````````````````````````````````
  1490. ```````````````````````````````` example
  1491. ~~~~ ruby startline=3 $%@#$
  1492. def foo(x)
  1493. return 3
  1494. end
  1495. ~~~~~~~
  1496. .
  1497. <pre><code class="language-ruby">def foo(x)
  1498. return 3
  1499. end
  1500. </code></pre>
  1501. ````````````````````````````````
  1502. ```````````````````````````````` example
  1503. ````;
  1504. ````
  1505. .
  1506. <pre><code class="language-;"></code></pre>
  1507. ````````````````````````````````
  1508. [Info strings] for backtick code blocks cannot contain backticks:
  1509. ```````````````````````````````` example
  1510. ``` aa ```
  1511. foo
  1512. .
  1513. <p><code>aa</code>
  1514. foo</p>
  1515. ````````````````````````````````
  1516. [Info strings] for tilde code blocks can contain backticks and tildes:
  1517. ```````````````````````````````` example
  1518. ~~~ aa ``` ~~~
  1519. foo
  1520. ~~~
  1521. .
  1522. <pre><code class="language-aa">foo
  1523. </code></pre>
  1524. ````````````````````````````````
  1525. Closing code fences cannot have [info strings]:
  1526. ```````````````````````````````` example
  1527. ```
  1528. ``` aaa
  1529. ```
  1530. .
  1531. <pre><code>``` aaa
  1532. </code></pre>
  1533. ````````````````````````````````
  1534. ## HTML blocks
  1535. An [HTML block](@) is a group of lines that is treated
  1536. as raw HTML (and will not be escaped in HTML output).
  1537. There are seven kinds of [HTML block], which can be defined
  1538. by their start and end conditions. The block begins with a line that
  1539. meets a [start condition](@) (after up to three spaces
  1540. optional indentation). It ends with the first subsequent line that
  1541. meets a matching [end condition](@), or the last line of
  1542. the document or other [container block](#container-blocks)), if no
  1543. line is encountered that meets the [end condition]. If the first line
  1544. meets both the [start condition] and the [end condition], the block
  1545. will contain just that line.
  1546. 1. **Start condition:** line begins with the string `<script`,
  1547. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1548. the string `>`, or the end of the line.\
  1549. **End condition:** line contains an end tag
  1550. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1551. need not match the start tag).
  1552. 2. **Start condition:** line begins with the string `<!--`.\
  1553. **End condition:** line contains the string `-->`.
  1554. 3. **Start condition:** line begins with the string `<?`.\
  1555. **End condition:** line contains the string `?>`.
  1556. 4. **Start condition:** line begins with the string `<!`
  1557. followed by an uppercase ASCII letter.\
  1558. **End condition:** line contains the character `>`.
  1559. 5. **Start condition:** line begins with the string
  1560. `<![CDATA[`.\
  1561. **End condition:** line contains the string `]]>`.
  1562. 6. **Start condition:** line begins the string `<` or `</`
  1563. followed by one of the strings (case-insensitive) `address`,
  1564. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1565. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1566. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1567. `footer`, `form`, `frame`, `frameset`,
  1568. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1569. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1570. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1571. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1572. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1573. by [whitespace], the end of the line, the string `>`, or
  1574. the string `/>`.\
  1575. **End condition:** line is followed by a [blank line].
  1576. 7. **Start condition:** line begins with a complete [open tag]
  1577. or [closing tag] (with any [tag name] other than `script`,
  1578. `style`, or `pre`) followed only by [whitespace]
  1579. or the end of the line.\
  1580. **End condition:** line is followed by a [blank line].
  1581. HTML blocks continue until they are closed by their appropriate
  1582. [end condition], or the last line of the document or other [container
  1583. block](#container-blocks). This means any HTML **within an HTML
  1584. block** that might otherwise be recognised as a start condition will
  1585. be ignored by the parser and passed through as-is, without changing
  1586. the parser's state.
  1587. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1588. the parser state; as the HTML block was started in by start condition 6, it
  1589. will end at any blank line. This can be surprising:
  1590. ```````````````````````````````` example
  1591. <table><tr><td>
  1592. <pre>
  1593. **Hello**,
  1594. _world_.
  1595. </pre>
  1596. </td></tr></table>
  1597. .
  1598. <table><tr><td>
  1599. <pre>
  1600. **Hello**,
  1601. <p><em>world</em>.
  1602. </pre></p>
  1603. </td></tr></table>
  1604. ````````````````````````````````
  1605. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1606. text remains verbatim — and regular parsing resumes, with a paragraph,
  1607. emphasised `world` and inline and block HTML following.
  1608. All types of [HTML blocks] except type 7 may interrupt
  1609. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1610. (This restriction is intended to prevent unwanted interpretation
  1611. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1612. Some simple examples follow. Here are some basic HTML blocks
  1613. of type 6:
  1614. ```````````````````````````````` example
  1615. <table>
  1616. <tr>
  1617. <td>
  1618. hi
  1619. </td>
  1620. </tr>
  1621. </table>
  1622. okay.
  1623. .
  1624. <table>
  1625. <tr>
  1626. <td>
  1627. hi
  1628. </td>
  1629. </tr>
  1630. </table>
  1631. <p>okay.</p>
  1632. ````````````````````````````````
  1633. ```````````````````````````````` example
  1634. <div>
  1635. *hello*
  1636. <foo><a>
  1637. .
  1638. <div>
  1639. *hello*
  1640. <foo><a>
  1641. ````````````````````````````````
  1642. A block can also start with a closing tag:
  1643. ```````````````````````````````` example
  1644. </div>
  1645. *foo*
  1646. .
  1647. </div>
  1648. *foo*
  1649. ````````````````````````````````
  1650. Here we have two HTML blocks with a Markdown paragraph between them:
  1651. ```````````````````````````````` example
  1652. <DIV CLASS="foo">
  1653. *Markdown*
  1654. </DIV>
  1655. .
  1656. <DIV CLASS="foo">
  1657. <p><em>Markdown</em></p>
  1658. </DIV>
  1659. ````````````````````````````````
  1660. The tag on the first line can be partial, as long
  1661. as it is split where there would be whitespace:
  1662. ```````````````````````````````` example
  1663. <div id="foo"
  1664. class="bar">
  1665. </div>
  1666. .
  1667. <div id="foo"
  1668. class="bar">
  1669. </div>
  1670. ````````````````````````````````
  1671. ```````````````````````````````` example
  1672. <div id="foo" class="bar
  1673. baz">
  1674. </div>
  1675. .
  1676. <div id="foo" class="bar
  1677. baz">
  1678. </div>
  1679. ````````````````````````````````
  1680. An open tag need not be closed:
  1681. ```````````````````````````````` example
  1682. <div>
  1683. *foo*
  1684. *bar*
  1685. .
  1686. <div>
  1687. *foo*
  1688. <p><em>bar</em></p>
  1689. ````````````````````````````````
  1690. A partial tag need not even be completed (garbage
  1691. in, garbage out):
  1692. ```````````````````````````````` example
  1693. <div id="foo"
  1694. *hi*
  1695. .
  1696. <div id="foo"
  1697. *hi*
  1698. ````````````````````````````````
  1699. ```````````````````````````````` example
  1700. <div class
  1701. foo
  1702. .
  1703. <div class
  1704. foo
  1705. ````````````````````````````````
  1706. The initial tag doesn't even need to be a valid
  1707. tag, as long as it starts like one:
  1708. ```````````````````````````````` example
  1709. <div *???-&&&-<---
  1710. *foo*
  1711. .
  1712. <div *???-&&&-<---
  1713. *foo*
  1714. ````````````````````````````````
  1715. In type 6 blocks, the initial tag need not be on a line by
  1716. itself:
  1717. ```````````````````````````````` example
  1718. <div><a href="bar">*foo*</a></div>
  1719. .
  1720. <div><a href="bar">*foo*</a></div>
  1721. ````````````````````````````````
  1722. ```````````````````````````````` example
  1723. <table><tr><td>
  1724. foo
  1725. </td></tr></table>
  1726. .
  1727. <table><tr><td>
  1728. foo
  1729. </td></tr></table>
  1730. ````````````````````````````````
  1731. Everything until the next blank line or end of document
  1732. gets included in the HTML block. So, in the following
  1733. example, what looks like a Markdown code block
  1734. is actually part of the HTML block, which continues until a blank
  1735. line or the end of the document is reached:
  1736. ```````````````````````````````` example
  1737. <div></div>
  1738. ``` c
  1739. int x = 33;
  1740. ```
  1741. .
  1742. <div></div>
  1743. ``` c
  1744. int x = 33;
  1745. ```
  1746. ````````````````````````````````
  1747. To start an [HTML block] with a tag that is *not* in the
  1748. list of block-level tags in (6), you must put the tag by
  1749. itself on the first line (and it must be complete):
  1750. ```````````````````````````````` example
  1751. <a href="foo">
  1752. *bar*
  1753. </a>
  1754. .
  1755. <a href="foo">
  1756. *bar*
  1757. </a>
  1758. ````````````````````````````````
  1759. In type 7 blocks, the [tag name] can be anything:
  1760. ```````````````````````````````` example
  1761. <Warning>
  1762. *bar*
  1763. </Warning>
  1764. .
  1765. <Warning>
  1766. *bar*
  1767. </Warning>
  1768. ````````````````````````````````
  1769. ```````````````````````````````` example
  1770. <i class="foo">
  1771. *bar*
  1772. </i>
  1773. .
  1774. <i class="foo">
  1775. *bar*
  1776. </i>
  1777. ````````````````````````````````
  1778. ```````````````````````````````` example
  1779. </ins>
  1780. *bar*
  1781. .
  1782. </ins>
  1783. *bar*
  1784. ````````````````````````````````
  1785. These rules are designed to allow us to work with tags that
  1786. can function as either block-level or inline-level tags.
  1787. The `<del>` tag is a nice example. We can surround content with
  1788. `<del>` tags in three different ways. In this case, we get a raw
  1789. HTML block, because the `<del>` tag is on a line by itself:
  1790. ```````````````````````````````` example
  1791. <del>
  1792. *foo*
  1793. </del>
  1794. .
  1795. <del>
  1796. *foo*
  1797. </del>
  1798. ````````````````````````````````
  1799. In this case, we get a raw HTML block that just includes
  1800. the `<del>` tag (because it ends with the following blank
  1801. line). So the contents get interpreted as CommonMark:
  1802. ```````````````````````````````` example
  1803. <del>
  1804. *foo*
  1805. </del>
  1806. .
  1807. <del>
  1808. <p><em>foo</em></p>
  1809. </del>
  1810. ````````````````````````````````
  1811. Finally, in this case, the `<del>` tags are interpreted
  1812. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1813. the tag is not on a line by itself, we get inline HTML
  1814. rather than an [HTML block].)
  1815. ```````````````````````````````` example
  1816. <del>*foo*</del>
  1817. .
  1818. <p><del><em>foo</em></del></p>
  1819. ````````````````````````````````
  1820. HTML tags designed to contain literal content
  1821. (`script`, `style`, `pre`), comments, processing instructions,
  1822. and declarations are treated somewhat differently.
  1823. Instead of ending at the first blank line, these blocks
  1824. end at the first line containing a corresponding end tag.
  1825. As a result, these blocks can contain blank lines:
  1826. A pre tag (type 1):
  1827. ```````````````````````````````` example
  1828. <pre language="haskell"><code>
  1829. import Text.HTML.TagSoup
  1830. main :: IO ()
  1831. main = print $ parseTags tags
  1832. </code></pre>
  1833. okay
  1834. .
  1835. <pre language="haskell"><code>
  1836. import Text.HTML.TagSoup
  1837. main :: IO ()
  1838. main = print $ parseTags tags
  1839. </code></pre>
  1840. <p>okay</p>
  1841. ````````````````````````````````
  1842. A script tag (type 1):
  1843. ```````````````````````````````` example
  1844. <script type="text/javascript">
  1845. // JavaScript example
  1846. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1847. </script>
  1848. okay
  1849. .
  1850. <script type="text/javascript">
  1851. // JavaScript example
  1852. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1853. </script>
  1854. <p>okay</p>
  1855. ````````````````````````````````
  1856. A style tag (type 1):
  1857. ```````````````````````````````` example
  1858. <style
  1859. type="text/css">
  1860. h1 {color:red;}
  1861. p {color:blue;}
  1862. </style>
  1863. okay
  1864. .
  1865. <style
  1866. type="text/css">
  1867. h1 {color:red;}
  1868. p {color:blue;}
  1869. </style>
  1870. <p>okay</p>
  1871. ````````````````````````````````
  1872. If there is no matching end tag, the block will end at the
  1873. end of the document (or the enclosing [block quote][block quotes]
  1874. or [list item][list items]):
  1875. ```````````````````````````````` example
  1876. <style
  1877. type="text/css">
  1878. foo
  1879. .
  1880. <style
  1881. type="text/css">
  1882. foo
  1883. ````````````````````````````````
  1884. ```````````````````````````````` example
  1885. > <div>
  1886. > foo
  1887. bar
  1888. .
  1889. <blockquote>
  1890. <div>
  1891. foo
  1892. </blockquote>
  1893. <p>bar</p>
  1894. ````````````````````````````````
  1895. ```````````````````````````````` example
  1896. - <div>
  1897. - foo
  1898. .
  1899. <ul>
  1900. <li>
  1901. <div>
  1902. </li>
  1903. <li>foo</li>
  1904. </ul>
  1905. ````````````````````````````````
  1906. The end tag can occur on the same line as the start tag:
  1907. ```````````````````````````````` example
  1908. <style>p{color:red;}</style>
  1909. *foo*
  1910. .
  1911. <style>p{color:red;}</style>
  1912. <p><em>foo</em></p>
  1913. ````````````````````````````````
  1914. ```````````````````````````````` example
  1915. <!-- foo -->*bar*
  1916. *baz*
  1917. .
  1918. <!-- foo -->*bar*
  1919. <p><em>baz</em></p>
  1920. ````````````````````````````````
  1921. Note that anything on the last line after the
  1922. end tag will be included in the [HTML block]:
  1923. ```````````````````````````````` example
  1924. <script>
  1925. foo
  1926. </script>1. *bar*
  1927. .
  1928. <script>
  1929. foo
  1930. </script>1. *bar*
  1931. ````````````````````````````````
  1932. A comment (type 2):
  1933. ```````````````````````````````` example
  1934. <!-- Foo
  1935. bar
  1936. baz -->
  1937. okay
  1938. .
  1939. <!-- Foo
  1940. bar
  1941. baz -->
  1942. <p>okay</p>
  1943. ````````````````````````````````
  1944. A processing instruction (type 3):
  1945. ```````````````````````````````` example
  1946. <?php
  1947. echo '>';
  1948. ?>
  1949. okay
  1950. .
  1951. <?php
  1952. echo '>';
  1953. ?>
  1954. <p>okay</p>
  1955. ````````````````````````````````
  1956. A declaration (type 4):
  1957. ```````````````````````````````` example
  1958. <!DOCTYPE html>
  1959. .
  1960. <!DOCTYPE html>
  1961. ````````````````````````````````
  1962. CDATA (type 5):
  1963. ```````````````````````````````` example
  1964. <![CDATA[
  1965. function matchwo(a,b)
  1966. {
  1967. if (a < b && a < 0) then {
  1968. return 1;
  1969. } else {
  1970. return 0;
  1971. }
  1972. }
  1973. ]]>
  1974. okay
  1975. .
  1976. <![CDATA[
  1977. function matchwo(a,b)
  1978. {
  1979. if (a < b && a < 0) then {
  1980. return 1;
  1981. } else {
  1982. return 0;
  1983. }
  1984. }
  1985. ]]>
  1986. <p>okay</p>
  1987. ````````````````````````````````
  1988. The opening tag can be indented 1-3 spaces, but not 4:
  1989. ```````````````````````````````` example
  1990. <!-- foo -->
  1991. <!-- foo -->
  1992. .
  1993. <!-- foo -->
  1994. <pre><code>&lt;!-- foo --&gt;
  1995. </code></pre>
  1996. ````````````````````````````````
  1997. ```````````````````````````````` example
  1998. <div>
  1999. <div>
  2000. .
  2001. <div>
  2002. <pre><code>&lt;div&gt;
  2003. </code></pre>
  2004. ````````````````````````````````
  2005. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2006. preceded by a blank line.
  2007. ```````````````````````````````` example
  2008. Foo
  2009. <div>
  2010. bar
  2011. </div>
  2012. .
  2013. <p>Foo</p>
  2014. <div>
  2015. bar
  2016. </div>
  2017. ````````````````````````````````
  2018. However, a following blank line is needed, except at the end of
  2019. a document, and except for blocks of types 1--5, [above][HTML
  2020. block]:
  2021. ```````````````````````````````` example
  2022. <div>
  2023. bar
  2024. </div>
  2025. *foo*
  2026. .
  2027. <div>
  2028. bar
  2029. </div>
  2030. *foo*
  2031. ````````````````````````````````
  2032. HTML blocks of type 7 cannot interrupt a paragraph:
  2033. ```````````````````````````````` example
  2034. Foo
  2035. <a href="bar">
  2036. baz
  2037. .
  2038. <p>Foo
  2039. <a href="bar">
  2040. baz</p>
  2041. ````````````````````````````````
  2042. This rule differs from John Gruber's original Markdown syntax
  2043. specification, which says:
  2044. > The only restrictions are that block-level HTML elements —
  2045. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2046. > surrounding content by blank lines, and the start and end tags of the
  2047. > block should not be indented with tabs or spaces.
  2048. In some ways Gruber's rule is more restrictive than the one given
  2049. here:
  2050. - It requires that an HTML block be preceded by a blank line.
  2051. - It does not allow the start tag to be indented.
  2052. - It requires a matching end tag, which it also does not allow to
  2053. be indented.
  2054. Most Markdown implementations (including some of Gruber's own) do not
  2055. respect all of these restrictions.
  2056. There is one respect, however, in which Gruber's rule is more liberal
  2057. than the one given here, since it allows blank lines to occur inside
  2058. an HTML block. There are two reasons for disallowing them here.
  2059. First, it removes the need to parse balanced tags, which is
  2060. expensive and can require backtracking from the end of the document
  2061. if no matching end tag is found. Second, it provides a very simple
  2062. and flexible way of including Markdown content inside HTML tags:
  2063. simply separate the Markdown from the HTML using blank lines:
  2064. Compare:
  2065. ```````````````````````````````` example
  2066. <div>
  2067. *Emphasized* text.
  2068. </div>
  2069. .
  2070. <div>
  2071. <p><em>Emphasized</em> text.</p>
  2072. </div>
  2073. ````````````````````````````````
  2074. ```````````````````````````````` example
  2075. <div>
  2076. *Emphasized* text.
  2077. </div>
  2078. .
  2079. <div>
  2080. *Emphasized* text.
  2081. </div>
  2082. ````````````````````````````````
  2083. Some Markdown implementations have adopted a convention of
  2084. interpreting content inside tags as text if the open tag has
  2085. the attribute `markdown=1`. The rule given above seems a simpler and
  2086. more elegant way of achieving the same expressive power, which is also
  2087. much simpler to parse.
  2088. The main potential drawback is that one can no longer paste HTML
  2089. blocks into Markdown documents with 100% reliability. However,
  2090. *in most cases* this will work fine, because the blank lines in
  2091. HTML are usually followed by HTML block tags. For example:
  2092. ```````````````````````````````` example
  2093. <table>
  2094. <tr>
  2095. <td>
  2096. Hi
  2097. </td>
  2098. </tr>
  2099. </table>
  2100. .
  2101. <table>
  2102. <tr>
  2103. <td>
  2104. Hi
  2105. </td>
  2106. </tr>
  2107. </table>
  2108. ````````````````````````````````
  2109. There are problems, however, if the inner tags are indented
  2110. *and* separated by spaces, as then they will be interpreted as
  2111. an indented code block:
  2112. ```````````````````````````````` example
  2113. <table>
  2114. <tr>
  2115. <td>
  2116. Hi
  2117. </td>
  2118. </tr>
  2119. </table>
  2120. .
  2121. <table>
  2122. <tr>
  2123. <pre><code>&lt;td&gt;
  2124. Hi
  2125. &lt;/td&gt;
  2126. </code></pre>
  2127. </tr>
  2128. </table>
  2129. ````````````````````````````````
  2130. Fortunately, blank lines are usually not necessary and can be
  2131. deleted. The exception is inside `<pre>` tags, but as described
  2132. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2133. *can* contain blank lines.
  2134. ## Link reference definitions
  2135. A [link reference definition](@)
  2136. consists of a [link label], indented up to three spaces, followed
  2137. by a colon (`:`), optional [whitespace] (including up to one
  2138. [line ending]), a [link destination],
  2139. optional [whitespace] (including up to one
  2140. [line ending]), and an optional [link
  2141. title], which if it is present must be separated
  2142. from the [link destination] by [whitespace].
  2143. No further [non-whitespace characters] may occur on the line.
  2144. A [link reference definition]
  2145. does not correspond to a structural element of a document. Instead, it
  2146. defines a label which can be used in [reference links]
  2147. and reference-style [images] elsewhere in the document. [Link
  2148. reference definitions] can come either before or after the links that use
  2149. them.
  2150. ```````````````````````````````` example
  2151. [foo]: /url "title"
  2152. [foo]
  2153. .
  2154. <p><a href="/url" title="title">foo</a></p>
  2155. ````````````````````````````````
  2156. ```````````````````````````````` example
  2157. [foo]:
  2158. /url
  2159. 'the title'
  2160. [foo]
  2161. .
  2162. <p><a href="/url" title="the title">foo</a></p>
  2163. ````````````````````````````````
  2164. ```````````````````````````````` example
  2165. [Foo*bar\]]:my_(url) 'title (with parens)'
  2166. [Foo*bar\]]
  2167. .
  2168. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2169. ````````````````````````````````
  2170. ```````````````````````````````` example
  2171. [Foo bar]:
  2172. <my url>
  2173. 'title'
  2174. [Foo bar]
  2175. .
  2176. <p><a href="my%20url" title="title">Foo bar</a></p>
  2177. ````````````````````````````````
  2178. The title may extend over multiple lines:
  2179. ```````````````````````````````` example
  2180. [foo]: /url '
  2181. title
  2182. line1
  2183. line2
  2184. '
  2185. [foo]
  2186. .
  2187. <p><a href="/url" title="
  2188. title
  2189. line1
  2190. line2
  2191. ">foo</a></p>
  2192. ````````````````````````````````
  2193. However, it may not contain a [blank line]:
  2194. ```````````````````````````````` example
  2195. [foo]: /url 'title
  2196. with blank line'
  2197. [foo]
  2198. .
  2199. <p>[foo]: /url 'title</p>
  2200. <p>with blank line'</p>
  2201. <p>[foo]</p>
  2202. ````````````````````````````````
  2203. The title may be omitted:
  2204. ```````````````````````````````` example
  2205. [foo]:
  2206. /url
  2207. [foo]
  2208. .
  2209. <p><a href="/url">foo</a></p>
  2210. ````````````````````````````````
  2211. The link destination may not be omitted:
  2212. ```````````````````````````````` example
  2213. [foo]:
  2214. [foo]
  2215. .
  2216. <p>[foo]:</p>
  2217. <p>[foo]</p>
  2218. ````````````````````````````````
  2219. The title must be separated from the link destination by
  2220. whitespace:
  2221. ```````````````````````````````` example
  2222. [foo]: <bar>(baz)
  2223. [foo]
  2224. .
  2225. <p>[foo]: <bar>(baz)</p>
  2226. <p>[foo]</p>
  2227. ````````````````````````````````
  2228. Both title and destination can contain backslash escapes
  2229. and literal backslashes:
  2230. ```````````````````````````````` example
  2231. [foo]: /url\bar\*baz "foo\"bar\baz"
  2232. [foo]
  2233. .
  2234. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2235. ````````````````````````````````
  2236. A link can come before its corresponding definition:
  2237. ```````````````````````````````` example
  2238. [foo]
  2239. [foo]: url
  2240. .
  2241. <p><a href="url">foo</a></p>
  2242. ````````````````````````````````
  2243. If there are several matching definitions, the first one takes
  2244. precedence:
  2245. ```````````````````````````````` example
  2246. [foo]
  2247. [foo]: first
  2248. [foo]: second
  2249. .
  2250. <p><a href="first">foo</a></p>
  2251. ````````````````````````````````
  2252. As noted in the section on [Links], matching of labels is
  2253. case-insensitive (see [matches]).
  2254. ```````````````````````````````` example
  2255. [FOO]: /url
  2256. [Foo]
  2257. .
  2258. <p><a href="/url">Foo</a></p>
  2259. ````````````````````````````````
  2260. ```````````````````````````````` example
  2261. [ΑΓΩ]: /φου
  2262. [αγω]
  2263. .
  2264. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2265. ````````````````````````````````
  2266. Here is a link reference definition with no corresponding link.
  2267. It contributes nothing to the document.
  2268. ```````````````````````````````` example
  2269. [foo]: /url
  2270. .
  2271. ````````````````````````````````
  2272. Here is another one:
  2273. ```````````````````````````````` example
  2274. [
  2275. foo
  2276. ]: /url
  2277. bar
  2278. .
  2279. <p>bar</p>
  2280. ````````````````````````````````
  2281. This is not a link reference definition, because there are
  2282. [non-whitespace characters] after the title:
  2283. ```````````````````````````````` example
  2284. [foo]: /url "title" ok
  2285. .
  2286. <p>[foo]: /url &quot;title&quot; ok</p>
  2287. ````````````````````````````````
  2288. This is a link reference definition, but it has no title:
  2289. ```````````````````````````````` example
  2290. [foo]: /url
  2291. "title" ok
  2292. .
  2293. <p>&quot;title&quot; ok</p>
  2294. ````````````````````````````````
  2295. This is not a link reference definition, because it is indented
  2296. four spaces:
  2297. ```````````````````````````````` example
  2298. [foo]: /url "title"
  2299. [foo]
  2300. .
  2301. <pre><code>[foo]: /url &quot;title&quot;
  2302. </code></pre>
  2303. <p>[foo]</p>
  2304. ````````````````````````````````
  2305. This is not a link reference definition, because it occurs inside
  2306. a code block:
  2307. ```````````````````````````````` example
  2308. ```
  2309. [foo]: /url
  2310. ```
  2311. [foo]
  2312. .
  2313. <pre><code>[foo]: /url
  2314. </code></pre>
  2315. <p>[foo]</p>
  2316. ````````````````````````````````
  2317. A [link reference definition] cannot interrupt a paragraph.
  2318. ```````````````````````````````` example
  2319. Foo
  2320. [bar]: /baz
  2321. [bar]
  2322. .
  2323. <p>Foo
  2324. [bar]: /baz</p>
  2325. <p>[bar]</p>
  2326. ````````````````````````````````
  2327. However, it can directly follow other block elements, such as headings
  2328. and thematic breaks, and it need not be followed by a blank line.
  2329. ```````````````````````````````` example
  2330. # [Foo]
  2331. [foo]: /url
  2332. > bar
  2333. .
  2334. <h1><a href="/url">Foo</a></h1>
  2335. <blockquote>
  2336. <p>bar</p>
  2337. </blockquote>
  2338. ````````````````````````````````
  2339. Several [link reference definitions]
  2340. can occur one after another, without intervening blank lines.
  2341. ```````````````````````````````` example
  2342. [foo]: /foo-url "foo"
  2343. [bar]: /bar-url
  2344. "bar"
  2345. [baz]: /baz-url
  2346. [foo],
  2347. [bar],
  2348. [baz]
  2349. .
  2350. <p><a href="/foo-url" title="foo">foo</a>,
  2351. <a href="/bar-url" title="bar">bar</a>,
  2352. <a href="/baz-url">baz</a></p>
  2353. ````````````````````````````````
  2354. [Link reference definitions] can occur
  2355. inside block containers, like lists and block quotations. They
  2356. affect the entire document, not just the container in which they
  2357. are defined:
  2358. ```````````````````````````````` example
  2359. [foo]
  2360. > [foo]: /url
  2361. .
  2362. <p><a href="/url">foo</a></p>
  2363. <blockquote>
  2364. </blockquote>
  2365. ````````````````````````````````
  2366. ## Paragraphs
  2367. A sequence of non-blank lines that cannot be interpreted as other
  2368. kinds of blocks forms a [paragraph](@).
  2369. The contents of the paragraph are the result of parsing the
  2370. paragraph's raw content as inlines. The paragraph's raw content
  2371. is formed by concatenating the lines and removing initial and final
  2372. [whitespace].
  2373. A simple example with two paragraphs:
  2374. ```````````````````````````````` example
  2375. aaa
  2376. bbb
  2377. .
  2378. <p>aaa</p>
  2379. <p>bbb</p>
  2380. ````````````````````````````````
  2381. Paragraphs can contain multiple lines, but no blank lines:
  2382. ```````````````````````````````` example
  2383. aaa
  2384. bbb
  2385. ccc
  2386. ddd
  2387. .
  2388. <p>aaa
  2389. bbb</p>
  2390. <p>ccc
  2391. ddd</p>
  2392. ````````````````````````````````
  2393. Multiple blank lines between paragraph have no effect:
  2394. ```````````````````````````````` example
  2395. aaa
  2396. bbb
  2397. .
  2398. <p>aaa</p>
  2399. <p>bbb</p>
  2400. ````````````````````````````````
  2401. Leading spaces are skipped:
  2402. ```````````````````````````````` example
  2403. aaa
  2404. bbb
  2405. .
  2406. <p>aaa
  2407. bbb</p>
  2408. ````````````````````````````````
  2409. Lines after the first may be indented any amount, since indented
  2410. code blocks cannot interrupt paragraphs.
  2411. ```````````````````````````````` example
  2412. aaa
  2413. bbb
  2414. ccc
  2415. .
  2416. <p>aaa
  2417. bbb
  2418. ccc</p>
  2419. ````````````````````````````````
  2420. However, the first line may be indented at most three spaces,
  2421. or an indented code block will be triggered:
  2422. ```````````````````````````````` example
  2423. aaa
  2424. bbb
  2425. .
  2426. <p>aaa
  2427. bbb</p>
  2428. ````````````````````````````````
  2429. ```````````````````````````````` example
  2430. aaa
  2431. bbb
  2432. .
  2433. <pre><code>aaa
  2434. </code></pre>
  2435. <p>bbb</p>
  2436. ````````````````````````````````
  2437. Final spaces are stripped before inline parsing, so a paragraph
  2438. that ends with two or more spaces will not end with a [hard line
  2439. break]:
  2440. ```````````````````````````````` example
  2441. aaa
  2442. bbb
  2443. .
  2444. <p>aaa<br />
  2445. bbb</p>
  2446. ````````````````````````````````
  2447. ## Blank lines
  2448. [Blank lines] between block-level elements are ignored,
  2449. except for the role they play in determining whether a [list]
  2450. is [tight] or [loose].
  2451. Blank lines at the beginning and end of the document are also ignored.
  2452. ```````````````````````````````` example
  2453. aaa
  2454. # aaa
  2455. .
  2456. <p>aaa</p>
  2457. <h1>aaa</h1>
  2458. ````````````````````````````````
  2459. # Container blocks
  2460. A [container block](#container-blocks) is a block that has other
  2461. blocks as its contents. There are two basic kinds of container blocks:
  2462. [block quotes] and [list items].
  2463. [Lists] are meta-containers for [list items].
  2464. We define the syntax for container blocks recursively. The general
  2465. form of the definition is:
  2466. > If X is a sequence of blocks, then the result of
  2467. > transforming X in such-and-such a way is a container of type Y
  2468. > with these blocks as its content.
  2469. So, we explain what counts as a block quote or list item by explaining
  2470. how these can be *generated* from their contents. This should suffice
  2471. to define the syntax, although it does not give a recipe for *parsing*
  2472. these constructions. (A recipe is provided below in the section entitled
  2473. [A parsing strategy](#appendix-a-parsing-strategy).)
  2474. ## Block quotes
  2475. A [block quote marker](@)
  2476. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2477. with a following space, or (b) a single character `>` not followed by a space.
  2478. The following rules define [block quotes]:
  2479. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2480. of blocks *Bs*, then the result of prepending a [block quote
  2481. marker] to the beginning of each line in *Ls*
  2482. is a [block quote](#block-quotes) containing *Bs*.
  2483. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2484. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2485. the initial [block quote marker] from one or
  2486. more lines in which the next [non-whitespace character] after the [block
  2487. quote marker] is [paragraph continuation
  2488. text] is a block quote with *Bs* as its content.
  2489. [Paragraph continuation text](@) is text
  2490. that will be parsed as part of the content of a paragraph, but does
  2491. not occur at the beginning of the paragraph.
  2492. 3. **Consecutiveness.** A document cannot contain two [block
  2493. quotes] in a row unless there is a [blank line] between them.
  2494. Nothing else counts as a [block quote](#block-quotes).
  2495. Here is a simple example:
  2496. ```````````````````````````````` example
  2497. > # Foo
  2498. > bar
  2499. > baz
  2500. .
  2501. <blockquote>
  2502. <h1>Foo</h1>
  2503. <p>bar
  2504. baz</p>
  2505. </blockquote>
  2506. ````````````````````````````````
  2507. The spaces after the `>` characters can be omitted:
  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 `>` characters can be indented 1-3 spaces:
  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. Four spaces gives us a code block:
  2532. ```````````````````````````````` example
  2533. > # Foo
  2534. > bar
  2535. > baz
  2536. .
  2537. <pre><code>&gt; # Foo
  2538. &gt; bar
  2539. &gt; baz
  2540. </code></pre>
  2541. ````````````````````````````````
  2542. The Laziness clause allows us to omit the `>` before
  2543. [paragraph continuation text]:
  2544. ```````````````````````````````` example
  2545. > # Foo
  2546. > bar
  2547. baz
  2548. .
  2549. <blockquote>
  2550. <h1>Foo</h1>
  2551. <p>bar
  2552. baz</p>
  2553. </blockquote>
  2554. ````````````````````````````````
  2555. A block quote can contain some lazy and some non-lazy
  2556. continuation lines:
  2557. ```````````````````````````````` example
  2558. > bar
  2559. baz
  2560. > foo
  2561. .
  2562. <blockquote>
  2563. <p>bar
  2564. baz
  2565. foo</p>
  2566. </blockquote>
  2567. ````````````````````````````````
  2568. Laziness only applies to lines that would have been continuations of
  2569. paragraphs had they been prepended with [block quote markers].
  2570. For example, the `> ` cannot be omitted in the second line of
  2571. ``` markdown
  2572. > foo
  2573. > ---
  2574. ```
  2575. without changing the meaning:
  2576. ```````````````````````````````` example
  2577. > foo
  2578. ---
  2579. .
  2580. <blockquote>
  2581. <p>foo</p>
  2582. </blockquote>
  2583. <hr />
  2584. ````````````````````````````````
  2585. Similarly, if we omit the `> ` in the second line of
  2586. ``` markdown
  2587. > - foo
  2588. > - bar
  2589. ```
  2590. then the block quote ends after the first line:
  2591. ```````````````````````````````` example
  2592. > - foo
  2593. - bar
  2594. .
  2595. <blockquote>
  2596. <ul>
  2597. <li>foo</li>
  2598. </ul>
  2599. </blockquote>
  2600. <ul>
  2601. <li>bar</li>
  2602. </ul>
  2603. ````````````````````````````````
  2604. For the same reason, we can't omit the `> ` in front of
  2605. subsequent lines of an indented or fenced code block:
  2606. ```````````````````````````````` example
  2607. > foo
  2608. bar
  2609. .
  2610. <blockquote>
  2611. <pre><code>foo
  2612. </code></pre>
  2613. </blockquote>
  2614. <pre><code>bar
  2615. </code></pre>
  2616. ````````````````````````````````
  2617. ```````````````````````````````` example
  2618. > ```
  2619. foo
  2620. ```
  2621. .
  2622. <blockquote>
  2623. <pre><code></code></pre>
  2624. </blockquote>
  2625. <p>foo</p>
  2626. <pre><code></code></pre>
  2627. ````````````````````````````````
  2628. Note that in the following case, we have a [lazy
  2629. continuation line]:
  2630. ```````````````````````````````` example
  2631. > foo
  2632. - bar
  2633. .
  2634. <blockquote>
  2635. <p>foo
  2636. - bar</p>
  2637. </blockquote>
  2638. ````````````````````````````````
  2639. To see why, note that in
  2640. ```markdown
  2641. > foo
  2642. > - bar
  2643. ```
  2644. the `- bar` is indented too far to start a list, and can't
  2645. be an indented code block because indented code blocks cannot
  2646. interrupt paragraphs, so it is [paragraph continuation text].
  2647. A block quote can be empty:
  2648. ```````````````````````````````` example
  2649. >
  2650. .
  2651. <blockquote>
  2652. </blockquote>
  2653. ````````````````````````````````
  2654. ```````````````````````````````` example
  2655. >
  2656. >
  2657. >
  2658. .
  2659. <blockquote>
  2660. </blockquote>
  2661. ````````````````````````````````
  2662. A block quote can have initial or final blank lines:
  2663. ```````````````````````````````` example
  2664. >
  2665. > foo
  2666. >
  2667. .
  2668. <blockquote>
  2669. <p>foo</p>
  2670. </blockquote>
  2671. ````````````````````````````````
  2672. A blank line always separates block quotes:
  2673. ```````````````````````````````` example
  2674. > foo
  2675. > bar
  2676. .
  2677. <blockquote>
  2678. <p>foo</p>
  2679. </blockquote>
  2680. <blockquote>
  2681. <p>bar</p>
  2682. </blockquote>
  2683. ````````````````````````````````
  2684. (Most current Markdown implementations, including John Gruber's
  2685. original `Markdown.pl`, will parse this example as a single block quote
  2686. with two paragraphs. But it seems better to allow the author to decide
  2687. whether two block quotes or one are wanted.)
  2688. Consecutiveness means that if we put these block quotes together,
  2689. we get a single block quote:
  2690. ```````````````````````````````` example
  2691. > foo
  2692. > bar
  2693. .
  2694. <blockquote>
  2695. <p>foo
  2696. bar</p>
  2697. </blockquote>
  2698. ````````````````````````````````
  2699. To get a block quote with two paragraphs, use:
  2700. ```````````````````````````````` example
  2701. > foo
  2702. >
  2703. > bar
  2704. .
  2705. <blockquote>
  2706. <p>foo</p>
  2707. <p>bar</p>
  2708. </blockquote>
  2709. ````````````````````````````````
  2710. Block quotes can interrupt paragraphs:
  2711. ```````````````````````````````` example
  2712. foo
  2713. > bar
  2714. .
  2715. <p>foo</p>
  2716. <blockquote>
  2717. <p>bar</p>
  2718. </blockquote>
  2719. ````````````````````````````````
  2720. In general, blank lines are not needed before or after block
  2721. quotes:
  2722. ```````````````````````````````` example
  2723. > aaa
  2724. ***
  2725. > bbb
  2726. .
  2727. <blockquote>
  2728. <p>aaa</p>
  2729. </blockquote>
  2730. <hr />
  2731. <blockquote>
  2732. <p>bbb</p>
  2733. </blockquote>
  2734. ````````````````````````````````
  2735. However, because of laziness, a blank line is needed between
  2736. a block quote and a following paragraph:
  2737. ```````````````````````````````` example
  2738. > bar
  2739. baz
  2740. .
  2741. <blockquote>
  2742. <p>bar
  2743. baz</p>
  2744. </blockquote>
  2745. ````````````````````````````````
  2746. ```````````````````````````````` example
  2747. > bar
  2748. baz
  2749. .
  2750. <blockquote>
  2751. <p>bar</p>
  2752. </blockquote>
  2753. <p>baz</p>
  2754. ````````````````````````````````
  2755. ```````````````````````````````` example
  2756. > bar
  2757. >
  2758. baz
  2759. .
  2760. <blockquote>
  2761. <p>bar</p>
  2762. </blockquote>
  2763. <p>baz</p>
  2764. ````````````````````````````````
  2765. It is a consequence of the Laziness rule that any number
  2766. of initial `>`s may be omitted on a continuation line of a
  2767. nested block quote:
  2768. ```````````````````````````````` example
  2769. > > > foo
  2770. bar
  2771. .
  2772. <blockquote>
  2773. <blockquote>
  2774. <blockquote>
  2775. <p>foo
  2776. bar</p>
  2777. </blockquote>
  2778. </blockquote>
  2779. </blockquote>
  2780. ````````````````````````````````
  2781. ```````````````````````````````` example
  2782. >>> foo
  2783. > bar
  2784. >>baz
  2785. .
  2786. <blockquote>
  2787. <blockquote>
  2788. <blockquote>
  2789. <p>foo
  2790. bar
  2791. baz</p>
  2792. </blockquote>
  2793. </blockquote>
  2794. </blockquote>
  2795. ````````````````````````````````
  2796. When including an indented code block in a block quote,
  2797. remember that the [block quote marker] includes
  2798. both the `>` and a following space. So *five spaces* are needed after
  2799. the `>`:
  2800. ```````````````````````````````` example
  2801. > code
  2802. > not code
  2803. .
  2804. <blockquote>
  2805. <pre><code>code
  2806. </code></pre>
  2807. </blockquote>
  2808. <blockquote>
  2809. <p>not code</p>
  2810. </blockquote>
  2811. ````````````````````````````````
  2812. ## List items
  2813. A [list marker](@) is a
  2814. [bullet list marker] or an [ordered list marker].
  2815. A [bullet list marker](@)
  2816. is a `-`, `+`, or `*` character.
  2817. An [ordered list marker](@)
  2818. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2819. `.` character or a `)` character. (The reason for the length
  2820. limit is that with 10 digits we start seeing integer overflows
  2821. in some browsers.)
  2822. The following rules define [list items]:
  2823. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2824. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  2825. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2826. of prepending *M* and the following spaces to the first line of
  2827. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2828. list item with *Bs* as its contents. The type of the list item
  2829. (bullet or ordered) is determined by the type of its list marker.
  2830. If the list item is ordered, then it is also assigned a start
  2831. number, based on the ordered list marker.
  2832. Exceptions:
  2833. 1. When the first list item in a [list] interrupts
  2834. a paragraph---that is, when it starts on a line that would
  2835. otherwise count as [paragraph continuation text]---then (a)
  2836. the lines *Ls* must not begin with a blank line, and (b) if
  2837. the list item is ordered, the start number must be 1.
  2838. 2. If any line is a [thematic break][thematic breaks] then
  2839. that line is not a list item.
  2840. For example, let *Ls* be the lines
  2841. ```````````````````````````````` example
  2842. A paragraph
  2843. with two lines.
  2844. indented code
  2845. > A block quote.
  2846. .
  2847. <p>A paragraph
  2848. with two lines.</p>
  2849. <pre><code>indented code
  2850. </code></pre>
  2851. <blockquote>
  2852. <p>A block quote.</p>
  2853. </blockquote>
  2854. ````````````````````````````````
  2855. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2856. that the following is an ordered list item with start number 1,
  2857. and the same contents as *Ls*:
  2858. ```````````````````````````````` example
  2859. 1. A paragraph
  2860. with two lines.
  2861. indented code
  2862. > A block quote.
  2863. .
  2864. <ol>
  2865. <li>
  2866. <p>A paragraph
  2867. with two lines.</p>
  2868. <pre><code>indented code
  2869. </code></pre>
  2870. <blockquote>
  2871. <p>A block quote.</p>
  2872. </blockquote>
  2873. </li>
  2874. </ol>
  2875. ````````````````````````````````
  2876. The most important thing to notice is that the position of
  2877. the text after the list marker determines how much indentation
  2878. is needed in subsequent blocks in the list item. If the list
  2879. marker takes up two spaces, and there are three spaces between
  2880. the list marker and the next [non-whitespace character], then blocks
  2881. must be indented five spaces in order to fall under the list
  2882. item.
  2883. Here are some examples showing how far content must be indented to be
  2884. put under the list item:
  2885. ```````````````````````````````` example
  2886. - one
  2887. two
  2888. .
  2889. <ul>
  2890. <li>one</li>
  2891. </ul>
  2892. <p>two</p>
  2893. ````````````````````````````````
  2894. ```````````````````````````````` example
  2895. - one
  2896. two
  2897. .
  2898. <ul>
  2899. <li>
  2900. <p>one</p>
  2901. <p>two</p>
  2902. </li>
  2903. </ul>
  2904. ````````````````````````````````
  2905. ```````````````````````````````` example
  2906. - one
  2907. two
  2908. .
  2909. <ul>
  2910. <li>one</li>
  2911. </ul>
  2912. <pre><code> two
  2913. </code></pre>
  2914. ````````````````````````````````
  2915. ```````````````````````````````` example
  2916. - one
  2917. two
  2918. .
  2919. <ul>
  2920. <li>
  2921. <p>one</p>
  2922. <p>two</p>
  2923. </li>
  2924. </ul>
  2925. ````````````````````````````````
  2926. It is tempting to think of this in terms of columns: the continuation
  2927. blocks must be indented at least to the column of the first
  2928. [non-whitespace character] after the list marker. However, that is not quite right.
  2929. The spaces after the list marker determine how much relative indentation
  2930. is needed. Which column this indentation reaches will depend on
  2931. how the list item is embedded in other constructions, as shown by
  2932. this example:
  2933. ```````````````````````````````` example
  2934. > > 1. one
  2935. >>
  2936. >> two
  2937. .
  2938. <blockquote>
  2939. <blockquote>
  2940. <ol>
  2941. <li>
  2942. <p>one</p>
  2943. <p>two</p>
  2944. </li>
  2945. </ol>
  2946. </blockquote>
  2947. </blockquote>
  2948. ````````````````````````````````
  2949. Here `two` occurs in the same column as the list marker `1.`,
  2950. but is actually contained in the list item, because there is
  2951. sufficient indentation after the last containing blockquote marker.
  2952. The converse is also possible. In the following example, the word `two`
  2953. occurs far to the right of the initial text of the list item, `one`, but
  2954. it is not considered part of the list item, because it is not indented
  2955. far enough past the blockquote marker:
  2956. ```````````````````````````````` example
  2957. >>- one
  2958. >>
  2959. > > two
  2960. .
  2961. <blockquote>
  2962. <blockquote>
  2963. <ul>
  2964. <li>one</li>
  2965. </ul>
  2966. <p>two</p>
  2967. </blockquote>
  2968. </blockquote>
  2969. ````````````````````````````````
  2970. Note that at least one space is needed between the list marker and
  2971. any following content, so these are not list items:
  2972. ```````````````````````````````` example
  2973. -one
  2974. 2.two
  2975. .
  2976. <p>-one</p>
  2977. <p>2.two</p>
  2978. ````````````````````````````````
  2979. A list item may contain blocks that are separated by more than
  2980. one blank line.
  2981. ```````````````````````````````` example
  2982. - foo
  2983. bar
  2984. .
  2985. <ul>
  2986. <li>
  2987. <p>foo</p>
  2988. <p>bar</p>
  2989. </li>
  2990. </ul>
  2991. ````````````````````````````````
  2992. A list item may contain any kind of block:
  2993. ```````````````````````````````` example
  2994. 1. foo
  2995. ```
  2996. bar
  2997. ```
  2998. baz
  2999. > bam
  3000. .
  3001. <ol>
  3002. <li>
  3003. <p>foo</p>
  3004. <pre><code>bar
  3005. </code></pre>
  3006. <p>baz</p>
  3007. <blockquote>
  3008. <p>bam</p>
  3009. </blockquote>
  3010. </li>
  3011. </ol>
  3012. ````````````````````````````````
  3013. A list item that contains an indented code block will preserve
  3014. empty lines within the code block verbatim.
  3015. ```````````````````````````````` example
  3016. - Foo
  3017. bar
  3018. baz
  3019. .
  3020. <ul>
  3021. <li>
  3022. <p>Foo</p>
  3023. <pre><code>bar
  3024. baz
  3025. </code></pre>
  3026. </li>
  3027. </ul>
  3028. ````````````````````````````````
  3029. Note that ordered list start numbers must be nine digits or less:
  3030. ```````````````````````````````` example
  3031. 123456789. ok
  3032. .
  3033. <ol start="123456789">
  3034. <li>ok</li>
  3035. </ol>
  3036. ````````````````````````````````
  3037. ```````````````````````````````` example
  3038. 1234567890. not ok
  3039. .
  3040. <p>1234567890. not ok</p>
  3041. ````````````````````````````````
  3042. A start number may begin with 0s:
  3043. ```````````````````````````````` example
  3044. 0. ok
  3045. .
  3046. <ol start="0">
  3047. <li>ok</li>
  3048. </ol>
  3049. ````````````````````````````````
  3050. ```````````````````````````````` example
  3051. 003. ok
  3052. .
  3053. <ol start="3">
  3054. <li>ok</li>
  3055. </ol>
  3056. ````````````````````````````````
  3057. A start number may not be negative:
  3058. ```````````````````````````````` example
  3059. -1. not ok
  3060. .
  3061. <p>-1. not ok</p>
  3062. ````````````````````````````````
  3063. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3064. constitute a sequence of blocks *Bs* starting with an indented code
  3065. block, and *M* is a list marker of width *W* followed by
  3066. one space, then the result of prepending *M* and the following
  3067. space to the first line of *Ls*, and indenting subsequent lines of
  3068. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3069. If a line is empty, then it need not be indented. The type of the
  3070. list item (bullet or ordered) is determined by the type of its list
  3071. marker. If the list item is ordered, then it is also assigned a
  3072. start number, based on the ordered list marker.
  3073. An indented code block will have to be indented four spaces beyond
  3074. the edge of the region where text will be included in the list item.
  3075. In the following case that is 6 spaces:
  3076. ```````````````````````````````` example
  3077. - foo
  3078. bar
  3079. .
  3080. <ul>
  3081. <li>
  3082. <p>foo</p>
  3083. <pre><code>bar
  3084. </code></pre>
  3085. </li>
  3086. </ul>
  3087. ````````````````````````````````
  3088. And in this case it is 11 spaces:
  3089. ```````````````````````````````` example
  3090. 10. foo
  3091. bar
  3092. .
  3093. <ol start="10">
  3094. <li>
  3095. <p>foo</p>
  3096. <pre><code>bar
  3097. </code></pre>
  3098. </li>
  3099. </ol>
  3100. ````````````````````````````````
  3101. If the *first* block in the list item is an indented code block,
  3102. then by rule #2, the contents must be indented *one* space after the
  3103. list marker:
  3104. ```````````````````````````````` example
  3105. indented code
  3106. paragraph
  3107. more code
  3108. .
  3109. <pre><code>indented code
  3110. </code></pre>
  3111. <p>paragraph</p>
  3112. <pre><code>more code
  3113. </code></pre>
  3114. ````````````````````````````````
  3115. ```````````````````````````````` example
  3116. 1. indented code
  3117. paragraph
  3118. more code
  3119. .
  3120. <ol>
  3121. <li>
  3122. <pre><code>indented code
  3123. </code></pre>
  3124. <p>paragraph</p>
  3125. <pre><code>more code
  3126. </code></pre>
  3127. </li>
  3128. </ol>
  3129. ````````````````````````````````
  3130. Note that an additional space indent is interpreted as space
  3131. inside the code block:
  3132. ```````````````````````````````` example
  3133. 1. indented code
  3134. paragraph
  3135. more code
  3136. .
  3137. <ol>
  3138. <li>
  3139. <pre><code> indented code
  3140. </code></pre>
  3141. <p>paragraph</p>
  3142. <pre><code>more code
  3143. </code></pre>
  3144. </li>
  3145. </ol>
  3146. ````````````````````````````````
  3147. Note that rules #1 and #2 only apply to two cases: (a) cases
  3148. in which the lines to be included in a list item begin with a
  3149. [non-whitespace character], and (b) cases in which
  3150. they begin with an indented code
  3151. block. In a case like the following, where the first block begins with
  3152. a three-space indent, the rules do not allow us to form a list item by
  3153. indenting the whole thing and prepending a list marker:
  3154. ```````````````````````````````` example
  3155. foo
  3156. bar
  3157. .
  3158. <p>foo</p>
  3159. <p>bar</p>
  3160. ````````````````````````````````
  3161. ```````````````````````````````` example
  3162. - foo
  3163. bar
  3164. .
  3165. <ul>
  3166. <li>foo</li>
  3167. </ul>
  3168. <p>bar</p>
  3169. ````````````````````````````````
  3170. This is not a significant restriction, because when a block begins
  3171. with 1-3 spaces indent, the indentation can always be removed without
  3172. a change in interpretation, allowing rule #1 to be applied. So, in
  3173. the above case:
  3174. ```````````````````````````````` example
  3175. - foo
  3176. bar
  3177. .
  3178. <ul>
  3179. <li>
  3180. <p>foo</p>
  3181. <p>bar</p>
  3182. </li>
  3183. </ul>
  3184. ````````````````````````````````
  3185. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3186. starting with a single [blank line] constitute a (possibly empty)
  3187. sequence of blocks *Bs*, not separated from each other by more than
  3188. one blank line, and *M* is a list marker of width *W*,
  3189. then the result of prepending *M* to the first line of *Ls*, and
  3190. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3191. item with *Bs* as its contents.
  3192. If a line is empty, then it need not be indented. The type of the
  3193. list item (bullet or ordered) is determined by the type of its list
  3194. marker. If the list item is ordered, then it is also assigned a
  3195. start number, based on the ordered list marker.
  3196. Here are some list items that start with a blank line but are not empty:
  3197. ```````````````````````````````` example
  3198. -
  3199. foo
  3200. -
  3201. ```
  3202. bar
  3203. ```
  3204. -
  3205. baz
  3206. .
  3207. <ul>
  3208. <li>foo</li>
  3209. <li>
  3210. <pre><code>bar
  3211. </code></pre>
  3212. </li>
  3213. <li>
  3214. <pre><code>baz
  3215. </code></pre>
  3216. </li>
  3217. </ul>
  3218. ````````````````````````````````
  3219. When the list item starts with a blank line, the number of spaces
  3220. following the list marker doesn't change the required indentation:
  3221. ```````````````````````````````` example
  3222. -
  3223. foo
  3224. .
  3225. <ul>
  3226. <li>foo</li>
  3227. </ul>
  3228. ````````````````````````````````
  3229. A list item can begin with at most one blank line.
  3230. In the following example, `foo` is not part of the list
  3231. item:
  3232. ```````````````````````````````` example
  3233. -
  3234. foo
  3235. .
  3236. <ul>
  3237. <li></li>
  3238. </ul>
  3239. <p>foo</p>
  3240. ````````````````````````````````
  3241. Here is an empty bullet list item:
  3242. ```````````````````````````````` example
  3243. - foo
  3244. -
  3245. - bar
  3246. .
  3247. <ul>
  3248. <li>foo</li>
  3249. <li></li>
  3250. <li>bar</li>
  3251. </ul>
  3252. ````````````````````````````````
  3253. It does not matter whether there are spaces following the [list marker]:
  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. Here is an empty ordered list item:
  3266. ```````````````````````````````` example
  3267. 1. foo
  3268. 2.
  3269. 3. bar
  3270. .
  3271. <ol>
  3272. <li>foo</li>
  3273. <li></li>
  3274. <li>bar</li>
  3275. </ol>
  3276. ````````````````````````````````
  3277. A list may start or end with an empty list item:
  3278. ```````````````````````````````` example
  3279. *
  3280. .
  3281. <ul>
  3282. <li></li>
  3283. </ul>
  3284. ````````````````````````````````
  3285. However, an empty list item cannot interrupt a paragraph:
  3286. ```````````````````````````````` example
  3287. foo
  3288. *
  3289. foo
  3290. 1.
  3291. .
  3292. <p>foo
  3293. *</p>
  3294. <p>foo
  3295. 1.</p>
  3296. ````````````````````````````````
  3297. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3298. according to rule #1, #2, or #3, then the result of indenting each line
  3299. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3300. list item with the same contents and attributes. If a line is
  3301. empty, then it need not be indented.
  3302. Indented one space:
  3303. ```````````````````````````````` example
  3304. 1. A paragraph
  3305. with two lines.
  3306. indented code
  3307. > A block quote.
  3308. .
  3309. <ol>
  3310. <li>
  3311. <p>A paragraph
  3312. with two lines.</p>
  3313. <pre><code>indented code
  3314. </code></pre>
  3315. <blockquote>
  3316. <p>A block quote.</p>
  3317. </blockquote>
  3318. </li>
  3319. </ol>
  3320. ````````````````````````````````
  3321. Indented two spaces:
  3322. ```````````````````````````````` example
  3323. 1. A paragraph
  3324. with two lines.
  3325. indented code
  3326. > A block quote.
  3327. .
  3328. <ol>
  3329. <li>
  3330. <p>A paragraph
  3331. with two lines.</p>
  3332. <pre><code>indented code
  3333. </code></pre>
  3334. <blockquote>
  3335. <p>A block quote.</p>
  3336. </blockquote>
  3337. </li>
  3338. </ol>
  3339. ````````````````````````````````
  3340. Indented three spaces:
  3341. ```````````````````````````````` example
  3342. 1. A paragraph
  3343. with two lines.
  3344. indented code
  3345. > A block quote.
  3346. .
  3347. <ol>
  3348. <li>
  3349. <p>A paragraph
  3350. with two lines.</p>
  3351. <pre><code>indented code
  3352. </code></pre>
  3353. <blockquote>
  3354. <p>A block quote.</p>
  3355. </blockquote>
  3356. </li>
  3357. </ol>
  3358. ````````````````````````````````
  3359. Four spaces indent gives a code block:
  3360. ```````````````````````````````` example
  3361. 1. A paragraph
  3362. with two lines.
  3363. indented code
  3364. > A block quote.
  3365. .
  3366. <pre><code>1. A paragraph
  3367. with two lines.
  3368. indented code
  3369. &gt; A block quote.
  3370. </code></pre>
  3371. ````````````````````````````````
  3372. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3373. item](#list-items) with contents *Bs*, then the result of deleting
  3374. some or all of the indentation from one or more lines in which the
  3375. next [non-whitespace character] after the indentation is
  3376. [paragraph continuation text] is a
  3377. list item with the same contents and attributes. The unindented
  3378. lines are called
  3379. [lazy continuation line](@)s.
  3380. Here is an example with [lazy continuation lines]:
  3381. ```````````````````````````````` example
  3382. 1. A paragraph
  3383. with two lines.
  3384. indented code
  3385. > A block quote.
  3386. .
  3387. <ol>
  3388. <li>
  3389. <p>A paragraph
  3390. with two lines.</p>
  3391. <pre><code>indented code
  3392. </code></pre>
  3393. <blockquote>
  3394. <p>A block quote.</p>
  3395. </blockquote>
  3396. </li>
  3397. </ol>
  3398. ````````````````````````````````
  3399. Indentation can be partially deleted:
  3400. ```````````````````````````````` example
  3401. 1. A paragraph
  3402. with two lines.
  3403. .
  3404. <ol>
  3405. <li>A paragraph
  3406. with two lines.</li>
  3407. </ol>
  3408. ````````````````````````````````
  3409. These examples show how laziness can work in nested structures:
  3410. ```````````````````````````````` example
  3411. > 1. > Blockquote
  3412. continued here.
  3413. .
  3414. <blockquote>
  3415. <ol>
  3416. <li>
  3417. <blockquote>
  3418. <p>Blockquote
  3419. continued here.</p>
  3420. </blockquote>
  3421. </li>
  3422. </ol>
  3423. </blockquote>
  3424. ````````````````````````````````
  3425. ```````````````````````````````` example
  3426. > 1. > Blockquote
  3427. > continued here.
  3428. .
  3429. <blockquote>
  3430. <ol>
  3431. <li>
  3432. <blockquote>
  3433. <p>Blockquote
  3434. continued here.</p>
  3435. </blockquote>
  3436. </li>
  3437. </ol>
  3438. </blockquote>
  3439. ````````````````````````````````
  3440. 6. **That's all.** Nothing that is not counted as a list item by rules
  3441. #1--5 counts as a [list item](#list-items).
  3442. The rules for sublists follow from the general rules
  3443. [above][List items]. A sublist must be indented the same number
  3444. of spaces a paragraph would need to be in order to be included
  3445. in the list item.
  3446. So, in this case we need two spaces indent:
  3447. ```````````````````````````````` example
  3448. - foo
  3449. - bar
  3450. - baz
  3451. - boo
  3452. .
  3453. <ul>
  3454. <li>foo
  3455. <ul>
  3456. <li>bar
  3457. <ul>
  3458. <li>baz
  3459. <ul>
  3460. <li>boo</li>
  3461. </ul>
  3462. </li>
  3463. </ul>
  3464. </li>
  3465. </ul>
  3466. </li>
  3467. </ul>
  3468. ````````````````````````````````
  3469. One is not enough:
  3470. ```````````````````````````````` example
  3471. - foo
  3472. - bar
  3473. - baz
  3474. - boo
  3475. .
  3476. <ul>
  3477. <li>foo</li>
  3478. <li>bar</li>
  3479. <li>baz</li>
  3480. <li>boo</li>
  3481. </ul>
  3482. ````````````````````````````````
  3483. Here we need four, because the list marker is wider:
  3484. ```````````````````````````````` example
  3485. 10) foo
  3486. - bar
  3487. .
  3488. <ol start="10">
  3489. <li>foo
  3490. <ul>
  3491. <li>bar</li>
  3492. </ul>
  3493. </li>
  3494. </ol>
  3495. ````````````````````````````````
  3496. Three is not enough:
  3497. ```````````````````````````````` example
  3498. 10) foo
  3499. - bar
  3500. .
  3501. <ol start="10">
  3502. <li>foo</li>
  3503. </ol>
  3504. <ul>
  3505. <li>bar</li>
  3506. </ul>
  3507. ````````````````````````````````
  3508. A list may be the first block in a list item:
  3509. ```````````````````````````````` example
  3510. - - foo
  3511. .
  3512. <ul>
  3513. <li>
  3514. <ul>
  3515. <li>foo</li>
  3516. </ul>
  3517. </li>
  3518. </ul>
  3519. ````````````````````````````````
  3520. ```````````````````````````````` example
  3521. 1. - 2. foo
  3522. .
  3523. <ol>
  3524. <li>
  3525. <ul>
  3526. <li>
  3527. <ol start="2">
  3528. <li>foo</li>
  3529. </ol>
  3530. </li>
  3531. </ul>
  3532. </li>
  3533. </ol>
  3534. ````````````````````````````````
  3535. A list item can contain a heading:
  3536. ```````````````````````````````` example
  3537. - # Foo
  3538. - Bar
  3539. ---
  3540. baz
  3541. .
  3542. <ul>
  3543. <li>
  3544. <h1>Foo</h1>
  3545. </li>
  3546. <li>
  3547. <h2>Bar</h2>
  3548. baz</li>
  3549. </ul>
  3550. ````````````````````````````````
  3551. ### Motivation
  3552. John Gruber's Markdown spec says the following about list items:
  3553. 1. "List markers typically start at the left margin, but may be indented
  3554. by up to three spaces. List markers must be followed by one or more
  3555. spaces or a tab."
  3556. 2. "To make lists look nice, you can wrap items with hanging indents....
  3557. But if you don't want to, you don't have to."
  3558. 3. "List items may consist of multiple paragraphs. Each subsequent
  3559. paragraph in a list item must be indented by either 4 spaces or one
  3560. tab."
  3561. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3562. but here again, Markdown will allow you to be lazy."
  3563. 5. "To put a blockquote within a list item, the blockquote's `>`
  3564. delimiters need to be indented."
  3565. 6. "To put a code block within a list item, the code block needs to be
  3566. indented twice — 8 spaces or two tabs."
  3567. These rules specify that a paragraph under a list item must be indented
  3568. four spaces (presumably, from the left margin, rather than the start of
  3569. the list marker, but this is not said), and that code under a list item
  3570. must be indented eight spaces instead of the usual four. They also say
  3571. that a block quote must be indented, but not by how much; however, the
  3572. example given has four spaces indentation. Although nothing is said
  3573. about other kinds of block-level content, it is certainly reasonable to
  3574. infer that *all* block elements under a list item, including other
  3575. lists, must be indented four spaces. This principle has been called the
  3576. *four-space rule*.
  3577. The four-space rule is clear and principled, and if the reference
  3578. implementation `Markdown.pl` had followed it, it probably would have
  3579. become the standard. However, `Markdown.pl` allowed paragraphs and
  3580. sublists to start with only two spaces indentation, at least on the
  3581. outer level. Worse, its behavior was inconsistent: a sublist of an
  3582. outer-level list needed two spaces indentation, but a sublist of this
  3583. sublist needed three spaces. It is not surprising, then, that different
  3584. implementations of Markdown have developed very different rules for
  3585. determining what comes under a list item. (Pandoc and python-Markdown,
  3586. for example, stuck with Gruber's syntax description and the four-space
  3587. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3588. followed `Markdown.pl`'s behavior more closely.)
  3589. Unfortunately, given the divergences between implementations, there
  3590. is no way to give a spec for list items that will be guaranteed not
  3591. to break any existing documents. However, the spec given here should
  3592. correctly handle lists formatted with either the four-space rule or
  3593. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3594. in a way that is natural for a human to read.
  3595. The strategy here is to let the width and indentation of the list marker
  3596. determine the indentation necessary for blocks to fall under the list
  3597. item, rather than having a fixed and arbitrary number. The writer can
  3598. think of the body of the list item as a unit which gets indented to the
  3599. right enough to fit the list marker (and any indentation on the list
  3600. marker). (The laziness rule, #5, then allows continuation lines to be
  3601. unindented if needed.)
  3602. This rule is superior, we claim, to any rule requiring a fixed level of
  3603. indentation from the margin. The four-space rule is clear but
  3604. unnatural. It is quite unintuitive that
  3605. ``` markdown
  3606. - foo
  3607. bar
  3608. - baz
  3609. ```
  3610. should be parsed as two lists with an intervening paragraph,
  3611. ``` html
  3612. <ul>
  3613. <li>foo</li>
  3614. </ul>
  3615. <p>bar</p>
  3616. <ul>
  3617. <li>baz</li>
  3618. </ul>
  3619. ```
  3620. as the four-space rule demands, rather than a single list,
  3621. ``` html
  3622. <ul>
  3623. <li>
  3624. <p>foo</p>
  3625. <p>bar</p>
  3626. <ul>
  3627. <li>baz</li>
  3628. </ul>
  3629. </li>
  3630. </ul>
  3631. ```
  3632. The choice of four spaces is arbitrary. It can be learned, but it is
  3633. not likely to be guessed, and it trips up beginners regularly.
  3634. Would it help to adopt a two-space rule? The problem is that such
  3635. a rule, together with the rule allowing 1--3 spaces indentation of the
  3636. initial list marker, allows text that is indented *less than* the
  3637. original list marker to be included in the list item. For example,
  3638. `Markdown.pl` parses
  3639. ``` markdown
  3640. - one
  3641. two
  3642. ```
  3643. as a single list item, with `two` a continuation paragraph:
  3644. ``` html
  3645. <ul>
  3646. <li>
  3647. <p>one</p>
  3648. <p>two</p>
  3649. </li>
  3650. </ul>
  3651. ```
  3652. and similarly
  3653. ``` markdown
  3654. > - one
  3655. >
  3656. > two
  3657. ```
  3658. as
  3659. ``` html
  3660. <blockquote>
  3661. <ul>
  3662. <li>
  3663. <p>one</p>
  3664. <p>two</p>
  3665. </li>
  3666. </ul>
  3667. </blockquote>
  3668. ```
  3669. This is extremely unintuitive.
  3670. Rather than requiring a fixed indent from the margin, we could require
  3671. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3672. may itself be indented). This proposal would remove the last anomaly
  3673. discussed. Unlike the spec presented above, it would count the following
  3674. as a list item with a subparagraph, even though the paragraph `bar`
  3675. is not indented as far as the first paragraph `foo`:
  3676. ``` markdown
  3677. 10. foo
  3678. bar
  3679. ```
  3680. Arguably this text does read like a list item with `bar` as a subparagraph,
  3681. which may count in favor of the proposal. However, on this proposal indented
  3682. code would have to be indented six spaces after the list marker. And this
  3683. would break a lot of existing Markdown, which has the pattern:
  3684. ``` markdown
  3685. 1. foo
  3686. indented code
  3687. ```
  3688. where the code is indented eight spaces. The spec above, by contrast, will
  3689. parse this text as expected, since the code block's indentation is measured
  3690. from the beginning of `foo`.
  3691. The one case that needs special treatment is a list item that *starts*
  3692. with indented code. How much indentation is required in that case, since
  3693. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3694. that in such cases, we require one space indentation from the list marker
  3695. (and then the normal four spaces for the indented code). This will match the
  3696. four-space rule in cases where the list marker plus its initial indentation
  3697. takes four spaces (a common case), but diverge in other cases.
  3698. ## Lists
  3699. A [list](@) is a sequence of one or more
  3700. list items [of the same type]. The list items
  3701. may be separated by any number of blank lines.
  3702. Two list items are [of the same type](@)
  3703. if they begin with a [list marker] of the same type.
  3704. Two list markers are of the
  3705. same type if (a) they are bullet list markers using the same character
  3706. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3707. delimiter (either `.` or `)`).
  3708. A list is an [ordered list](@)
  3709. if its constituent list items begin with
  3710. [ordered list markers], and a
  3711. [bullet list](@) if its constituent list
  3712. items begin with [bullet list markers].
  3713. The [start number](@)
  3714. of an [ordered list] is determined by the list number of
  3715. its initial list item. The numbers of subsequent list items are
  3716. disregarded.
  3717. A list is [loose](@) if any of its constituent
  3718. list items are separated by blank lines, or if any of its constituent
  3719. list items directly contain two block-level elements with a blank line
  3720. between them. Otherwise a list is [tight](@).
  3721. (The difference in HTML output is that paragraphs in a loose list are
  3722. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3723. Changing the bullet or ordered list delimiter starts a new list:
  3724. ```````````````````````````````` example
  3725. - foo
  3726. - bar
  3727. + baz
  3728. .
  3729. <ul>
  3730. <li>foo</li>
  3731. <li>bar</li>
  3732. </ul>
  3733. <ul>
  3734. <li>baz</li>
  3735. </ul>
  3736. ````````````````````````````````
  3737. ```````````````````````````````` example
  3738. 1. foo
  3739. 2. bar
  3740. 3) baz
  3741. .
  3742. <ol>
  3743. <li>foo</li>
  3744. <li>bar</li>
  3745. </ol>
  3746. <ol start="3">
  3747. <li>baz</li>
  3748. </ol>
  3749. ````````````````````````````````
  3750. In CommonMark, a list can interrupt a paragraph. That is,
  3751. no blank line is needed to separate a paragraph from a following
  3752. list:
  3753. ```````````````````````````````` example
  3754. Foo
  3755. - bar
  3756. - baz
  3757. .
  3758. <p>Foo</p>
  3759. <ul>
  3760. <li>bar</li>
  3761. <li>baz</li>
  3762. </ul>
  3763. ````````````````````````````````
  3764. `Markdown.pl` does not allow this, through fear of triggering a list
  3765. via a numeral in a hard-wrapped line:
  3766. ``` markdown
  3767. The number of windows in my house is
  3768. 14. The number of doors is 6.
  3769. ```
  3770. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3771. interrupt a paragraph, even though the same considerations might
  3772. apply.
  3773. In CommonMark, we do allow lists to interrupt paragraphs, for
  3774. two reasons. First, it is natural and not uncommon for people
  3775. to start lists without blank lines:
  3776. ``` markdown
  3777. I need to buy
  3778. - new shoes
  3779. - a coat
  3780. - a plane ticket
  3781. ```
  3782. Second, we are attracted to a
  3783. > [principle of uniformity](@):
  3784. > if a chunk of text has a certain
  3785. > meaning, it will continue to have the same meaning when put into a
  3786. > container block (such as a list item or blockquote).
  3787. (Indeed, the spec for [list items] and [block quotes] presupposes
  3788. this principle.) This principle implies that if
  3789. ``` markdown
  3790. * I need to buy
  3791. - new shoes
  3792. - a coat
  3793. - a plane ticket
  3794. ```
  3795. is a list item containing a paragraph followed by a nested sublist,
  3796. as all Markdown implementations agree it is (though the paragraph
  3797. may be rendered without `<p>` tags, since the list is "tight"),
  3798. then
  3799. ``` markdown
  3800. I need to buy
  3801. - new shoes
  3802. - a coat
  3803. - a plane ticket
  3804. ```
  3805. by itself should be a paragraph followed by a nested sublist.
  3806. Since it is well established Markdown practice to allow lists to
  3807. interrupt paragraphs inside list items, the [principle of
  3808. uniformity] requires us to allow this outside list items as
  3809. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3810. takes a different approach, requiring blank lines before lists
  3811. even inside other list items.)
  3812. In order to solve of unwanted lists in paragraphs with
  3813. hard-wrapped numerals, we allow only lists starting with `1` to
  3814. interrupt paragraphs. Thus,
  3815. ```````````````````````````````` example
  3816. The number of windows in my house is
  3817. 14. The number of doors is 6.
  3818. .
  3819. <p>The number of windows in my house is
  3820. 14. The number of doors is 6.</p>
  3821. ````````````````````````````````
  3822. We may still get an unintended result in cases like
  3823. ```````````````````````````````` example
  3824. The number of windows in my house is
  3825. 1. The number of doors is 6.
  3826. .
  3827. <p>The number of windows in my house is</p>
  3828. <ol>
  3829. <li>The number of doors is 6.</li>
  3830. </ol>
  3831. ````````````````````````````````
  3832. but this rule should prevent most spurious list captures.
  3833. There can be any number of blank lines between items:
  3834. ```````````````````````````````` example
  3835. - foo
  3836. - bar
  3837. - baz
  3838. .
  3839. <ul>
  3840. <li>
  3841. <p>foo</p>
  3842. </li>
  3843. <li>
  3844. <p>bar</p>
  3845. </li>
  3846. <li>
  3847. <p>baz</p>
  3848. </li>
  3849. </ul>
  3850. ````````````````````````````````
  3851. ```````````````````````````````` example
  3852. - foo
  3853. - bar
  3854. - baz
  3855. bim
  3856. .
  3857. <ul>
  3858. <li>foo
  3859. <ul>
  3860. <li>bar
  3861. <ul>
  3862. <li>
  3863. <p>baz</p>
  3864. <p>bim</p>
  3865. </li>
  3866. </ul>
  3867. </li>
  3868. </ul>
  3869. </li>
  3870. </ul>
  3871. ````````````````````````````````
  3872. To separate consecutive lists of the same type, or to separate a
  3873. list from an indented code block that would otherwise be parsed
  3874. as a subparagraph of the final list item, you can insert a blank HTML
  3875. comment:
  3876. ```````````````````````````````` example
  3877. - foo
  3878. - bar
  3879. <!-- -->
  3880. - baz
  3881. - bim
  3882. .
  3883. <ul>
  3884. <li>foo</li>
  3885. <li>bar</li>
  3886. </ul>
  3887. <!-- -->
  3888. <ul>
  3889. <li>baz</li>
  3890. <li>bim</li>
  3891. </ul>
  3892. ````````````````````````````````
  3893. ```````````````````````````````` example
  3894. - foo
  3895. notcode
  3896. - foo
  3897. <!-- -->
  3898. code
  3899. .
  3900. <ul>
  3901. <li>
  3902. <p>foo</p>
  3903. <p>notcode</p>
  3904. </li>
  3905. <li>
  3906. <p>foo</p>
  3907. </li>
  3908. </ul>
  3909. <!-- -->
  3910. <pre><code>code
  3911. </code></pre>
  3912. ````````````````````````````````
  3913. List items need not be indented to the same level. The following
  3914. list items will be treated as items at the same list level,
  3915. since none is indented enough to belong to the previous list
  3916. item:
  3917. ```````````````````````````````` example
  3918. - a
  3919. - b
  3920. - c
  3921. - d
  3922. - e
  3923. - f
  3924. - g
  3925. .
  3926. <ul>
  3927. <li>a</li>
  3928. <li>b</li>
  3929. <li>c</li>
  3930. <li>d</li>
  3931. <li>e</li>
  3932. <li>f</li>
  3933. <li>g</li>
  3934. </ul>
  3935. ````````````````````````````````
  3936. ```````````````````````````````` example
  3937. 1. a
  3938. 2. b
  3939. 3. c
  3940. .
  3941. <ol>
  3942. <li>
  3943. <p>a</p>
  3944. </li>
  3945. <li>
  3946. <p>b</p>
  3947. </li>
  3948. <li>
  3949. <p>c</p>
  3950. </li>
  3951. </ol>
  3952. ````````````````````````````````
  3953. Note, however, that list items may not be indented more than
  3954. three spaces. Here `- e` is treated as a paragraph continuation
  3955. line, because it is indented more than three spaces:
  3956. ```````````````````````````````` example
  3957. - a
  3958. - b
  3959. - c
  3960. - d
  3961. - e
  3962. .
  3963. <ul>
  3964. <li>a</li>
  3965. <li>b</li>
  3966. <li>c</li>
  3967. <li>d
  3968. - e</li>
  3969. </ul>
  3970. ````````````````````````````````
  3971. And here, `3. c` is treated as in indented code block,
  3972. because it is indented four spaces and preceded by a
  3973. blank line.
  3974. ```````````````````````````````` example
  3975. 1. a
  3976. 2. b
  3977. 3. c
  3978. .
  3979. <ol>
  3980. <li>
  3981. <p>a</p>
  3982. </li>
  3983. <li>
  3984. <p>b</p>
  3985. </li>
  3986. </ol>
  3987. <pre><code>3. c
  3988. </code></pre>
  3989. ````````````````````````````````
  3990. This is a loose list, because there is a blank line between
  3991. two of the list items:
  3992. ```````````````````````````````` example
  3993. - a
  3994. - b
  3995. - c
  3996. .
  3997. <ul>
  3998. <li>
  3999. <p>a</p>
  4000. </li>
  4001. <li>
  4002. <p>b</p>
  4003. </li>
  4004. <li>
  4005. <p>c</p>
  4006. </li>
  4007. </ul>
  4008. ````````````````````````````````
  4009. So is this, with a empty second item:
  4010. ```````````````````````````````` example
  4011. * a
  4012. *
  4013. * c
  4014. .
  4015. <ul>
  4016. <li>
  4017. <p>a</p>
  4018. </li>
  4019. <li></li>
  4020. <li>
  4021. <p>c</p>
  4022. </li>
  4023. </ul>
  4024. ````````````````````````````````
  4025. These are loose lists, even though there is no space between the items,
  4026. because one of the items directly contains two block-level elements
  4027. with a blank line between them:
  4028. ```````````````````````````````` example
  4029. - a
  4030. - b
  4031. c
  4032. - d
  4033. .
  4034. <ul>
  4035. <li>
  4036. <p>a</p>
  4037. </li>
  4038. <li>
  4039. <p>b</p>
  4040. <p>c</p>
  4041. </li>
  4042. <li>
  4043. <p>d</p>
  4044. </li>
  4045. </ul>
  4046. ````````````````````````````````
  4047. ```````````````````````````````` example
  4048. - a
  4049. - b
  4050. [ref]: /url
  4051. - d
  4052. .
  4053. <ul>
  4054. <li>
  4055. <p>a</p>
  4056. </li>
  4057. <li>
  4058. <p>b</p>
  4059. </li>
  4060. <li>
  4061. <p>d</p>
  4062. </li>
  4063. </ul>
  4064. ````````````````````````````````
  4065. This is a tight list, because the blank lines are in a code block:
  4066. ```````````````````````````````` example
  4067. - a
  4068. - ```
  4069. b
  4070. ```
  4071. - c
  4072. .
  4073. <ul>
  4074. <li>a</li>
  4075. <li>
  4076. <pre><code>b
  4077. </code></pre>
  4078. </li>
  4079. <li>c</li>
  4080. </ul>
  4081. ````````````````````````````````
  4082. This is a tight list, because the blank line is between two
  4083. paragraphs of a sublist. So the sublist is loose while
  4084. the outer list is tight:
  4085. ```````````````````````````````` example
  4086. - a
  4087. - b
  4088. c
  4089. - d
  4090. .
  4091. <ul>
  4092. <li>a
  4093. <ul>
  4094. <li>
  4095. <p>b</p>
  4096. <p>c</p>
  4097. </li>
  4098. </ul>
  4099. </li>
  4100. <li>d</li>
  4101. </ul>
  4102. ````````````````````````````````
  4103. This is a tight list, because the blank line is inside the
  4104. block quote:
  4105. ```````````````````````````````` example
  4106. * a
  4107. > b
  4108. >
  4109. * c
  4110. .
  4111. <ul>
  4112. <li>a
  4113. <blockquote>
  4114. <p>b</p>
  4115. </blockquote>
  4116. </li>
  4117. <li>c</li>
  4118. </ul>
  4119. ````````````````````````````````
  4120. This list is tight, because the consecutive block elements
  4121. are not separated by blank lines:
  4122. ```````````````````````````````` example
  4123. - a
  4124. > b
  4125. ```
  4126. c
  4127. ```
  4128. - d
  4129. .
  4130. <ul>
  4131. <li>a
  4132. <blockquote>
  4133. <p>b</p>
  4134. </blockquote>
  4135. <pre><code>c
  4136. </code></pre>
  4137. </li>
  4138. <li>d</li>
  4139. </ul>
  4140. ````````````````````````````````
  4141. A single-paragraph list is tight:
  4142. ```````````````````````````````` example
  4143. - a
  4144. .
  4145. <ul>
  4146. <li>a</li>
  4147. </ul>
  4148. ````````````````````````````````
  4149. ```````````````````````````````` example
  4150. - a
  4151. - b
  4152. .
  4153. <ul>
  4154. <li>a
  4155. <ul>
  4156. <li>b</li>
  4157. </ul>
  4158. </li>
  4159. </ul>
  4160. ````````````````````````````````
  4161. This list is loose, because of the blank line between the
  4162. two block elements in the list item:
  4163. ```````````````````````````````` example
  4164. 1. ```
  4165. foo
  4166. ```
  4167. bar
  4168. .
  4169. <ol>
  4170. <li>
  4171. <pre><code>foo
  4172. </code></pre>
  4173. <p>bar</p>
  4174. </li>
  4175. </ol>
  4176. ````````````````````````````````
  4177. Here the outer list is loose, the inner list tight:
  4178. ```````````````````````````````` example
  4179. * foo
  4180. * bar
  4181. baz
  4182. .
  4183. <ul>
  4184. <li>
  4185. <p>foo</p>
  4186. <ul>
  4187. <li>bar</li>
  4188. </ul>
  4189. <p>baz</p>
  4190. </li>
  4191. </ul>
  4192. ````````````````````````````````
  4193. ```````````````````````````````` example
  4194. - a
  4195. - b
  4196. - c
  4197. - d
  4198. - e
  4199. - f
  4200. .
  4201. <ul>
  4202. <li>
  4203. <p>a</p>
  4204. <ul>
  4205. <li>b</li>
  4206. <li>c</li>
  4207. </ul>
  4208. </li>
  4209. <li>
  4210. <p>d</p>
  4211. <ul>
  4212. <li>e</li>
  4213. <li>f</li>
  4214. </ul>
  4215. </li>
  4216. </ul>
  4217. ````````````````````````````````
  4218. # Inlines
  4219. Inlines are parsed sequentially from the beginning of the character
  4220. stream to the end (left to right, in left-to-right languages).
  4221. Thus, for example, in
  4222. ```````````````````````````````` example
  4223. `hi`lo`
  4224. .
  4225. <p><code>hi</code>lo`</p>
  4226. ````````````````````````````````
  4227. `hi` is parsed as code, leaving the backtick at the end as a literal
  4228. backtick.
  4229. ## Backslash escapes
  4230. Any ASCII punctuation character may be backslash-escaped:
  4231. ```````````````````````````````` example
  4232. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4233. .
  4234. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4235. ````````````````````````````````
  4236. Backslashes before other characters are treated as literal
  4237. backslashes:
  4238. ```````````````````````````````` example
  4239. \→\A\a\ \3\φ\«
  4240. .
  4241. <p>\→\A\a\ \3\φ\«</p>
  4242. ````````````````````````````````
  4243. Escaped characters are treated as regular characters and do
  4244. not have their usual Markdown meanings:
  4245. ```````````````````````````````` example
  4246. \*not emphasized*
  4247. \<br/> not a tag
  4248. \[not a link](/foo)
  4249. \`not code`
  4250. 1\. not a list
  4251. \* not a list
  4252. \# not a heading
  4253. \[foo]: /url "not a reference"
  4254. .
  4255. <p>*not emphasized*
  4256. &lt;br/&gt; not a tag
  4257. [not a link](/foo)
  4258. `not code`
  4259. 1. not a list
  4260. * not a list
  4261. # not a heading
  4262. [foo]: /url &quot;not a reference&quot;</p>
  4263. ````````````````````````````````
  4264. If a backslash is itself escaped, the following character is not:
  4265. ```````````````````````````````` example
  4266. \\*emphasis*
  4267. .
  4268. <p>\<em>emphasis</em></p>
  4269. ````````````````````````````````
  4270. A backslash at the end of the line is a [hard line break]:
  4271. ```````````````````````````````` example
  4272. foo\
  4273. bar
  4274. .
  4275. <p>foo<br />
  4276. bar</p>
  4277. ````````````````````````````````
  4278. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4279. raw HTML:
  4280. ```````````````````````````````` example
  4281. `` \[\` ``
  4282. .
  4283. <p><code>\[\`</code></p>
  4284. ````````````````````````````````
  4285. ```````````````````````````````` example
  4286. \[\]
  4287. .
  4288. <pre><code>\[\]
  4289. </code></pre>
  4290. ````````````````````````````````
  4291. ```````````````````````````````` example
  4292. ~~~
  4293. \[\]
  4294. ~~~
  4295. .
  4296. <pre><code>\[\]
  4297. </code></pre>
  4298. ````````````````````````````````
  4299. ```````````````````````````````` example
  4300. <http://example.com?find=\*>
  4301. .
  4302. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4303. ````````````````````````````````
  4304. ```````````````````````````````` example
  4305. <a href="/bar\/)">
  4306. .
  4307. <a href="/bar\/)">
  4308. ````````````````````````````````
  4309. But they work in all other contexts, including URLs and link titles,
  4310. link references, and [info strings] in [fenced code blocks]:
  4311. ```````````````````````````````` example
  4312. [foo](/bar\* "ti\*tle")
  4313. .
  4314. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4315. ````````````````````````````````
  4316. ```````````````````````````````` example
  4317. [foo]
  4318. [foo]: /bar\* "ti\*tle"
  4319. .
  4320. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4321. ````````````````````````````````
  4322. ```````````````````````````````` example
  4323. ``` foo\+bar
  4324. foo
  4325. ```
  4326. .
  4327. <pre><code class="language-foo+bar">foo
  4328. </code></pre>
  4329. ````````````````````````````````
  4330. ## Entity and numeric character references
  4331. All valid HTML entity references and numeric character
  4332. references, except those occurring in code blocks and code spans,
  4333. are recognized as such and treated as equivalent to the
  4334. corresponding Unicode characters. Conforming CommonMark parsers
  4335. need not store information about whether a particular character
  4336. was represented in the source using a Unicode character or
  4337. an entity reference.
  4338. [Entity references](@) consist of `&` + any of the valid
  4339. HTML5 entity names + `;`. The
  4340. document <https://html.spec.whatwg.org/multipage/entities.json>
  4341. is used as an authoritative source for the valid entity
  4342. references and their corresponding code points.
  4343. ```````````````````````````````` example
  4344. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4345. &frac34; &HilbertSpace; &DifferentialD;
  4346. &ClockwiseContourIntegral; &ngE;
  4347. .
  4348. <p>  &amp; © Æ Ď
  4349. ¾ ℋ ⅆ
  4350. ∲ ≧̸</p>
  4351. ````````````````````````````````
  4352. [Decimal numeric character
  4353. references](@)
  4354. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4355. numeric character reference is parsed as the corresponding
  4356. Unicode character. Invalid Unicode code points will be replaced by
  4357. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4358. the code point `U+0000` will also be replaced by `U+FFFD`.
  4359. ```````````````````````````````` example
  4360. &#35; &#1234; &#992; &#0;
  4361. .
  4362. <p># Ӓ Ϡ �</p>
  4363. ````````````````````````````````
  4364. [Hexadecimal numeric character
  4365. references](@) consist of `&#` +
  4366. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4367. They too are parsed as the corresponding Unicode character (this
  4368. time specified with a hexadecimal numeral instead of decimal).
  4369. ```````````````````````````````` example
  4370. &#X22; &#XD06; &#xcab;
  4371. .
  4372. <p>&quot; ആ ಫ</p>
  4373. ````````````````````````````````
  4374. Here are some nonentities:
  4375. ```````````````````````````````` example
  4376. &nbsp &x; &#; &#x;
  4377. &#987654321;
  4378. &#abcdef0;
  4379. &ThisIsNotDefined; &hi?;
  4380. .
  4381. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4382. &amp;#987654321;
  4383. &amp;#abcdef0;
  4384. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4385. ````````````````````````````````
  4386. Although HTML5 does accept some entity references
  4387. without a trailing semicolon (such as `&copy`), these are not
  4388. recognized here, because it makes the grammar too ambiguous:
  4389. ```````````````````````````````` example
  4390. &copy
  4391. .
  4392. <p>&amp;copy</p>
  4393. ````````````````````````````````
  4394. Strings that are not on the list of HTML5 named entities are not
  4395. recognized as entity references either:
  4396. ```````````````````````````````` example
  4397. &MadeUpEntity;
  4398. .
  4399. <p>&amp;MadeUpEntity;</p>
  4400. ````````````````````````````````
  4401. Entity and numeric character references are recognized in any
  4402. context besides code spans or code blocks, including
  4403. URLs, [link titles], and [fenced code block][] [info strings]:
  4404. ```````````````````````````````` example
  4405. <a href="&ouml;&ouml;.html">
  4406. .
  4407. <a href="&ouml;&ouml;.html">
  4408. ````````````````````````````````
  4409. ```````````````````````````````` example
  4410. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4411. .
  4412. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4413. ````````````````````````````````
  4414. ```````````````````````````````` example
  4415. [foo]
  4416. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4417. .
  4418. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4419. ````````````````````````````````
  4420. ```````````````````````````````` example
  4421. ``` f&ouml;&ouml;
  4422. foo
  4423. ```
  4424. .
  4425. <pre><code class="language-föö">foo
  4426. </code></pre>
  4427. ````````````````````````````````
  4428. Entity and numeric character references are treated as literal
  4429. text in code spans and code blocks:
  4430. ```````````````````````````````` example
  4431. `f&ouml;&ouml;`
  4432. .
  4433. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4434. ````````````````````````````````
  4435. ```````````````````````````````` example
  4436. f&ouml;f&ouml;
  4437. .
  4438. <pre><code>f&amp;ouml;f&amp;ouml;
  4439. </code></pre>
  4440. ````````````````````````````````
  4441. ## Code spans
  4442. A [backtick string](@)
  4443. is a string of one or more backtick characters (`` ` ``) that is neither
  4444. preceded nor followed by a backtick.
  4445. A [code span](@) begins with a backtick string and ends with
  4446. a backtick string of equal length. The contents of the code span are
  4447. the characters between the two backtick strings, normalized in the
  4448. following ways:
  4449. - First, [line endings] are converted to [spaces].
  4450. - If the resulting string both begins *and* ends with a [space]
  4451. character, a single [space] character is removed from the
  4452. front and back. This allows you to include code that begins
  4453. or ends with backtick characters, which must be separated by
  4454. whitespace from the opening or closing backtick strings.
  4455. This is a simple code span:
  4456. ```````````````````````````````` example
  4457. `foo`
  4458. .
  4459. <p><code>foo</code></p>
  4460. ````````````````````````````````
  4461. Here two backticks are used, because the code contains a backtick.
  4462. This example also illustrates stripping of a single leading and
  4463. trailing space:
  4464. ```````````````````````````````` example
  4465. `` foo ` bar ``
  4466. .
  4467. <p><code>foo ` bar</code></p>
  4468. ````````````````````````````````
  4469. This example shows the motivation for stripping leading and trailing
  4470. spaces:
  4471. ```````````````````````````````` example
  4472. ` `` `
  4473. .
  4474. <p><code>``</code></p>
  4475. ````````````````````````````````
  4476. Note that only *one* space is stripped:
  4477. ```````````````````````````````` example
  4478. ` `` `
  4479. .
  4480. <p><code> `` </code></p>
  4481. ````````````````````````````````
  4482. The stripping only happens if the space is on both
  4483. sides of the string:
  4484. ```````````````````````````````` example
  4485. ` a`
  4486. .
  4487. <p><code> a</code></p>
  4488. ````````````````````````````````
  4489. Only [spaces], and not [unicode whitespace] in general, are
  4490. stripped in this way:
  4491. ```````````````````````````````` example
  4492. ` b `
  4493. .
  4494. <p><code> b </code></p>
  4495. ````````````````````````````````
  4496. [Line endings] are treated like spaces:
  4497. ```````````````````````````````` example
  4498. ``
  4499. foo
  4500. bar
  4501. baz
  4502. ``
  4503. .
  4504. <p><code>foo bar baz</code></p>
  4505. ````````````````````````````````
  4506. ```````````````````````````````` example
  4507. ``
  4508. foo
  4509. ``
  4510. .
  4511. <p><code>foo </code></p>
  4512. ````````````````````````````````
  4513. Interior spaces are not collapsed:
  4514. ```````````````````````````````` example
  4515. `foo bar
  4516. baz`
  4517. .
  4518. <p><code>foo bar baz</code></p>
  4519. ````````````````````````````````
  4520. Note that browsers will typically collapse consecutive spaces
  4521. when rendering `<code>` elements, so it is recommended that
  4522. the following CSS be used:
  4523. code{white-space: pre-wrap;}
  4524. Note that backslash escapes do not work in code spans. All backslashes
  4525. are treated literally:
  4526. ```````````````````````````````` example
  4527. `foo\`bar`
  4528. .
  4529. <p><code>foo\</code>bar`</p>
  4530. ````````````````````````````````
  4531. Backslash escapes are never needed, because one can always choose a
  4532. string of *n* backtick characters as delimiters, where the code does
  4533. not contain any strings of exactly *n* backtick characters.
  4534. ```````````````````````````````` example
  4535. ``foo`bar``
  4536. .
  4537. <p><code>foo`bar</code></p>
  4538. ````````````````````````````````
  4539. ```````````````````````````````` example
  4540. ` foo `` bar `
  4541. .
  4542. <p><code>foo `` bar</code></p>
  4543. ````````````````````````````````
  4544. Code span backticks have higher precedence than any other inline
  4545. constructs except HTML tags and autolinks. Thus, for example, this is
  4546. not parsed as emphasized text, since the second `*` is part of a code
  4547. span:
  4548. ```````````````````````````````` example
  4549. *foo`*`
  4550. .
  4551. <p>*foo<code>*</code></p>
  4552. ````````````````````````````````
  4553. And this is not parsed as a link:
  4554. ```````````````````````````````` example
  4555. [not a `link](/foo`)
  4556. .
  4557. <p>[not a <code>link](/foo</code>)</p>
  4558. ````````````````````````````````
  4559. Code spans, HTML tags, and autolinks have the same precedence.
  4560. Thus, this is code:
  4561. ```````````````````````````````` example
  4562. `<a href="`">`
  4563. .
  4564. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4565. ````````````````````````````````
  4566. But this is an HTML tag:
  4567. ```````````````````````````````` example
  4568. <a href="`">`
  4569. .
  4570. <p><a href="`">`</p>
  4571. ````````````````````````````````
  4572. And this is code:
  4573. ```````````````````````````````` example
  4574. `<http://foo.bar.`baz>`
  4575. .
  4576. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4577. ````````````````````````````````
  4578. But this is an autolink:
  4579. ```````````````````````````````` example
  4580. <http://foo.bar.`baz>`
  4581. .
  4582. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4583. ````````````````````````````````
  4584. When a backtick string is not closed by a matching backtick string,
  4585. we just have literal backticks:
  4586. ```````````````````````````````` example
  4587. ```foo``
  4588. .
  4589. <p>```foo``</p>
  4590. ````````````````````````````````
  4591. ```````````````````````````````` example
  4592. `foo
  4593. .
  4594. <p>`foo</p>
  4595. ````````````````````````````````
  4596. The following case also illustrates the need for opening and
  4597. closing backtick strings to be equal in length:
  4598. ```````````````````````````````` example
  4599. `foo``bar``
  4600. .
  4601. <p>`foo<code>bar</code></p>
  4602. ````````````````````````````````
  4603. ## Emphasis and strong emphasis
  4604. John Gruber's original [Markdown syntax
  4605. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4606. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4607. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4608. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4609. > tag.
  4610. This is enough for most users, but these rules leave much undecided,
  4611. especially when it comes to nested emphasis. The original
  4612. `Markdown.pl` test suite makes it clear that triple `***` and
  4613. `___` delimiters can be used for strong emphasis, and most
  4614. implementations have also allowed the following patterns:
  4615. ``` markdown
  4616. ***strong emph***
  4617. ***strong** in emph*
  4618. ***emph* in strong**
  4619. **in strong *emph***
  4620. *in emph **strong***
  4621. ```
  4622. The following patterns are less widely supported, but the intent
  4623. is clear and they are useful (especially in contexts like bibliography
  4624. entries):
  4625. ``` markdown
  4626. *emph *with emph* in it*
  4627. **strong **with strong** in it**
  4628. ```
  4629. Many implementations have also restricted intraword emphasis to
  4630. the `*` forms, to avoid unwanted emphasis in words containing
  4631. internal underscores. (It is best practice to put these in code
  4632. spans, but users often do not.)
  4633. ``` markdown
  4634. internal emphasis: foo*bar*baz
  4635. no emphasis: foo_bar_baz
  4636. ```
  4637. The rules given below capture all of these patterns, while allowing
  4638. for efficient parsing strategies that do not backtrack.
  4639. First, some definitions. A [delimiter run](@) is either
  4640. a sequence of one or more `*` characters that is not preceded or
  4641. followed by a non-backslash-escaped `*` character, or a sequence
  4642. of one or more `_` characters that is not preceded or followed by
  4643. a non-backslash-escaped `_` character.
  4644. A [left-flanking delimiter run](@) is
  4645. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4646. and either (2a) not followed by a [punctuation character], or
  4647. (2b) followed by a [punctuation character] and
  4648. preceded by [Unicode whitespace] or a [punctuation character].
  4649. For purposes of this definition, the beginning and the end of
  4650. the line count as Unicode whitespace.
  4651. A [right-flanking delimiter run](@) is
  4652. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4653. and either (2a) not preceded by a [punctuation character], or
  4654. (2b) preceded by a [punctuation character] and
  4655. followed by [Unicode whitespace] or a [punctuation character].
  4656. For purposes of this definition, the beginning and the end of
  4657. the line count as Unicode whitespace.
  4658. Here are some examples of delimiter runs.
  4659. - left-flanking but not right-flanking:
  4660. ```
  4661. ***abc
  4662. _abc
  4663. **"abc"
  4664. _"abc"
  4665. ```
  4666. - right-flanking but not left-flanking:
  4667. ```
  4668. abc***
  4669. abc_
  4670. "abc"**
  4671. "abc"_
  4672. ```
  4673. - Both left and right-flanking:
  4674. ```
  4675. abc***def
  4676. "abc"_"def"
  4677. ```
  4678. - Neither left nor right-flanking:
  4679. ```
  4680. abc *** def
  4681. a _ b
  4682. ```
  4683. (The idea of distinguishing left-flanking and right-flanking
  4684. delimiter runs based on the character before and the character
  4685. after comes from Roopesh Chander's
  4686. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4687. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4688. run," and its rules for distinguishing left- and right-flanking runs
  4689. are a bit more complex than the ones given here.)
  4690. The following rules define emphasis and strong emphasis:
  4691. 1. A single `*` character [can open emphasis](@)
  4692. iff (if and only if) it is part of a [left-flanking delimiter run].
  4693. 2. A single `_` character [can open emphasis] iff
  4694. it is part of a [left-flanking delimiter run]
  4695. and either (a) not part of a [right-flanking delimiter run]
  4696. or (b) part of a [right-flanking delimiter run]
  4697. preceded by punctuation.
  4698. 3. A single `*` character [can close emphasis](@)
  4699. iff it is part of a [right-flanking delimiter run].
  4700. 4. A single `_` character [can close emphasis] iff
  4701. it is part of a [right-flanking delimiter run]
  4702. and either (a) not part of a [left-flanking delimiter run]
  4703. or (b) part of a [left-flanking delimiter run]
  4704. followed by punctuation.
  4705. 5. A double `**` [can open strong emphasis](@)
  4706. iff it is part of a [left-flanking delimiter run].
  4707. 6. A double `__` [can open strong emphasis] iff
  4708. it is part of a [left-flanking delimiter run]
  4709. and either (a) not part of a [right-flanking delimiter run]
  4710. or (b) part of a [right-flanking delimiter run]
  4711. preceded by punctuation.
  4712. 7. A double `**` [can close strong emphasis](@)
  4713. iff it is part of a [right-flanking delimiter run].
  4714. 8. A double `__` [can close strong emphasis] iff
  4715. it is part of a [right-flanking delimiter run]
  4716. and either (a) not part of a [left-flanking delimiter run]
  4717. or (b) part of a [left-flanking delimiter run]
  4718. followed by punctuation.
  4719. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4720. with a delimiter that [can close emphasis], and that uses the same
  4721. character (`_` or `*`) as the opening delimiter. The
  4722. opening and closing delimiters must belong to separate
  4723. [delimiter runs]. If one of the delimiters can both
  4724. open and close emphasis, then the sum of the lengths of the
  4725. delimiter runs containing the opening and closing delimiters
  4726. must not be a multiple of 3.
  4727. 10. Strong emphasis begins with a delimiter that
  4728. [can open strong emphasis] and ends with a delimiter that
  4729. [can close strong emphasis], and that uses the same character
  4730. (`_` or `*`) as the opening delimiter. The
  4731. opening and closing delimiters must belong to separate
  4732. [delimiter runs]. If one of the delimiters can both open
  4733. and close strong emphasis, then the sum of the lengths of
  4734. the delimiter runs containing the opening and closing
  4735. delimiters must not be a multiple of 3.
  4736. 11. A literal `*` character cannot occur at the beginning or end of
  4737. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4738. is backslash-escaped.
  4739. 12. A literal `_` character cannot occur at the beginning or end of
  4740. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4741. is backslash-escaped.
  4742. Where rules 1--12 above are compatible with multiple parsings,
  4743. the following principles resolve ambiguity:
  4744. 13. The number of nestings should be minimized. Thus, for example,
  4745. an interpretation `<strong>...</strong>` is always preferred to
  4746. `<em><em>...</em></em>`.
  4747. 14. An interpretation `<em><strong>...</strong></em>` is always
  4748. preferred to `<strong><em>...</em></strong>`.
  4749. 15. When two potential emphasis or strong emphasis spans overlap,
  4750. so that the second begins before the first ends and ends after
  4751. the first ends, the first takes precedence. Thus, for example,
  4752. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4753. than `*foo <em>bar* baz</em>`.
  4754. 16. When there are two potential emphasis or strong emphasis spans
  4755. with the same closing delimiter, the shorter one (the one that
  4756. opens later) takes precedence. Thus, for example,
  4757. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4758. rather than `<strong>foo **bar baz</strong>`.
  4759. 17. Inline code spans, links, images, and HTML tags group more tightly
  4760. than emphasis. So, when there is a choice between an interpretation
  4761. that contains one of these elements and one that does not, the
  4762. former always wins. Thus, for example, `*[foo*](bar)` is
  4763. parsed as `*<a href="bar">foo*</a>` rather than as
  4764. `<em>[foo</em>](bar)`.
  4765. These rules can be illustrated through a series of examples.
  4766. Rule 1:
  4767. ```````````````````````````````` example
  4768. *foo bar*
  4769. .
  4770. <p><em>foo bar</em></p>
  4771. ````````````````````````````````
  4772. This is not emphasis, because the opening `*` is followed by
  4773. whitespace, and hence not part of a [left-flanking delimiter run]:
  4774. ```````````````````````````````` example
  4775. a * foo bar*
  4776. .
  4777. <p>a * foo bar*</p>
  4778. ````````````````````````````````
  4779. This is not emphasis, because the opening `*` is preceded
  4780. by an alphanumeric and followed by punctuation, and hence
  4781. not part of a [left-flanking delimiter run]:
  4782. ```````````````````````````````` example
  4783. a*"foo"*
  4784. .
  4785. <p>a*&quot;foo&quot;*</p>
  4786. ````````````````````````````````
  4787. Unicode nonbreaking spaces count as whitespace, too:
  4788. ```````````````````````````````` example
  4789. * a *
  4790. .
  4791. <p>* a *</p>
  4792. ````````````````````````````````
  4793. Intraword emphasis with `*` is permitted:
  4794. ```````````````````````````````` example
  4795. foo*bar*
  4796. .
  4797. <p>foo<em>bar</em></p>
  4798. ````````````````````````````````
  4799. ```````````````````````````````` example
  4800. 5*6*78
  4801. .
  4802. <p>5<em>6</em>78</p>
  4803. ````````````````````````````````
  4804. Rule 2:
  4805. ```````````````````````````````` example
  4806. _foo bar_
  4807. .
  4808. <p><em>foo bar</em></p>
  4809. ````````````````````````````````
  4810. This is not emphasis, because the opening `_` is followed by
  4811. whitespace:
  4812. ```````````````````````````````` example
  4813. _ foo bar_
  4814. .
  4815. <p>_ foo bar_</p>
  4816. ````````````````````````````````
  4817. This is not emphasis, because the opening `_` is preceded
  4818. by an alphanumeric and followed by punctuation:
  4819. ```````````````````````````````` example
  4820. a_"foo"_
  4821. .
  4822. <p>a_&quot;foo&quot;_</p>
  4823. ````````````````````````````````
  4824. Emphasis with `_` is not allowed inside words:
  4825. ```````````````````````````````` example
  4826. foo_bar_
  4827. .
  4828. <p>foo_bar_</p>
  4829. ````````````````````````````````
  4830. ```````````````````````````````` example
  4831. 5_6_78
  4832. .
  4833. <p>5_6_78</p>
  4834. ````````````````````````````````
  4835. ```````````````````````````````` example
  4836. пристаням_стремятся_
  4837. .
  4838. <p>пристаням_стремятся_</p>
  4839. ````````````````````````````````
  4840. Here `_` does not generate emphasis, because the first delimiter run
  4841. is right-flanking and the second left-flanking:
  4842. ```````````````````````````````` example
  4843. aa_"bb"_cc
  4844. .
  4845. <p>aa_&quot;bb&quot;_cc</p>
  4846. ````````````````````````````````
  4847. This is emphasis, even though the opening delimiter is
  4848. both left- and right-flanking, because it is preceded by
  4849. punctuation:
  4850. ```````````````````````````````` example
  4851. foo-_(bar)_
  4852. .
  4853. <p>foo-<em>(bar)</em></p>
  4854. ````````````````````````````````
  4855. Rule 3:
  4856. This is not emphasis, because the closing delimiter does
  4857. not match the opening delimiter:
  4858. ```````````````````````````````` example
  4859. _foo*
  4860. .
  4861. <p>_foo*</p>
  4862. ````````````````````````````````
  4863. This is not emphasis, because the closing `*` is preceded by
  4864. whitespace:
  4865. ```````````````````````````````` example
  4866. *foo bar *
  4867. .
  4868. <p>*foo bar *</p>
  4869. ````````````````````````````````
  4870. A newline also counts as whitespace:
  4871. ```````````````````````````````` example
  4872. *foo bar
  4873. *
  4874. .
  4875. <p>*foo bar
  4876. *</p>
  4877. ````````````````````````````````
  4878. This is not emphasis, because the second `*` is
  4879. preceded by punctuation and followed by an alphanumeric
  4880. (hence it is not part of a [right-flanking delimiter run]:
  4881. ```````````````````````````````` example
  4882. *(*foo)
  4883. .
  4884. <p>*(*foo)</p>
  4885. ````````````````````````````````
  4886. The point of this restriction is more easily appreciated
  4887. with this example:
  4888. ```````````````````````````````` example
  4889. *(*foo*)*
  4890. .
  4891. <p><em>(<em>foo</em>)</em></p>
  4892. ````````````````````````````````
  4893. Intraword emphasis with `*` is allowed:
  4894. ```````````````````````````````` example
  4895. *foo*bar
  4896. .
  4897. <p><em>foo</em>bar</p>
  4898. ````````````````````````````````
  4899. Rule 4:
  4900. This is not emphasis, because the closing `_` is preceded by
  4901. whitespace:
  4902. ```````````````````````````````` example
  4903. _foo bar _
  4904. .
  4905. <p>_foo bar _</p>
  4906. ````````````````````````````````
  4907. This is not emphasis, because the second `_` is
  4908. preceded by punctuation and followed by an alphanumeric:
  4909. ```````````````````````````````` example
  4910. _(_foo)
  4911. .
  4912. <p>_(_foo)</p>
  4913. ````````````````````````````````
  4914. This is emphasis within emphasis:
  4915. ```````````````````````````````` example
  4916. _(_foo_)_
  4917. .
  4918. <p><em>(<em>foo</em>)</em></p>
  4919. ````````````````````````````````
  4920. Intraword emphasis is disallowed for `_`:
  4921. ```````````````````````````````` example
  4922. _foo_bar
  4923. .
  4924. <p>_foo_bar</p>
  4925. ````````````````````````````````
  4926. ```````````````````````````````` example
  4927. _пристаням_стремятся
  4928. .
  4929. <p>_пристаням_стремятся</p>
  4930. ````````````````````````````````
  4931. ```````````````````````````````` example
  4932. _foo_bar_baz_
  4933. .
  4934. <p><em>foo_bar_baz</em></p>
  4935. ````````````````````````````````
  4936. This is emphasis, even though the closing delimiter is
  4937. both left- and right-flanking, because it is followed by
  4938. punctuation:
  4939. ```````````````````````````````` example
  4940. _(bar)_.
  4941. .
  4942. <p><em>(bar)</em>.</p>
  4943. ````````````````````````````````
  4944. Rule 5:
  4945. ```````````````````````````````` example
  4946. **foo bar**
  4947. .
  4948. <p><strong>foo bar</strong></p>
  4949. ````````````````````````````````
  4950. This is not strong emphasis, because the opening delimiter is
  4951. followed by whitespace:
  4952. ```````````````````````````````` example
  4953. ** foo bar**
  4954. .
  4955. <p>** foo bar**</p>
  4956. ````````````````````````````````
  4957. This is not strong emphasis, because the opening `**` is preceded
  4958. by an alphanumeric and followed by punctuation, and hence
  4959. not part of a [left-flanking delimiter run]:
  4960. ```````````````````````````````` example
  4961. a**"foo"**
  4962. .
  4963. <p>a**&quot;foo&quot;**</p>
  4964. ````````````````````````````````
  4965. Intraword strong emphasis with `**` is permitted:
  4966. ```````````````````````````````` example
  4967. foo**bar**
  4968. .
  4969. <p>foo<strong>bar</strong></p>
  4970. ````````````````````````````````
  4971. Rule 6:
  4972. ```````````````````````````````` example
  4973. __foo bar__
  4974. .
  4975. <p><strong>foo bar</strong></p>
  4976. ````````````````````````````````
  4977. This is not strong emphasis, because the opening delimiter is
  4978. followed by whitespace:
  4979. ```````````````````````````````` example
  4980. __ foo bar__
  4981. .
  4982. <p>__ foo bar__</p>
  4983. ````````````````````````````````
  4984. A newline counts as whitespace:
  4985. ```````````````````````````````` example
  4986. __
  4987. foo bar__
  4988. .
  4989. <p>__
  4990. foo bar__</p>
  4991. ````````````````````````````````
  4992. This is not strong emphasis, because the opening `__` is preceded
  4993. by an alphanumeric and followed by punctuation:
  4994. ```````````````````````````````` example
  4995. a__"foo"__
  4996. .
  4997. <p>a__&quot;foo&quot;__</p>
  4998. ````````````````````````````````
  4999. Intraword strong emphasis is forbidden with `__`:
  5000. ```````````````````````````````` example
  5001. foo__bar__
  5002. .
  5003. <p>foo__bar__</p>
  5004. ````````````````````````````````
  5005. ```````````````````````````````` example
  5006. 5__6__78
  5007. .
  5008. <p>5__6__78</p>
  5009. ````````````````````````````````
  5010. ```````````````````````````````` example
  5011. пристаням__стремятся__
  5012. .
  5013. <p>пристаням__стремятся__</p>
  5014. ````````````````````````````````
  5015. ```````````````````````````````` example
  5016. __foo, __bar__, baz__
  5017. .
  5018. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5019. ````````````````````````````````
  5020. This is strong emphasis, even though the opening delimiter is
  5021. both left- and right-flanking, because it is preceded by
  5022. punctuation:
  5023. ```````````````````````````````` example
  5024. foo-__(bar)__
  5025. .
  5026. <p>foo-<strong>(bar)</strong></p>
  5027. ````````````````````````````````
  5028. Rule 7:
  5029. This is not strong emphasis, because the closing delimiter is preceded
  5030. by whitespace:
  5031. ```````````````````````````````` example
  5032. **foo bar **
  5033. .
  5034. <p>**foo bar **</p>
  5035. ````````````````````````````````
  5036. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5037. Rule 11.)
  5038. This is not strong emphasis, because the second `**` is
  5039. preceded by punctuation and followed by an alphanumeric:
  5040. ```````````````````````````````` example
  5041. **(**foo)
  5042. .
  5043. <p>**(**foo)</p>
  5044. ````````````````````````````````
  5045. The point of this restriction is more easily appreciated
  5046. with these examples:
  5047. ```````````````````````````````` example
  5048. *(**foo**)*
  5049. .
  5050. <p><em>(<strong>foo</strong>)</em></p>
  5051. ````````````````````````````````
  5052. ```````````````````````````````` example
  5053. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5054. *Asclepias physocarpa*)**
  5055. .
  5056. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5057. <em>Asclepias physocarpa</em>)</strong></p>
  5058. ````````````````````````````````
  5059. ```````````````````````````````` example
  5060. **foo "*bar*" foo**
  5061. .
  5062. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5063. ````````````````````````````````
  5064. Intraword emphasis:
  5065. ```````````````````````````````` example
  5066. **foo**bar
  5067. .
  5068. <p><strong>foo</strong>bar</p>
  5069. ````````````````````````````````
  5070. Rule 8:
  5071. This is not strong emphasis, because the closing delimiter is
  5072. preceded by whitespace:
  5073. ```````````````````````````````` example
  5074. __foo bar __
  5075. .
  5076. <p>__foo bar __</p>
  5077. ````````````````````````````````
  5078. This is not strong emphasis, because the second `__` is
  5079. preceded by punctuation and followed by an alphanumeric:
  5080. ```````````````````````````````` example
  5081. __(__foo)
  5082. .
  5083. <p>__(__foo)</p>
  5084. ````````````````````````````````
  5085. The point of this restriction is more easily appreciated
  5086. with this example:
  5087. ```````````````````````````````` example
  5088. _(__foo__)_
  5089. .
  5090. <p><em>(<strong>foo</strong>)</em></p>
  5091. ````````````````````````````````
  5092. Intraword strong emphasis is forbidden with `__`:
  5093. ```````````````````````````````` example
  5094. __foo__bar
  5095. .
  5096. <p>__foo__bar</p>
  5097. ````````````````````````````````
  5098. ```````````````````````````````` example
  5099. __пристаням__стремятся
  5100. .
  5101. <p>__пристаням__стремятся</p>
  5102. ````````````````````````````````
  5103. ```````````````````````````````` example
  5104. __foo__bar__baz__
  5105. .
  5106. <p><strong>foo__bar__baz</strong></p>
  5107. ````````````````````````````````
  5108. This is strong emphasis, even though the closing delimiter is
  5109. both left- and right-flanking, because it is followed by
  5110. punctuation:
  5111. ```````````````````````````````` example
  5112. __(bar)__.
  5113. .
  5114. <p><strong>(bar)</strong>.</p>
  5115. ````````````````````````````````
  5116. Rule 9:
  5117. Any nonempty sequence of inline elements can be the contents of an
  5118. emphasized span.
  5119. ```````````````````````````````` example
  5120. *foo [bar](/url)*
  5121. .
  5122. <p><em>foo <a href="/url">bar</a></em></p>
  5123. ````````````````````````````````
  5124. ```````````````````````````````` example
  5125. *foo
  5126. bar*
  5127. .
  5128. <p><em>foo
  5129. bar</em></p>
  5130. ````````````````````````````````
  5131. In particular, emphasis and strong emphasis can be nested
  5132. inside emphasis:
  5133. ```````````````````````````````` example
  5134. _foo __bar__ baz_
  5135. .
  5136. <p><em>foo <strong>bar</strong> baz</em></p>
  5137. ````````````````````````````````
  5138. ```````````````````````````````` example
  5139. _foo _bar_ baz_
  5140. .
  5141. <p><em>foo <em>bar</em> baz</em></p>
  5142. ````````````````````````````````
  5143. ```````````````````````````````` example
  5144. __foo_ bar_
  5145. .
  5146. <p><em><em>foo</em> bar</em></p>
  5147. ````````````````````````````````
  5148. ```````````````````````````````` example
  5149. *foo *bar**
  5150. .
  5151. <p><em>foo <em>bar</em></em></p>
  5152. ````````````````````````````````
  5153. ```````````````````````````````` example
  5154. *foo **bar** baz*
  5155. .
  5156. <p><em>foo <strong>bar</strong> baz</em></p>
  5157. ````````````````````````````````
  5158. ```````````````````````````````` example
  5159. *foo**bar**baz*
  5160. .
  5161. <p><em>foo<strong>bar</strong>baz</em></p>
  5162. ````````````````````````````````
  5163. Note that in the preceding case, the interpretation
  5164. ``` markdown
  5165. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5166. ```
  5167. is precluded by the condition that a delimiter that
  5168. can both open and close (like the `*` after `foo`)
  5169. cannot form emphasis if the sum of the lengths of
  5170. the delimiter runs containing the opening and
  5171. closing delimiters is a multiple of 3.
  5172. For the same reason, we don't get two consecutive
  5173. emphasis sections in this example:
  5174. ```````````````````````````````` example
  5175. *foo**bar*
  5176. .
  5177. <p><em>foo**bar</em></p>
  5178. ````````````````````````````````
  5179. The same condition ensures that the following
  5180. cases are all strong emphasis nested inside
  5181. emphasis, even when the interior spaces are
  5182. omitted:
  5183. ```````````````````````````````` example
  5184. ***foo** bar*
  5185. .
  5186. <p><em><strong>foo</strong> bar</em></p>
  5187. ````````````````````````````````
  5188. ```````````````````````````````` example
  5189. *foo **bar***
  5190. .
  5191. <p><em>foo <strong>bar</strong></em></p>
  5192. ````````````````````````````````
  5193. ```````````````````````````````` example
  5194. *foo**bar***
  5195. .
  5196. <p><em>foo<strong>bar</strong></em></p>
  5197. ````````````````````````````````
  5198. Indefinite levels of nesting are possible:
  5199. ```````````````````````````````` example
  5200. *foo **bar *baz* bim** bop*
  5201. .
  5202. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5203. ````````````````````````````````
  5204. ```````````````````````````````` example
  5205. *foo [*bar*](/url)*
  5206. .
  5207. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5208. ````````````````````````````````
  5209. There can be no empty emphasis or strong emphasis:
  5210. ```````````````````````````````` example
  5211. ** is not an empty emphasis
  5212. .
  5213. <p>** is not an empty emphasis</p>
  5214. ````````````````````````````````
  5215. ```````````````````````````````` example
  5216. **** is not an empty strong emphasis
  5217. .
  5218. <p>**** is not an empty strong emphasis</p>
  5219. ````````````````````````````````
  5220. Rule 10:
  5221. Any nonempty sequence of inline elements can be the contents of an
  5222. strongly emphasized span.
  5223. ```````````````````````````````` example
  5224. **foo [bar](/url)**
  5225. .
  5226. <p><strong>foo <a href="/url">bar</a></strong></p>
  5227. ````````````````````````````````
  5228. ```````````````````````````````` example
  5229. **foo
  5230. bar**
  5231. .
  5232. <p><strong>foo
  5233. bar</strong></p>
  5234. ````````````````````````````````
  5235. In particular, emphasis and strong emphasis can be nested
  5236. inside strong emphasis:
  5237. ```````````````````````````````` example
  5238. __foo _bar_ baz__
  5239. .
  5240. <p><strong>foo <em>bar</em> baz</strong></p>
  5241. ````````````````````````````````
  5242. ```````````````````````````````` example
  5243. __foo __bar__ baz__
  5244. .
  5245. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5246. ````````````````````````````````
  5247. ```````````````````````````````` example
  5248. ____foo__ bar__
  5249. .
  5250. <p><strong><strong>foo</strong> bar</strong></p>
  5251. ````````````````````````````````
  5252. ```````````````````````````````` example
  5253. **foo **bar****
  5254. .
  5255. <p><strong>foo <strong>bar</strong></strong></p>
  5256. ````````````````````````````````
  5257. ```````````````````````````````` example
  5258. **foo *bar* baz**
  5259. .
  5260. <p><strong>foo <em>bar</em> baz</strong></p>
  5261. ````````````````````````````````
  5262. ```````````````````````````````` example
  5263. **foo*bar*baz**
  5264. .
  5265. <p><strong>foo<em>bar</em>baz</strong></p>
  5266. ````````````````````````````````
  5267. ```````````````````````````````` example
  5268. ***foo* bar**
  5269. .
  5270. <p><strong><em>foo</em> bar</strong></p>
  5271. ````````````````````````````````
  5272. ```````````````````````````````` example
  5273. **foo *bar***
  5274. .
  5275. <p><strong>foo <em>bar</em></strong></p>
  5276. ````````````````````````````````
  5277. Indefinite levels of nesting are possible:
  5278. ```````````````````````````````` example
  5279. **foo *bar **baz**
  5280. bim* bop**
  5281. .
  5282. <p><strong>foo <em>bar <strong>baz</strong>
  5283. bim</em> bop</strong></p>
  5284. ````````````````````````````````
  5285. ```````````````````````````````` example
  5286. **foo [*bar*](/url)**
  5287. .
  5288. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5289. ````````````````````````````````
  5290. There can be no empty emphasis or strong emphasis:
  5291. ```````````````````````````````` example
  5292. __ is not an empty emphasis
  5293. .
  5294. <p>__ is not an empty emphasis</p>
  5295. ````````````````````````````````
  5296. ```````````````````````````````` example
  5297. ____ is not an empty strong emphasis
  5298. .
  5299. <p>____ is not an empty strong emphasis</p>
  5300. ````````````````````````````````
  5301. Rule 11:
  5302. ```````````````````````````````` example
  5303. foo ***
  5304. .
  5305. <p>foo ***</p>
  5306. ````````````````````````````````
  5307. ```````````````````````````````` example
  5308. foo *\**
  5309. .
  5310. <p>foo <em>*</em></p>
  5311. ````````````````````````````````
  5312. ```````````````````````````````` example
  5313. foo *_*
  5314. .
  5315. <p>foo <em>_</em></p>
  5316. ````````````````````````````````
  5317. ```````````````````````````````` example
  5318. foo *****
  5319. .
  5320. <p>foo *****</p>
  5321. ````````````````````````````````
  5322. ```````````````````````````````` example
  5323. foo **\***
  5324. .
  5325. <p>foo <strong>*</strong></p>
  5326. ````````````````````````````````
  5327. ```````````````````````````````` example
  5328. foo **_**
  5329. .
  5330. <p>foo <strong>_</strong></p>
  5331. ````````````````````````````````
  5332. Note that when delimiters do not match evenly, Rule 11 determines
  5333. that the excess literal `*` characters will appear outside of the
  5334. emphasis, rather than inside it:
  5335. ```````````````````````````````` example
  5336. **foo*
  5337. .
  5338. <p>*<em>foo</em></p>
  5339. ````````````````````````````````
  5340. ```````````````````````````````` example
  5341. *foo**
  5342. .
  5343. <p><em>foo</em>*</p>
  5344. ````````````````````````````````
  5345. ```````````````````````````````` example
  5346. ***foo**
  5347. .
  5348. <p>*<strong>foo</strong></p>
  5349. ````````````````````````````````
  5350. ```````````````````````````````` example
  5351. ****foo*
  5352. .
  5353. <p>***<em>foo</em></p>
  5354. ````````````````````````````````
  5355. ```````````````````````````````` example
  5356. **foo***
  5357. .
  5358. <p><strong>foo</strong>*</p>
  5359. ````````````````````````````````
  5360. ```````````````````````````````` example
  5361. *foo****
  5362. .
  5363. <p><em>foo</em>***</p>
  5364. ````````````````````````````````
  5365. Rule 12:
  5366. ```````````````````````````````` example
  5367. foo ___
  5368. .
  5369. <p>foo ___</p>
  5370. ````````````````````````````````
  5371. ```````````````````````````````` example
  5372. foo _\__
  5373. .
  5374. <p>foo <em>_</em></p>
  5375. ````````````````````````````````
  5376. ```````````````````````````````` example
  5377. foo _*_
  5378. .
  5379. <p>foo <em>*</em></p>
  5380. ````````````````````````````````
  5381. ```````````````````````````````` example
  5382. foo _____
  5383. .
  5384. <p>foo _____</p>
  5385. ````````````````````````````````
  5386. ```````````````````````````````` example
  5387. foo __\___
  5388. .
  5389. <p>foo <strong>_</strong></p>
  5390. ````````````````````````````````
  5391. ```````````````````````````````` example
  5392. foo __*__
  5393. .
  5394. <p>foo <strong>*</strong></p>
  5395. ````````````````````````````````
  5396. ```````````````````````````````` example
  5397. __foo_
  5398. .
  5399. <p>_<em>foo</em></p>
  5400. ````````````````````````````````
  5401. Note that when delimiters do not match evenly, Rule 12 determines
  5402. that the excess literal `_` characters will appear outside of the
  5403. emphasis, rather than inside it:
  5404. ```````````````````````````````` example
  5405. _foo__
  5406. .
  5407. <p><em>foo</em>_</p>
  5408. ````````````````````````````````
  5409. ```````````````````````````````` example
  5410. ___foo__
  5411. .
  5412. <p>_<strong>foo</strong></p>
  5413. ````````````````````````````````
  5414. ```````````````````````````````` example
  5415. ____foo_
  5416. .
  5417. <p>___<em>foo</em></p>
  5418. ````````````````````````````````
  5419. ```````````````````````````````` example
  5420. __foo___
  5421. .
  5422. <p><strong>foo</strong>_</p>
  5423. ````````````````````````````````
  5424. ```````````````````````````````` example
  5425. _foo____
  5426. .
  5427. <p><em>foo</em>___</p>
  5428. ````````````````````````````````
  5429. Rule 13 implies that if you want emphasis nested directly inside
  5430. emphasis, you must use different delimiters:
  5431. ```````````````````````````````` example
  5432. **foo**
  5433. .
  5434. <p><strong>foo</strong></p>
  5435. ````````````````````````````````
  5436. ```````````````````````````````` example
  5437. *_foo_*
  5438. .
  5439. <p><em><em>foo</em></em></p>
  5440. ````````````````````````````````
  5441. ```````````````````````````````` example
  5442. __foo__
  5443. .
  5444. <p><strong>foo</strong></p>
  5445. ````````````````````````````````
  5446. ```````````````````````````````` example
  5447. _*foo*_
  5448. .
  5449. <p><em><em>foo</em></em></p>
  5450. ````````````````````````````````
  5451. However, strong emphasis within strong emphasis is possible without
  5452. switching delimiters:
  5453. ```````````````````````````````` example
  5454. ****foo****
  5455. .
  5456. <p><strong><strong>foo</strong></strong></p>
  5457. ````````````````````````````````
  5458. ```````````````````````````````` example
  5459. ____foo____
  5460. .
  5461. <p><strong><strong>foo</strong></strong></p>
  5462. ````````````````````````````````
  5463. Rule 13 can be applied to arbitrarily long sequences of
  5464. delimiters:
  5465. ```````````````````````````````` example
  5466. ******foo******
  5467. .
  5468. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5469. ````````````````````````````````
  5470. Rule 14:
  5471. ```````````````````````````````` example
  5472. ***foo***
  5473. .
  5474. <p><em><strong>foo</strong></em></p>
  5475. ````````````````````````````````
  5476. ```````````````````````````````` example
  5477. _____foo_____
  5478. .
  5479. <p><em><strong><strong>foo</strong></strong></em></p>
  5480. ````````````````````````````````
  5481. Rule 15:
  5482. ```````````````````````````````` example
  5483. *foo _bar* baz_
  5484. .
  5485. <p><em>foo _bar</em> baz_</p>
  5486. ````````````````````````````````
  5487. ```````````````````````````````` example
  5488. *foo __bar *baz bim__ bam*
  5489. .
  5490. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5491. ````````````````````````````````
  5492. Rule 16:
  5493. ```````````````````````````````` example
  5494. **foo **bar baz**
  5495. .
  5496. <p>**foo <strong>bar baz</strong></p>
  5497. ````````````````````````````````
  5498. ```````````````````````````````` example
  5499. *foo *bar baz*
  5500. .
  5501. <p>*foo <em>bar baz</em></p>
  5502. ````````````````````````````````
  5503. Rule 17:
  5504. ```````````````````````````````` example
  5505. *[bar*](/url)
  5506. .
  5507. <p>*<a href="/url">bar*</a></p>
  5508. ````````````````````````````````
  5509. ```````````````````````````````` example
  5510. _foo [bar_](/url)
  5511. .
  5512. <p>_foo <a href="/url">bar_</a></p>
  5513. ````````````````````````````````
  5514. ```````````````````````````````` example
  5515. *<img src="foo" title="*"/>
  5516. .
  5517. <p>*<img src="foo" title="*"/></p>
  5518. ````````````````````````````````
  5519. ```````````````````````````````` example
  5520. **<a href="**">
  5521. .
  5522. <p>**<a href="**"></p>
  5523. ````````````````````````````````
  5524. ```````````````````````````````` example
  5525. __<a href="__">
  5526. .
  5527. <p>__<a href="__"></p>
  5528. ````````````````````````````````
  5529. ```````````````````````````````` example
  5530. *a `*`*
  5531. .
  5532. <p><em>a <code>*</code></em></p>
  5533. ````````````````````````````````
  5534. ```````````````````````````````` example
  5535. _a `_`_
  5536. .
  5537. <p><em>a <code>_</code></em></p>
  5538. ````````````````````````````````
  5539. ```````````````````````````````` example
  5540. **a<http://foo.bar/?q=**>
  5541. .
  5542. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5543. ````````````````````````````````
  5544. ```````````````````````````````` example
  5545. __a<http://foo.bar/?q=__>
  5546. .
  5547. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5548. ````````````````````````````````
  5549. ## Links
  5550. A link contains [link text] (the visible text), a [link destination]
  5551. (the URI that is the link destination), and optionally a [link title].
  5552. There are two basic kinds of links in Markdown. In [inline links] the
  5553. destination and title are given immediately after the link text. In
  5554. [reference links] the destination and title are defined elsewhere in
  5555. the document.
  5556. A [link text](@) consists of a sequence of zero or more
  5557. inline elements enclosed by square brackets (`[` and `]`). The
  5558. following rules apply:
  5559. - Links may not contain other links, at any level of nesting. If
  5560. multiple otherwise valid link definitions appear nested inside each
  5561. other, the inner-most definition is used.
  5562. - Brackets are allowed in the [link text] only if (a) they
  5563. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5564. with an open bracket `[`, a sequence of zero or more inlines, and
  5565. a close bracket `]`.
  5566. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5567. than the brackets in link text. Thus, for example,
  5568. `` [foo`]` `` could not be a link text, since the second `]`
  5569. is part of a code span.
  5570. - The brackets in link text bind more tightly than markers for
  5571. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5572. A [link destination](@) consists of either
  5573. - a sequence of zero or more characters between an opening `<` and a
  5574. closing `>` that contains no line breaks or unescaped
  5575. `<` or `>` characters, or
  5576. - a nonempty sequence of characters that does not include
  5577. ASCII space or control characters, and includes parentheses
  5578. only if (a) they are backslash-escaped or (b) they are part of
  5579. a balanced pair of unescaped parentheses. (Implementations
  5580. may impose limits on parentheses nesting to avoid performance
  5581. issues, but at least three levels of nesting should be supported.)
  5582. A [link title](@) consists of either
  5583. - a sequence of zero or more characters between straight double-quote
  5584. characters (`"`), including a `"` character only if it is
  5585. backslash-escaped, or
  5586. - a sequence of zero or more characters between straight single-quote
  5587. characters (`'`), including a `'` character only if it is
  5588. backslash-escaped, or
  5589. - a sequence of zero or more characters between matching parentheses
  5590. (`(...)`), including a `)` character only if it is backslash-escaped.
  5591. Although [link titles] may span multiple lines, they may not contain
  5592. a [blank line].
  5593. An [inline link](@) consists of a [link text] followed immediately
  5594. by a left parenthesis `(`, optional [whitespace], an optional
  5595. [link destination], an optional [link title] separated from the link
  5596. destination by [whitespace], optional [whitespace], and a right
  5597. parenthesis `)`. The link's text consists of the inlines contained
  5598. in the [link text] (excluding the enclosing square brackets).
  5599. The link's URI consists of the link destination, excluding enclosing
  5600. `<...>` if present, with backslash-escapes in effect as described
  5601. above. The link's title consists of the link title, excluding its
  5602. enclosing delimiters, with backslash-escapes in effect as described
  5603. above.
  5604. Here is a simple inline link:
  5605. ```````````````````````````````` example
  5606. [link](/uri "title")
  5607. .
  5608. <p><a href="/uri" title="title">link</a></p>
  5609. ````````````````````````````````
  5610. The title may be omitted:
  5611. ```````````````````````````````` example
  5612. [link](/uri)
  5613. .
  5614. <p><a href="/uri">link</a></p>
  5615. ````````````````````````````````
  5616. Both the title and the destination may be omitted:
  5617. ```````````````````````````````` example
  5618. [link]()
  5619. .
  5620. <p><a href="">link</a></p>
  5621. ````````````````````````````````
  5622. ```````````````````````````````` example
  5623. [link](<>)
  5624. .
  5625. <p><a href="">link</a></p>
  5626. ````````````````````````````````
  5627. The destination can only contain spaces if it is
  5628. enclosed in pointy brackets:
  5629. ```````````````````````````````` example
  5630. [link](/my uri)
  5631. .
  5632. <p>[link](/my uri)</p>
  5633. ````````````````````````````````
  5634. ```````````````````````````````` example
  5635. [link](</my uri>)
  5636. .
  5637. <p><a href="/my%20uri">link</a></p>
  5638. ````````````````````````````````
  5639. The destination cannot contain line breaks,
  5640. even if enclosed in pointy brackets:
  5641. ```````````````````````````````` example
  5642. [link](foo
  5643. bar)
  5644. .
  5645. <p>[link](foo
  5646. bar)</p>
  5647. ````````````````````````````````
  5648. ```````````````````````````````` example
  5649. [link](<foo
  5650. bar>)
  5651. .
  5652. <p>[link](<foo
  5653. bar>)</p>
  5654. ````````````````````````````````
  5655. Parentheses inside the link destination may be escaped:
  5656. ```````````````````````````````` example
  5657. [link](\(foo\))
  5658. .
  5659. <p><a href="(foo)">link</a></p>
  5660. ````````````````````````````````
  5661. Any number of parentheses are allowed without escaping, as long as they are
  5662. balanced:
  5663. ```````````````````````````````` example
  5664. [link](foo(and(bar)))
  5665. .
  5666. <p><a href="foo(and(bar))">link</a></p>
  5667. ````````````````````````````````
  5668. However, if you have unbalanced parentheses, you need to escape or use the
  5669. `<...>` form:
  5670. ```````````````````````````````` example
  5671. [link](foo\(and\(bar\))
  5672. .
  5673. <p><a href="foo(and(bar)">link</a></p>
  5674. ````````````````````````````````
  5675. ```````````````````````````````` example
  5676. [link](<foo(and(bar)>)
  5677. .
  5678. <p><a href="foo(and(bar)">link</a></p>
  5679. ````````````````````````````````
  5680. Parentheses and other symbols can also be escaped, as usual
  5681. in Markdown:
  5682. ```````````````````````````````` example
  5683. [link](foo\)\:)
  5684. .
  5685. <p><a href="foo):">link</a></p>
  5686. ````````````````````````````````
  5687. A link can contain fragment identifiers and queries:
  5688. ```````````````````````````````` example
  5689. [link](#fragment)
  5690. [link](http://example.com#fragment)
  5691. [link](http://example.com?foo=3#frag)
  5692. .
  5693. <p><a href="#fragment">link</a></p>
  5694. <p><a href="http://example.com#fragment">link</a></p>
  5695. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5696. ````````````````````````````````
  5697. Note that a backslash before a non-escapable character is
  5698. just a backslash:
  5699. ```````````````````````````````` example
  5700. [link](foo\bar)
  5701. .
  5702. <p><a href="foo%5Cbar">link</a></p>
  5703. ````````````````````````````````
  5704. URL-escaping should be left alone inside the destination, as all
  5705. URL-escaped characters are also valid URL characters. Entity and
  5706. numerical character references in the destination will be parsed
  5707. into the corresponding Unicode code points, as usual. These may
  5708. be optionally URL-escaped when written as HTML, but this spec
  5709. does not enforce any particular policy for rendering URLs in
  5710. HTML or other formats. Renderers may make different decisions
  5711. about how to escape or normalize URLs in the output.
  5712. ```````````````````````````````` example
  5713. [link](foo%20b&auml;)
  5714. .
  5715. <p><a href="foo%20b%C3%A4">link</a></p>
  5716. ````````````````````````````````
  5717. Note that, because titles can often be parsed as destinations,
  5718. if you try to omit the destination and keep the title, you'll
  5719. get unexpected results:
  5720. ```````````````````````````````` example
  5721. [link]("title")
  5722. .
  5723. <p><a href="%22title%22">link</a></p>
  5724. ````````````````````````````````
  5725. Titles may be in single quotes, double quotes, or parentheses:
  5726. ```````````````````````````````` example
  5727. [link](/url "title")
  5728. [link](/url 'title')
  5729. [link](/url (title))
  5730. .
  5731. <p><a href="/url" title="title">link</a>
  5732. <a href="/url" title="title">link</a>
  5733. <a href="/url" title="title">link</a></p>
  5734. ````````````````````````````````
  5735. Backslash escapes and entity and numeric character references
  5736. may be used in titles:
  5737. ```````````````````````````````` example
  5738. [link](/url "title \"&quot;")
  5739. .
  5740. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5741. ````````````````````````````````
  5742. Titles must be separated from the link using a [whitespace].
  5743. Other [Unicode whitespace] like non-breaking space doesn't work.
  5744. ```````````````````````````````` example
  5745. [link](/url "title")
  5746. .
  5747. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5748. ````````````````````````````````
  5749. Nested balanced quotes are not allowed without escaping:
  5750. ```````````````````````````````` example
  5751. [link](/url "title "and" title")
  5752. .
  5753. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5754. ````````````````````````````````
  5755. But it is easy to work around this by using a different quote type:
  5756. ```````````````````````````````` example
  5757. [link](/url 'title "and" title')
  5758. .
  5759. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5760. ````````````````````````````````
  5761. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5762. title, and its test suite included a test demonstrating this.
  5763. But it is hard to see a good rationale for the extra complexity this
  5764. brings, since there are already many ways---backslash escaping,
  5765. entity and numeric character references, or using a different
  5766. quote type for the enclosing title---to write titles containing
  5767. double quotes. `Markdown.pl`'s handling of titles has a number
  5768. of other strange features. For example, it allows single-quoted
  5769. titles in inline links, but not reference links. And, in
  5770. reference links but not inline links, it allows a title to begin
  5771. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5772. titles with no closing quotation mark, though 1.0.2b8 does not.
  5773. It seems preferable to adopt a simple, rational rule that works
  5774. the same way in inline links and link reference definitions.)
  5775. [Whitespace] is allowed around the destination and title:
  5776. ```````````````````````````````` example
  5777. [link]( /uri
  5778. "title" )
  5779. .
  5780. <p><a href="/uri" title="title">link</a></p>
  5781. ````````````````````````````````
  5782. But it is not allowed between the link text and the
  5783. following parenthesis:
  5784. ```````````````````````````````` example
  5785. [link] (/uri)
  5786. .
  5787. <p>[link] (/uri)</p>
  5788. ````````````````````````````````
  5789. The link text may contain balanced brackets, but not unbalanced ones,
  5790. unless they are escaped:
  5791. ```````````````````````````````` example
  5792. [link [foo [bar]]](/uri)
  5793. .
  5794. <p><a href="/uri">link [foo [bar]]</a></p>
  5795. ````````````````````````````````
  5796. ```````````````````````````````` example
  5797. [link] bar](/uri)
  5798. .
  5799. <p>[link] bar](/uri)</p>
  5800. ````````````````````````````````
  5801. ```````````````````````````````` example
  5802. [link [bar](/uri)
  5803. .
  5804. <p>[link <a href="/uri">bar</a></p>
  5805. ````````````````````````````````
  5806. ```````````````````````````````` example
  5807. [link \[bar](/uri)
  5808. .
  5809. <p><a href="/uri">link [bar</a></p>
  5810. ````````````````````````````````
  5811. The link text may contain inline content:
  5812. ```````````````````````````````` example
  5813. [link *foo **bar** `#`*](/uri)
  5814. .
  5815. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5816. ````````````````````````````````
  5817. ```````````````````````````````` example
  5818. [![moon](moon.jpg)](/uri)
  5819. .
  5820. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5821. ````````````````````````````````
  5822. However, links may not contain other links, at any level of nesting.
  5823. ```````````````````````````````` example
  5824. [foo [bar](/uri)](/uri)
  5825. .
  5826. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5827. ````````````````````````````````
  5828. ```````````````````````````````` example
  5829. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5830. .
  5831. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5832. ````````````````````````````````
  5833. ```````````````````````````````` example
  5834. ![[[foo](uri1)](uri2)](uri3)
  5835. .
  5836. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5837. ````````````````````````````````
  5838. These cases illustrate the precedence of link text grouping over
  5839. emphasis grouping:
  5840. ```````````````````````````````` example
  5841. *[foo*](/uri)
  5842. .
  5843. <p>*<a href="/uri">foo*</a></p>
  5844. ````````````````````````````````
  5845. ```````````````````````````````` example
  5846. [foo *bar](baz*)
  5847. .
  5848. <p><a href="baz*">foo *bar</a></p>
  5849. ````````````````````````````````
  5850. Note that brackets that *aren't* part of links do not take
  5851. precedence:
  5852. ```````````````````````````````` example
  5853. *foo [bar* baz]
  5854. .
  5855. <p><em>foo [bar</em> baz]</p>
  5856. ````````````````````````````````
  5857. These cases illustrate the precedence of HTML tags, code spans,
  5858. and autolinks over link grouping:
  5859. ```````````````````````````````` example
  5860. [foo <bar attr="](baz)">
  5861. .
  5862. <p>[foo <bar attr="](baz)"></p>
  5863. ````````````````````````````````
  5864. ```````````````````````````````` example
  5865. [foo`](/uri)`
  5866. .
  5867. <p>[foo<code>](/uri)</code></p>
  5868. ````````````````````````````````
  5869. ```````````````````````````````` example
  5870. [foo<http://example.com/?search=](uri)>
  5871. .
  5872. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5873. ````````````````````````````````
  5874. There are three kinds of [reference link](@)s:
  5875. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5876. and [shortcut](#shortcut-reference-link).
  5877. A [full reference link](@)
  5878. consists of a [link text] immediately followed by a [link label]
  5879. that [matches] a [link reference definition] elsewhere in the document.
  5880. A [link label](@) begins with a left bracket (`[`) and ends
  5881. with the first right bracket (`]`) that is not backslash-escaped.
  5882. Between these brackets there must be at least one [non-whitespace character].
  5883. Unescaped square bracket characters are not allowed inside the
  5884. opening and closing square brackets of [link labels]. A link
  5885. label can have at most 999 characters inside the square
  5886. brackets.
  5887. One label [matches](@)
  5888. another just in case their normalized forms are equal. To normalize a
  5889. label, strip off the opening and closing brackets,
  5890. perform the *Unicode case fold*, strip leading and trailing
  5891. [whitespace] and collapse consecutive internal
  5892. [whitespace] to a single space. If there are multiple
  5893. matching reference link definitions, the one that comes first in the
  5894. document is used. (It is desirable in such cases to emit a warning.)
  5895. The contents of the first link label are parsed as inlines, which are
  5896. used as the link's text. The link's URI and title are provided by the
  5897. matching [link reference definition].
  5898. Here is a simple example:
  5899. ```````````````````````````````` example
  5900. [foo][bar]
  5901. [bar]: /url "title"
  5902. .
  5903. <p><a href="/url" title="title">foo</a></p>
  5904. ````````````````````````````````
  5905. The rules for the [link text] are the same as with
  5906. [inline links]. Thus:
  5907. The link text may contain balanced brackets, but not unbalanced ones,
  5908. unless they are escaped:
  5909. ```````````````````````````````` example
  5910. [link [foo [bar]]][ref]
  5911. [ref]: /uri
  5912. .
  5913. <p><a href="/uri">link [foo [bar]]</a></p>
  5914. ````````````````````````````````
  5915. ```````````````````````````````` example
  5916. [link \[bar][ref]
  5917. [ref]: /uri
  5918. .
  5919. <p><a href="/uri">link [bar</a></p>
  5920. ````````````````````````````````
  5921. The link text may contain inline content:
  5922. ```````````````````````````````` example
  5923. [link *foo **bar** `#`*][ref]
  5924. [ref]: /uri
  5925. .
  5926. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5927. ````````````````````````````````
  5928. ```````````````````````````````` example
  5929. [![moon](moon.jpg)][ref]
  5930. [ref]: /uri
  5931. .
  5932. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5933. ````````````````````````````````
  5934. However, links may not contain other links, at any level of nesting.
  5935. ```````````````````````````````` example
  5936. [foo [bar](/uri)][ref]
  5937. [ref]: /uri
  5938. .
  5939. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5940. ````````````````````````````````
  5941. ```````````````````````````````` example
  5942. [foo *bar [baz][ref]*][ref]
  5943. [ref]: /uri
  5944. .
  5945. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5946. ````````````````````````````````
  5947. (In the examples above, we have two [shortcut reference links]
  5948. instead of one [full reference link].)
  5949. The following cases illustrate the precedence of link text grouping over
  5950. emphasis grouping:
  5951. ```````````````````````````````` example
  5952. *[foo*][ref]
  5953. [ref]: /uri
  5954. .
  5955. <p>*<a href="/uri">foo*</a></p>
  5956. ````````````````````````````````
  5957. ```````````````````````````````` example
  5958. [foo *bar][ref]
  5959. [ref]: /uri
  5960. .
  5961. <p><a href="/uri">foo *bar</a></p>
  5962. ````````````````````````````````
  5963. These cases illustrate the precedence of HTML tags, code spans,
  5964. and autolinks over link grouping:
  5965. ```````````````````````````````` example
  5966. [foo <bar attr="][ref]">
  5967. [ref]: /uri
  5968. .
  5969. <p>[foo <bar attr="][ref]"></p>
  5970. ````````````````````````````````
  5971. ```````````````````````````````` example
  5972. [foo`][ref]`
  5973. [ref]: /uri
  5974. .
  5975. <p>[foo<code>][ref]</code></p>
  5976. ````````````````````````````````
  5977. ```````````````````````````````` example
  5978. [foo<http://example.com/?search=][ref]>
  5979. [ref]: /uri
  5980. .
  5981. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5982. ````````````````````````````````
  5983. Matching is case-insensitive:
  5984. ```````````````````````````````` example
  5985. [foo][BaR]
  5986. [bar]: /url "title"
  5987. .
  5988. <p><a href="/url" title="title">foo</a></p>
  5989. ````````````````````````````````
  5990. Unicode case fold is used:
  5991. ```````````````````````````````` example
  5992. [Толпой][Толпой] is a Russian word.
  5993. [ТОЛПОЙ]: /url
  5994. .
  5995. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5996. ````````````````````````````````
  5997. Consecutive internal [whitespace] is treated as one space for
  5998. purposes of determining matching:
  5999. ```````````````````````````````` example
  6000. [Foo
  6001. bar]: /url
  6002. [Baz][Foo bar]
  6003. .
  6004. <p><a href="/url">Baz</a></p>
  6005. ````````````````````````````````
  6006. No [whitespace] is allowed between the [link text] and the
  6007. [link label]:
  6008. ```````````````````````````````` example
  6009. [foo] [bar]
  6010. [bar]: /url "title"
  6011. .
  6012. <p>[foo] <a href="/url" title="title">bar</a></p>
  6013. ````````````````````````````````
  6014. ```````````````````````````````` example
  6015. [foo]
  6016. [bar]
  6017. [bar]: /url "title"
  6018. .
  6019. <p>[foo]
  6020. <a href="/url" title="title">bar</a></p>
  6021. ````````````````````````````````
  6022. This is a departure from John Gruber's original Markdown syntax
  6023. description, which explicitly allows whitespace between the link
  6024. text and the link label. It brings reference links in line with
  6025. [inline links], which (according to both original Markdown and
  6026. this spec) cannot have whitespace after the link text. More
  6027. importantly, it prevents inadvertent capture of consecutive
  6028. [shortcut reference links]. If whitespace is allowed between the
  6029. link text and the link label, then in the following we will have
  6030. a single reference link, not two shortcut reference links, as
  6031. intended:
  6032. ``` markdown
  6033. [foo]
  6034. [bar]
  6035. [foo]: /url1
  6036. [bar]: /url2
  6037. ```
  6038. (Note that [shortcut reference links] were introduced by Gruber
  6039. himself in a beta version of `Markdown.pl`, but never included
  6040. in the official syntax description. Without shortcut reference
  6041. links, it is harmless to allow space between the link text and
  6042. link label; but once shortcut references are introduced, it is
  6043. too dangerous to allow this, as it frequently leads to
  6044. unintended results.)
  6045. When there are multiple matching [link reference definitions],
  6046. the first is used:
  6047. ```````````````````````````````` example
  6048. [foo]: /url1
  6049. [foo]: /url2
  6050. [bar][foo]
  6051. .
  6052. <p><a href="/url1">bar</a></p>
  6053. ````````````````````````````````
  6054. Note that matching is performed on normalized strings, not parsed
  6055. inline content. So the following does not match, even though the
  6056. labels define equivalent inline content:
  6057. ```````````````````````````````` example
  6058. [bar][foo\!]
  6059. [foo!]: /url
  6060. .
  6061. <p>[bar][foo!]</p>
  6062. ````````````````````````````````
  6063. [Link labels] cannot contain brackets, unless they are
  6064. backslash-escaped:
  6065. ```````````````````````````````` example
  6066. [foo][ref[]
  6067. [ref[]: /uri
  6068. .
  6069. <p>[foo][ref[]</p>
  6070. <p>[ref[]: /uri</p>
  6071. ````````````````````````````````
  6072. ```````````````````````````````` example
  6073. [foo][ref[bar]]
  6074. [ref[bar]]: /uri
  6075. .
  6076. <p>[foo][ref[bar]]</p>
  6077. <p>[ref[bar]]: /uri</p>
  6078. ````````````````````````````````
  6079. ```````````````````````````````` example
  6080. [[[foo]]]
  6081. [[[foo]]]: /url
  6082. .
  6083. <p>[[[foo]]]</p>
  6084. <p>[[[foo]]]: /url</p>
  6085. ````````````````````````````````
  6086. ```````````````````````````````` example
  6087. [foo][ref\[]
  6088. [ref\[]: /uri
  6089. .
  6090. <p><a href="/uri">foo</a></p>
  6091. ````````````````````````````````
  6092. Note that in this example `]` is not backslash-escaped:
  6093. ```````````````````````````````` example
  6094. [bar\\]: /uri
  6095. [bar\\]
  6096. .
  6097. <p><a href="/uri">bar\</a></p>
  6098. ````````````````````````````````
  6099. A [link label] must contain at least one [non-whitespace character]:
  6100. ```````````````````````````````` example
  6101. []
  6102. []: /uri
  6103. .
  6104. <p>[]</p>
  6105. <p>[]: /uri</p>
  6106. ````````````````````````````````
  6107. ```````````````````````````````` example
  6108. [
  6109. ]
  6110. [
  6111. ]: /uri
  6112. .
  6113. <p>[
  6114. ]</p>
  6115. <p>[
  6116. ]: /uri</p>
  6117. ````````````````````````````````
  6118. A [collapsed reference link](@)
  6119. consists of a [link label] that [matches] a
  6120. [link reference definition] elsewhere in the
  6121. document, followed by the string `[]`.
  6122. The contents of the first link label are parsed as inlines,
  6123. which are used as the link's text. The link's URI and title are
  6124. provided by the matching reference link definition. Thus,
  6125. `[foo][]` is equivalent to `[foo][foo]`.
  6126. ```````````````````````````````` example
  6127. [foo][]
  6128. [foo]: /url "title"
  6129. .
  6130. <p><a href="/url" title="title">foo</a></p>
  6131. ````````````````````````````````
  6132. ```````````````````````````````` example
  6133. [*foo* bar][]
  6134. [*foo* bar]: /url "title"
  6135. .
  6136. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6137. ````````````````````````````````
  6138. The link labels are case-insensitive:
  6139. ```````````````````````````````` example
  6140. [Foo][]
  6141. [foo]: /url "title"
  6142. .
  6143. <p><a href="/url" title="title">Foo</a></p>
  6144. ````````````````````````````````
  6145. As with full reference links, [whitespace] is not
  6146. allowed between the two sets of brackets:
  6147. ```````````````````````````````` example
  6148. [foo]
  6149. []
  6150. [foo]: /url "title"
  6151. .
  6152. <p><a href="/url" title="title">foo</a>
  6153. []</p>
  6154. ````````````````````````````````
  6155. A [shortcut reference link](@)
  6156. consists of a [link label] that [matches] a
  6157. [link reference definition] elsewhere in the
  6158. document and is not followed by `[]` or a link label.
  6159. The contents of the first link label are parsed as inlines,
  6160. which are used as the link's text. The link's URI and title
  6161. are provided by the matching link reference definition.
  6162. Thus, `[foo]` is equivalent to `[foo][]`.
  6163. ```````````````````````````````` example
  6164. [foo]
  6165. [foo]: /url "title"
  6166. .
  6167. <p><a href="/url" title="title">foo</a></p>
  6168. ````````````````````````````````
  6169. ```````````````````````````````` example
  6170. [*foo* bar]
  6171. [*foo* bar]: /url "title"
  6172. .
  6173. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6174. ````````````````````````````````
  6175. ```````````````````````````````` example
  6176. [[*foo* bar]]
  6177. [*foo* bar]: /url "title"
  6178. .
  6179. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6180. ````````````````````````````````
  6181. ```````````````````````````````` example
  6182. [[bar [foo]
  6183. [foo]: /url
  6184. .
  6185. <p>[[bar <a href="/url">foo</a></p>
  6186. ````````````````````````````````
  6187. The link labels are case-insensitive:
  6188. ```````````````````````````````` example
  6189. [Foo]
  6190. [foo]: /url "title"
  6191. .
  6192. <p><a href="/url" title="title">Foo</a></p>
  6193. ````````````````````````````````
  6194. A space after the link text should be preserved:
  6195. ```````````````````````````````` example
  6196. [foo] bar
  6197. [foo]: /url
  6198. .
  6199. <p><a href="/url">foo</a> bar</p>
  6200. ````````````````````````````````
  6201. If you just want bracketed text, you can backslash-escape the
  6202. opening bracket to avoid links:
  6203. ```````````````````````````````` example
  6204. \[foo]
  6205. [foo]: /url "title"
  6206. .
  6207. <p>[foo]</p>
  6208. ````````````````````````````````
  6209. Note that this is a link, because a link label ends with the first
  6210. following closing bracket:
  6211. ```````````````````````````````` example
  6212. [foo*]: /url
  6213. *[foo*]
  6214. .
  6215. <p>*<a href="/url">foo*</a></p>
  6216. ````````````````````````````````
  6217. Full and compact references take precedence over shortcut
  6218. references:
  6219. ```````````````````````````````` example
  6220. [foo][bar]
  6221. [foo]: /url1
  6222. [bar]: /url2
  6223. .
  6224. <p><a href="/url2">foo</a></p>
  6225. ````````````````````````````````
  6226. ```````````````````````````````` example
  6227. [foo][]
  6228. [foo]: /url1
  6229. .
  6230. <p><a href="/url1">foo</a></p>
  6231. ````````````````````````````````
  6232. Inline links also take precedence:
  6233. ```````````````````````````````` example
  6234. [foo]()
  6235. [foo]: /url1
  6236. .
  6237. <p><a href="">foo</a></p>
  6238. ````````````````````````````````
  6239. ```````````````````````````````` example
  6240. [foo](not a link)
  6241. [foo]: /url1
  6242. .
  6243. <p><a href="/url1">foo</a>(not a link)</p>
  6244. ````````````````````````````````
  6245. In the following case `[bar][baz]` is parsed as a reference,
  6246. `[foo]` as normal text:
  6247. ```````````````````````````````` example
  6248. [foo][bar][baz]
  6249. [baz]: /url
  6250. .
  6251. <p>[foo]<a href="/url">bar</a></p>
  6252. ````````````````````````````````
  6253. Here, though, `[foo][bar]` is parsed as a reference, since
  6254. `[bar]` is defined:
  6255. ```````````````````````````````` example
  6256. [foo][bar][baz]
  6257. [baz]: /url1
  6258. [bar]: /url2
  6259. .
  6260. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6261. ````````````````````````````````
  6262. Here `[foo]` is not parsed as a shortcut reference, because it
  6263. is followed by a link label (even though `[bar]` is not defined):
  6264. ```````````````````````````````` example
  6265. [foo][bar][baz]
  6266. [baz]: /url1
  6267. [foo]: /url2
  6268. .
  6269. <p>[foo]<a href="/url1">bar</a></p>
  6270. ````````````````````````````````
  6271. ## Images
  6272. Syntax for images is like the syntax for links, with one
  6273. difference. Instead of [link text], we have an
  6274. [image description](@). The rules for this are the
  6275. same as for [link text], except that (a) an
  6276. image description starts with `![` rather than `[`, and
  6277. (b) an image description may contain links.
  6278. An image description has inline elements
  6279. as its contents. When an image is rendered to HTML,
  6280. this is standardly used as the image's `alt` attribute.
  6281. ```````````````````````````````` example
  6282. ![foo](/url "title")
  6283. .
  6284. <p><img src="/url" alt="foo" title="title" /></p>
  6285. ````````````````````````````````
  6286. ```````````````````````````````` example
  6287. ![foo *bar*]
  6288. [foo *bar*]: train.jpg "train & tracks"
  6289. .
  6290. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6291. ````````````````````````````````
  6292. ```````````````````````````````` example
  6293. ![foo ![bar](/url)](/url2)
  6294. .
  6295. <p><img src="/url2" alt="foo bar" /></p>
  6296. ````````````````````````````````
  6297. ```````````````````````````````` example
  6298. ![foo [bar](/url)](/url2)
  6299. .
  6300. <p><img src="/url2" alt="foo bar" /></p>
  6301. ````````````````````````````````
  6302. Though this spec is concerned with parsing, not rendering, it is
  6303. recommended that in rendering to HTML, only the plain string content
  6304. of the [image description] be used. Note that in
  6305. the above example, the alt attribute's value is `foo bar`, not `foo
  6306. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6307. content is rendered, without formatting.
  6308. ```````````````````````````````` example
  6309. ![foo *bar*][]
  6310. [foo *bar*]: train.jpg "train & tracks"
  6311. .
  6312. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6313. ````````````````````````````````
  6314. ```````````````````````````````` example
  6315. ![foo *bar*][foobar]
  6316. [FOOBAR]: train.jpg "train & tracks"
  6317. .
  6318. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6319. ````````````````````````````````
  6320. ```````````````````````````````` example
  6321. ![foo](train.jpg)
  6322. .
  6323. <p><img src="train.jpg" alt="foo" /></p>
  6324. ````````````````````````````````
  6325. ```````````````````````````````` example
  6326. My ![foo bar](/path/to/train.jpg "title" )
  6327. .
  6328. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6329. ````````````````````````````````
  6330. ```````````````````````````````` example
  6331. ![foo](<url>)
  6332. .
  6333. <p><img src="url" alt="foo" /></p>
  6334. ````````````````````````````````
  6335. ```````````````````````````````` example
  6336. ![](/url)
  6337. .
  6338. <p><img src="/url" alt="" /></p>
  6339. ````````````````````````````````
  6340. Reference-style:
  6341. ```````````````````````````````` example
  6342. ![foo][bar]
  6343. [bar]: /url
  6344. .
  6345. <p><img src="/url" alt="foo" /></p>
  6346. ````````````````````````````````
  6347. ```````````````````````````````` example
  6348. ![foo][bar]
  6349. [BAR]: /url
  6350. .
  6351. <p><img src="/url" alt="foo" /></p>
  6352. ````````````````````````````````
  6353. Collapsed:
  6354. ```````````````````````````````` example
  6355. ![foo][]
  6356. [foo]: /url "title"
  6357. .
  6358. <p><img src="/url" alt="foo" title="title" /></p>
  6359. ````````````````````````````````
  6360. ```````````````````````````````` example
  6361. ![*foo* bar][]
  6362. [*foo* bar]: /url "title"
  6363. .
  6364. <p><img src="/url" alt="foo bar" title="title" /></p>
  6365. ````````````````````````````````
  6366. The labels are case-insensitive:
  6367. ```````````````````````````````` example
  6368. ![Foo][]
  6369. [foo]: /url "title"
  6370. .
  6371. <p><img src="/url" alt="Foo" title="title" /></p>
  6372. ````````````````````````````````
  6373. As with reference links, [whitespace] is not allowed
  6374. between the two sets of brackets:
  6375. ```````````````````````````````` example
  6376. ![foo]
  6377. []
  6378. [foo]: /url "title"
  6379. .
  6380. <p><img src="/url" alt="foo" title="title" />
  6381. []</p>
  6382. ````````````````````````````````
  6383. Shortcut:
  6384. ```````````````````````````````` example
  6385. ![foo]
  6386. [foo]: /url "title"
  6387. .
  6388. <p><img src="/url" alt="foo" title="title" /></p>
  6389. ````````````````````````````````
  6390. ```````````````````````````````` example
  6391. ![*foo* bar]
  6392. [*foo* bar]: /url "title"
  6393. .
  6394. <p><img src="/url" alt="foo bar" title="title" /></p>
  6395. ````````````````````````````````
  6396. Note that link labels cannot contain unescaped brackets:
  6397. ```````````````````````````````` example
  6398. ![[foo]]
  6399. [[foo]]: /url "title"
  6400. .
  6401. <p>![[foo]]</p>
  6402. <p>[[foo]]: /url &quot;title&quot;</p>
  6403. ````````````````````````````````
  6404. The link labels are case-insensitive:
  6405. ```````````````````````````````` example
  6406. ![Foo]
  6407. [foo]: /url "title"
  6408. .
  6409. <p><img src="/url" alt="Foo" title="title" /></p>
  6410. ````````````````````````````````
  6411. If you just want a literal `!` followed by bracketed text, you can
  6412. backslash-escape the opening `[`:
  6413. ```````````````````````````````` example
  6414. !\[foo]
  6415. [foo]: /url "title"
  6416. .
  6417. <p>![foo]</p>
  6418. ````````````````````````````````
  6419. If you want a link after a literal `!`, backslash-escape the
  6420. `!`:
  6421. ```````````````````````````````` example
  6422. \![foo]
  6423. [foo]: /url "title"
  6424. .
  6425. <p>!<a href="/url" title="title">foo</a></p>
  6426. ````````````````````````````````
  6427. ## Autolinks
  6428. [Autolink](@)s are absolute URIs and email addresses inside
  6429. `<` and `>`. They are parsed as links, with the URL or email address
  6430. as the link label.
  6431. A [URI autolink](@) consists of `<`, followed by an
  6432. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6433. a link to the URI, with the URI as the link's label.
  6434. An [absolute URI](@),
  6435. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6436. followed by zero or more characters other than ASCII
  6437. [whitespace] and control characters, `<`, and `>`. If
  6438. the URI includes these characters, they must be percent-encoded
  6439. (e.g. `%20` for a space).
  6440. For purposes of this spec, a [scheme](@) is any sequence
  6441. of 2--32 characters beginning with an ASCII letter and followed
  6442. by any combination of ASCII letters, digits, or the symbols plus
  6443. ("+"), period ("."), or hyphen ("-").
  6444. Here are some valid autolinks:
  6445. ```````````````````````````````` example
  6446. <http://foo.bar.baz>
  6447. .
  6448. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6449. ````````````````````````````````
  6450. ```````````````````````````````` example
  6451. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6452. .
  6453. <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>
  6454. ````````````````````````````````
  6455. ```````````````````````````````` example
  6456. <irc://foo.bar:2233/baz>
  6457. .
  6458. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6459. ````````````````````````````````
  6460. Uppercase is also fine:
  6461. ```````````````````````````````` example
  6462. <MAILTO:FOO@BAR.BAZ>
  6463. .
  6464. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6465. ````````````````````````````````
  6466. Note that many strings that count as [absolute URIs] for
  6467. purposes of this spec are not valid URIs, because their
  6468. schemes are not registered or because of other problems
  6469. with their syntax:
  6470. ```````````````````````````````` example
  6471. <a+b+c:d>
  6472. .
  6473. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6474. ````````````````````````````````
  6475. ```````````````````````````````` example
  6476. <made-up-scheme://foo,bar>
  6477. .
  6478. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6479. ````````````````````````````````
  6480. ```````````````````````````````` example
  6481. <http://../>
  6482. .
  6483. <p><a href="http://../">http://../</a></p>
  6484. ````````````````````````````````
  6485. ```````````````````````````````` example
  6486. <localhost:5001/foo>
  6487. .
  6488. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6489. ````````````````````````````````
  6490. Spaces are not allowed in autolinks:
  6491. ```````````````````````````````` example
  6492. <http://foo.bar/baz bim>
  6493. .
  6494. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6495. ````````````````````````````````
  6496. Backslash-escapes do not work inside autolinks:
  6497. ```````````````````````````````` example
  6498. <http://example.com/\[\>
  6499. .
  6500. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6501. ````````````````````````````````
  6502. An [email autolink](@)
  6503. consists of `<`, followed by an [email address],
  6504. followed by `>`. The link's label is the email address,
  6505. and the URL is `mailto:` followed by the email address.
  6506. An [email address](@),
  6507. for these purposes, is anything that matches
  6508. the [non-normative regex from the HTML5
  6509. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6510. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6511. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6512. Examples of email autolinks:
  6513. ```````````````````````````````` example
  6514. <foo@bar.example.com>
  6515. .
  6516. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6517. ````````````````````````````````
  6518. ```````````````````````````````` example
  6519. <foo+special@Bar.baz-bar0.com>
  6520. .
  6521. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6522. ````````````````````````````````
  6523. Backslash-escapes do not work inside email autolinks:
  6524. ```````````````````````````````` example
  6525. <foo\+@bar.example.com>
  6526. .
  6527. <p>&lt;foo+@bar.example.com&gt;</p>
  6528. ````````````````````````````````
  6529. These are not autolinks:
  6530. ```````````````````````````````` example
  6531. <>
  6532. .
  6533. <p>&lt;&gt;</p>
  6534. ````````````````````````````````
  6535. ```````````````````````````````` example
  6536. < http://foo.bar >
  6537. .
  6538. <p>&lt; http://foo.bar &gt;</p>
  6539. ````````````````````````````````
  6540. ```````````````````````````````` example
  6541. <m:abc>
  6542. .
  6543. <p>&lt;m:abc&gt;</p>
  6544. ````````````````````````````````
  6545. ```````````````````````````````` example
  6546. <foo.bar.baz>
  6547. .
  6548. <p>&lt;foo.bar.baz&gt;</p>
  6549. ````````````````````````````````
  6550. ```````````````````````````````` example
  6551. http://example.com
  6552. .
  6553. <p>http://example.com</p>
  6554. ````````````````````````````````
  6555. ```````````````````````````````` example
  6556. foo@bar.example.com
  6557. .
  6558. <p>foo@bar.example.com</p>
  6559. ````````````````````````````````
  6560. ## Raw HTML
  6561. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6562. raw HTML tag and will be rendered in HTML without escaping.
  6563. Tag and attribute names are not limited to current HTML tags,
  6564. so custom tags (and even, say, DocBook tags) may be used.
  6565. Here is the grammar for tags:
  6566. A [tag name](@) consists of an ASCII letter
  6567. followed by zero or more ASCII letters, digits, or
  6568. hyphens (`-`).
  6569. An [attribute](@) consists of [whitespace],
  6570. an [attribute name], and an optional
  6571. [attribute value specification].
  6572. An [attribute name](@)
  6573. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6574. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6575. specification restricted to ASCII. HTML5 is laxer.)
  6576. An [attribute value specification](@)
  6577. consists of optional [whitespace],
  6578. a `=` character, optional [whitespace], and an [attribute
  6579. value].
  6580. An [attribute value](@)
  6581. consists of an [unquoted attribute value],
  6582. a [single-quoted attribute value], or a [double-quoted attribute value].
  6583. An [unquoted attribute value](@)
  6584. is a nonempty string of characters not
  6585. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6586. A [single-quoted attribute value](@)
  6587. consists of `'`, zero or more
  6588. characters not including `'`, and a final `'`.
  6589. A [double-quoted attribute value](@)
  6590. consists of `"`, zero or more
  6591. characters not including `"`, and a final `"`.
  6592. An [open tag](@) consists of a `<` character, a [tag name],
  6593. zero or more [attributes], optional [whitespace], an optional `/`
  6594. character, and a `>` character.
  6595. A [closing tag](@) consists of the string `</`, a
  6596. [tag name], optional [whitespace], and the character `>`.
  6597. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6598. where *text* does not start with `>` or `->`, does not end with `-`,
  6599. and does not contain `--`. (See the
  6600. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6601. A [processing instruction](@)
  6602. consists of the string `<?`, a string
  6603. of characters not including the string `?>`, and the string
  6604. `?>`.
  6605. A [declaration](@) consists of the
  6606. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6607. [whitespace], a string of characters not including the
  6608. character `>`, and the character `>`.
  6609. A [CDATA section](@) consists of
  6610. the string `<![CDATA[`, a string of characters not including the string
  6611. `]]>`, and the string `]]>`.
  6612. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6613. an [HTML comment], a [processing instruction], a [declaration],
  6614. or a [CDATA section].
  6615. Here are some simple open tags:
  6616. ```````````````````````````````` example
  6617. <a><bab><c2c>
  6618. .
  6619. <p><a><bab><c2c></p>
  6620. ````````````````````````````````
  6621. Empty elements:
  6622. ```````````````````````````````` example
  6623. <a/><b2/>
  6624. .
  6625. <p><a/><b2/></p>
  6626. ````````````````````````````````
  6627. [Whitespace] is allowed:
  6628. ```````````````````````````````` example
  6629. <a /><b2
  6630. data="foo" >
  6631. .
  6632. <p><a /><b2
  6633. data="foo" ></p>
  6634. ````````````````````````````````
  6635. With attributes:
  6636. ```````````````````````````````` example
  6637. <a foo="bar" bam = 'baz <em>"</em>'
  6638. _boolean zoop:33=zoop:33 />
  6639. .
  6640. <p><a foo="bar" bam = 'baz <em>"</em>'
  6641. _boolean zoop:33=zoop:33 /></p>
  6642. ````````````````````````````````
  6643. Custom tag names can be used:
  6644. ```````````````````````````````` example
  6645. Foo <responsive-image src="foo.jpg" />
  6646. .
  6647. <p>Foo <responsive-image src="foo.jpg" /></p>
  6648. ````````````````````````````````
  6649. Illegal tag names, not parsed as HTML:
  6650. ```````````````````````````````` example
  6651. <33> <__>
  6652. .
  6653. <p>&lt;33&gt; &lt;__&gt;</p>
  6654. ````````````````````````````````
  6655. Illegal attribute names:
  6656. ```````````````````````````````` example
  6657. <a h*#ref="hi">
  6658. .
  6659. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6660. ````````````````````````````````
  6661. Illegal attribute values:
  6662. ```````````````````````````````` example
  6663. <a href="hi'> <a href=hi'>
  6664. .
  6665. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6666. ````````````````````````````````
  6667. Illegal [whitespace]:
  6668. ```````````````````````````````` example
  6669. < a><
  6670. foo><bar/ >
  6671. <foo bar=baz
  6672. bim!bop />
  6673. .
  6674. <p>&lt; a&gt;&lt;
  6675. foo&gt;&lt;bar/ &gt;
  6676. &lt;foo bar=baz
  6677. bim!bop /&gt;</p>
  6678. ````````````````````````````````
  6679. Missing [whitespace]:
  6680. ```````````````````````````````` example
  6681. <a href='bar'title=title>
  6682. .
  6683. <p>&lt;a href='bar'title=title&gt;</p>
  6684. ````````````````````````````````
  6685. Closing tags:
  6686. ```````````````````````````````` example
  6687. </a></foo >
  6688. .
  6689. <p></a></foo ></p>
  6690. ````````````````````````````````
  6691. Illegal attributes in closing tag:
  6692. ```````````````````````````````` example
  6693. </a href="foo">
  6694. .
  6695. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6696. ````````````````````````````````
  6697. Comments:
  6698. ```````````````````````````````` example
  6699. foo <!-- this is a
  6700. comment - with hyphen -->
  6701. .
  6702. <p>foo <!-- this is a
  6703. comment - with hyphen --></p>
  6704. ````````````````````````````````
  6705. ```````````````````````````````` example
  6706. foo <!-- not a comment -- two hyphens -->
  6707. .
  6708. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6709. ````````````````````````````````
  6710. Not comments:
  6711. ```````````````````````````````` example
  6712. foo <!--> foo -->
  6713. foo <!-- foo--->
  6714. .
  6715. <p>foo &lt;!--&gt; foo --&gt;</p>
  6716. <p>foo &lt;!-- foo---&gt;</p>
  6717. ````````````````````````````````
  6718. Processing instructions:
  6719. ```````````````````````````````` example
  6720. foo <?php echo $a; ?>
  6721. .
  6722. <p>foo <?php echo $a; ?></p>
  6723. ````````````````````````````````
  6724. Declarations:
  6725. ```````````````````````````````` example
  6726. foo <!ELEMENT br EMPTY>
  6727. .
  6728. <p>foo <!ELEMENT br EMPTY></p>
  6729. ````````````````````````````````
  6730. CDATA sections:
  6731. ```````````````````````````````` example
  6732. foo <![CDATA[>&<]]>
  6733. .
  6734. <p>foo <![CDATA[>&<]]></p>
  6735. ````````````````````````````````
  6736. Entity and numeric character references are preserved in HTML
  6737. attributes:
  6738. ```````````````````````````````` example
  6739. foo <a href="&ouml;">
  6740. .
  6741. <p>foo <a href="&ouml;"></p>
  6742. ````````````````````````````````
  6743. Backslash escapes do not work in HTML attributes:
  6744. ```````````````````````````````` example
  6745. foo <a href="\*">
  6746. .
  6747. <p>foo <a href="\*"></p>
  6748. ````````````````````````````````
  6749. ```````````````````````````````` example
  6750. <a href="\"">
  6751. .
  6752. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6753. ````````````````````````````````
  6754. ## Hard line breaks
  6755. A line break (not in a code span or HTML tag) that is preceded
  6756. by two or more spaces and does not occur at the end of a block
  6757. is parsed as a [hard line break](@) (rendered
  6758. in HTML as a `<br />` tag):
  6759. ```````````````````````````````` example
  6760. foo
  6761. baz
  6762. .
  6763. <p>foo<br />
  6764. baz</p>
  6765. ````````````````````````````````
  6766. For a more visible alternative, a backslash before the
  6767. [line ending] may be used instead of two spaces:
  6768. ```````````````````````````````` example
  6769. foo\
  6770. baz
  6771. .
  6772. <p>foo<br />
  6773. baz</p>
  6774. ````````````````````````````````
  6775. More than two spaces can be used:
  6776. ```````````````````````````````` example
  6777. foo
  6778. baz
  6779. .
  6780. <p>foo<br />
  6781. baz</p>
  6782. ````````````````````````````````
  6783. Leading spaces at the beginning of the next line are ignored:
  6784. ```````````````````````````````` example
  6785. foo
  6786. bar
  6787. .
  6788. <p>foo<br />
  6789. bar</p>
  6790. ````````````````````````````````
  6791. ```````````````````````````````` example
  6792. foo\
  6793. bar
  6794. .
  6795. <p>foo<br />
  6796. bar</p>
  6797. ````````````````````````````````
  6798. Line breaks can occur inside emphasis, links, and other constructs
  6799. that allow inline content:
  6800. ```````````````````````````````` example
  6801. *foo
  6802. bar*
  6803. .
  6804. <p><em>foo<br />
  6805. bar</em></p>
  6806. ````````````````````````````````
  6807. ```````````````````````````````` example
  6808. *foo\
  6809. bar*
  6810. .
  6811. <p><em>foo<br />
  6812. bar</em></p>
  6813. ````````````````````````````````
  6814. Line breaks do not occur inside code spans
  6815. ```````````````````````````````` example
  6816. `code
  6817. span`
  6818. .
  6819. <p><code>code span</code></p>
  6820. ````````````````````````````````
  6821. ```````````````````````````````` example
  6822. `code\
  6823. span`
  6824. .
  6825. <p><code>code\ span</code></p>
  6826. ````````````````````````````````
  6827. or HTML tags:
  6828. ```````````````````````````````` example
  6829. <a href="foo
  6830. bar">
  6831. .
  6832. <p><a href="foo
  6833. bar"></p>
  6834. ````````````````````````````````
  6835. ```````````````````````````````` example
  6836. <a href="foo\
  6837. bar">
  6838. .
  6839. <p><a href="foo\
  6840. bar"></p>
  6841. ````````````````````````````````
  6842. Hard line breaks are for separating inline content within a block.
  6843. Neither syntax for hard line breaks works at the end of a paragraph or
  6844. other block element:
  6845. ```````````````````````````````` example
  6846. foo\
  6847. .
  6848. <p>foo\</p>
  6849. ````````````````````````````````
  6850. ```````````````````````````````` example
  6851. foo
  6852. .
  6853. <p>foo</p>
  6854. ````````````````````````````````
  6855. ```````````````````````````````` example
  6856. ### foo\
  6857. .
  6858. <h3>foo\</h3>
  6859. ````````````````````````````````
  6860. ```````````````````````````````` example
  6861. ### foo
  6862. .
  6863. <h3>foo</h3>
  6864. ````````````````````````````````
  6865. ## Soft line breaks
  6866. A regular line break (not in a code span or HTML tag) that is not
  6867. preceded by two or more spaces or a backslash is parsed as a
  6868. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6869. [line ending] or as a space. The result will be the same in
  6870. browsers. In the examples here, a [line ending] will be used.)
  6871. ```````````````````````````````` example
  6872. foo
  6873. baz
  6874. .
  6875. <p>foo
  6876. baz</p>
  6877. ````````````````````````````````
  6878. Spaces at the end of the line and beginning of the next line are
  6879. removed:
  6880. ```````````````````````````````` example
  6881. foo
  6882. baz
  6883. .
  6884. <p>foo
  6885. baz</p>
  6886. ````````````````````````````````
  6887. A conforming parser may render a soft line break in HTML either as a
  6888. line break or as a space.
  6889. A renderer may also provide an option to render soft line breaks
  6890. as hard line breaks.
  6891. ## Textual content
  6892. Any characters not given an interpretation by the above rules will
  6893. be parsed as plain textual content.
  6894. ```````````````````````````````` example
  6895. hello $.;'there
  6896. .
  6897. <p>hello $.;'there</p>
  6898. ````````````````````````````````
  6899. ```````````````````````````````` example
  6900. Foo χρῆν
  6901. .
  6902. <p>Foo χρῆν</p>
  6903. ````````````````````````````````
  6904. Internal spaces are preserved verbatim:
  6905. ```````````````````````````````` example
  6906. Multiple spaces
  6907. .
  6908. <p>Multiple spaces</p>
  6909. ````````````````````````````````
  6910. <!-- END TESTS -->
  6911. # Appendix: A parsing strategy
  6912. In this appendix we describe some features of the parsing strategy
  6913. used in the CommonMark reference implementations.
  6914. ## Overview
  6915. Parsing has two phases:
  6916. 1. In the first phase, lines of input are consumed and the block
  6917. structure of the document---its division into paragraphs, block quotes,
  6918. list items, and so on---is constructed. Text is assigned to these
  6919. blocks but not parsed. Link reference definitions are parsed and a
  6920. map of links is constructed.
  6921. 2. In the second phase, the raw text contents of paragraphs and headings
  6922. are parsed into sequences of Markdown inline elements (strings,
  6923. code spans, links, emphasis, and so on), using the map of link
  6924. references constructed in phase 1.
  6925. At each point in processing, the document is represented as a tree of
  6926. **blocks**. The root of the tree is a `document` block. The `document`
  6927. may have any number of other blocks as **children**. These children
  6928. may, in turn, have other blocks as children. The last child of a block
  6929. is normally considered **open**, meaning that subsequent lines of input
  6930. can alter its contents. (Blocks that are not open are **closed**.)
  6931. Here, for example, is a possible document tree, with the open blocks
  6932. marked by arrows:
  6933. ``` tree
  6934. -> document
  6935. -> block_quote
  6936. paragraph
  6937. "Lorem ipsum dolor\nsit amet."
  6938. -> list (type=bullet tight=true bullet_char=-)
  6939. list_item
  6940. paragraph
  6941. "Qui *quodsi iracundia*"
  6942. -> list_item
  6943. -> paragraph
  6944. "aliquando id"
  6945. ```
  6946. ## Phase 1: block structure
  6947. Each line that is processed has an effect on this tree. The line is
  6948. analyzed and, depending on its contents, the document may be altered
  6949. in one or more of the following ways:
  6950. 1. One or more open blocks may be closed.
  6951. 2. One or more new blocks may be created as children of the
  6952. last open block.
  6953. 3. Text may be added to the last (deepest) open block remaining
  6954. on the tree.
  6955. Once a line has been incorporated into the tree in this way,
  6956. it can be discarded, so input can be read in a stream.
  6957. For each line, we follow this procedure:
  6958. 1. First we iterate through the open blocks, starting with the
  6959. root document, and descending through last children down to the last
  6960. open block. Each block imposes a condition that the line must satisfy
  6961. if the block is to remain open. For example, a block quote requires a
  6962. `>` character. A paragraph requires a non-blank line.
  6963. In this phase we may match all or just some of the open
  6964. blocks. But we cannot close unmatched blocks yet, because we may have a
  6965. [lazy continuation line].
  6966. 2. Next, after consuming the continuation markers for existing
  6967. blocks, we look for new block starts (e.g. `>` for a block quote).
  6968. If we encounter a new block start, we close any blocks unmatched
  6969. in step 1 before creating the new block as a child of the last
  6970. matched block.
  6971. 3. Finally, we look at the remainder of the line (after block
  6972. markers like `>`, list markers, and indentation have been consumed).
  6973. This is text that can be incorporated into the last open
  6974. block (a paragraph, code block, heading, or raw HTML).
  6975. Setext headings are formed when we see a line of a paragraph
  6976. that is a [setext heading underline].
  6977. Reference link definitions are detected when a paragraph is closed;
  6978. the accumulated text lines are parsed to see if they begin with
  6979. one or more reference link definitions. Any remainder becomes a
  6980. normal paragraph.
  6981. We can see how this works by considering how the tree above is
  6982. generated by four lines of Markdown:
  6983. ``` markdown
  6984. > Lorem ipsum dolor
  6985. sit amet.
  6986. > - Qui *quodsi iracundia*
  6987. > - aliquando id
  6988. ```
  6989. At the outset, our document model is just
  6990. ``` tree
  6991. -> document
  6992. ```
  6993. The first line of our text,
  6994. ``` markdown
  6995. > Lorem ipsum dolor
  6996. ```
  6997. causes a `block_quote` block to be created as a child of our
  6998. open `document` block, and a `paragraph` block as a child of
  6999. the `block_quote`. Then the text is added to the last open
  7000. block, the `paragraph`:
  7001. ``` tree
  7002. -> document
  7003. -> block_quote
  7004. -> paragraph
  7005. "Lorem ipsum dolor"
  7006. ```
  7007. The next line,
  7008. ``` markdown
  7009. sit amet.
  7010. ```
  7011. is a "lazy continuation" of the open `paragraph`, so it gets added
  7012. to the paragraph's text:
  7013. ``` tree
  7014. -> document
  7015. -> block_quote
  7016. -> paragraph
  7017. "Lorem ipsum dolor\nsit amet."
  7018. ```
  7019. The third line,
  7020. ``` markdown
  7021. > - Qui *quodsi iracundia*
  7022. ```
  7023. causes the `paragraph` block to be closed, and a new `list` block
  7024. opened as a child of the `block_quote`. A `list_item` is also
  7025. added as a child of the `list`, and a `paragraph` as a child of
  7026. the `list_item`. The text is then added to the new `paragraph`:
  7027. ``` tree
  7028. -> document
  7029. -> block_quote
  7030. paragraph
  7031. "Lorem ipsum dolor\nsit amet."
  7032. -> list (type=bullet tight=true bullet_char=-)
  7033. -> list_item
  7034. -> paragraph
  7035. "Qui *quodsi iracundia*"
  7036. ```
  7037. The fourth line,
  7038. ``` markdown
  7039. > - aliquando id
  7040. ```
  7041. causes the `list_item` (and its child the `paragraph`) to be closed,
  7042. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7043. is added as a child of the new `list_item`, to contain the text.
  7044. We thus obtain the final tree:
  7045. ``` tree
  7046. -> document
  7047. -> block_quote
  7048. paragraph
  7049. "Lorem ipsum dolor\nsit amet."
  7050. -> list (type=bullet tight=true bullet_char=-)
  7051. list_item
  7052. paragraph
  7053. "Qui *quodsi iracundia*"
  7054. -> list_item
  7055. -> paragraph
  7056. "aliquando id"
  7057. ```
  7058. ## Phase 2: inline structure
  7059. Once all of the input has been parsed, all open blocks are closed.
  7060. We then "walk the tree," visiting every node, and parse raw
  7061. string contents of paragraphs and headings as inlines. At this
  7062. point we have seen all the link reference definitions, so we can
  7063. resolve reference links as we go.
  7064. ``` tree
  7065. document
  7066. block_quote
  7067. paragraph
  7068. str "Lorem ipsum dolor"
  7069. softbreak
  7070. str "sit amet."
  7071. list (type=bullet tight=true bullet_char=-)
  7072. list_item
  7073. paragraph
  7074. str "Qui "
  7075. emph
  7076. str "quodsi iracundia"
  7077. list_item
  7078. paragraph
  7079. str "aliquando id"
  7080. ```
  7081. Notice how the [line ending] in the first paragraph has
  7082. been parsed as a `softbreak`, and the asterisks in the first list item
  7083. have become an `emph`.
  7084. ### An algorithm for parsing nested emphasis and links
  7085. By far the trickiest part of inline parsing is handling emphasis,
  7086. strong emphasis, links, and images. This is done using the following
  7087. algorithm.
  7088. When we're parsing inlines and we hit either
  7089. - a run of `*` or `_` characters, or
  7090. - a `[` or `![`
  7091. we insert a text node with these symbols as its literal content, and we
  7092. add a pointer to this text node to the [delimiter stack](@).
  7093. The [delimiter stack] is a doubly linked list. Each
  7094. element contains a pointer to a text node, plus information about
  7095. - the type of delimiter (`[`, `![`, `*`, `_`)
  7096. - the number of delimiters,
  7097. - whether the delimiter is "active" (all are active to start), and
  7098. - whether the delimiter is a potential opener, a potential closer,
  7099. or both (which depends on what sort of characters precede
  7100. and follow the delimiters).
  7101. When we hit a `]` character, we call the *look for link or image*
  7102. procedure (see below).
  7103. When we hit the end of the input, we call the *process emphasis*
  7104. procedure (see below), with `stack_bottom` = NULL.
  7105. #### *look for link or image*
  7106. Starting at the top of the delimiter stack, we look backwards
  7107. through the stack for an opening `[` or `![` delimiter.
  7108. - If we don't find one, we return a literal text node `]`.
  7109. - If we do find one, but it's not *active*, we remove the inactive
  7110. delimiter from the stack, and return a literal text node `]`.
  7111. - If we find one and it's active, then we parse ahead to see if
  7112. we have an inline link/image, reference link/image, compact reference
  7113. link/image, or shortcut reference link/image.
  7114. + If we don't, then we remove the opening delimiter from the
  7115. delimiter stack and return a literal text node `]`.
  7116. + If we do, then
  7117. * We return a link or image node whose children are the inlines
  7118. after the text node pointed to by the opening delimiter.
  7119. * We run *process emphasis* on these inlines, with the `[` opener
  7120. as `stack_bottom`.
  7121. * We remove the opening delimiter.
  7122. * If we have a link (and not an image), we also set all
  7123. `[` delimiters before the opening delimiter to *inactive*. (This
  7124. will prevent us from getting links within links.)
  7125. #### *process emphasis*
  7126. Parameter `stack_bottom` sets a lower bound to how far we
  7127. descend in the [delimiter stack]. If it is NULL, we can
  7128. go all the way to the bottom. Otherwise, we stop before
  7129. visiting `stack_bottom`.
  7130. Let `current_position` point to the element on the [delimiter stack]
  7131. just above `stack_bottom` (or the first element if `stack_bottom`
  7132. is NULL).
  7133. We keep track of the `openers_bottom` for each delimiter
  7134. type (`*`, `_`) and delimiter length (modulo 3). Initialize
  7135. this to `stack_bottom`.
  7136. Then we repeat the following until we run out of potential
  7137. closers:
  7138. - Move `current_position` forward in the delimiter stack (if needed)
  7139. until we find the first potential closer with delimiter `*` or `_`.
  7140. (This will be the potential closer closest
  7141. to the beginning of the input -- the first one in parse order.)
  7142. - Now, look back in the stack (staying above `stack_bottom` and
  7143. the `openers_bottom` for this delimiter type) for the
  7144. first matching potential opener ("matching" means same delimiter).
  7145. - If one is found:
  7146. + Figure out whether we have emphasis or strong emphasis:
  7147. if both closer and opener spans have length >= 2, we have
  7148. strong, otherwise regular.
  7149. + Insert an emph or strong emph node accordingly, after
  7150. the text node corresponding to the opener.
  7151. + Remove any delimiters between the opener and closer from
  7152. the delimiter stack.
  7153. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7154. from the opening and closing text nodes. If they become empty
  7155. as a result, remove them and remove the corresponding element
  7156. of the delimiter stack. If the closing node is removed, reset
  7157. `current_position` to the next element in the stack.
  7158. - If none in found:
  7159. + Set `openers_bottom` to the element before `current_position`.
  7160. (We know that there are no openers for this kind of closer up to and
  7161. including this point, so this puts a lower bound on future searches.)
  7162. + If the closer at `current_position` is not a potential opener,
  7163. remove it from the delimiter stack (since we know it can't
  7164. be a closer either).
  7165. + Advance `current_position` to the next element in the stack.
  7166. After we're done, we remove all delimiters above `stack_bottom` from the
  7167. delimiter stack.