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