aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 84b97af90e0330c1181e6f541a000e7da627234f (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.29
  5. date: '2019-04-06'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a GitHub wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  252. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  253. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  254. `{`, `|`, `}`, or `~` (U+007B–007E).
  255. A [punctuation character](@) is an [ASCII
  256. punctuation character] or anything in
  257. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  258. ## Tabs
  259. Tabs in lines are not expanded to [spaces]. However,
  260. in contexts where whitespace helps to define block structure,
  261. tabs behave as if they were replaced by spaces with a tab stop
  262. of 4 characters.
  263. Thus, for example, a tab can be used instead of four spaces
  264. in an indented code block. (Note, however, that internal
  265. tabs are passed through as literal tabs, not expanded to
  266. spaces.)
  267. ```````````````````````````````` example
  268. →foo→baz→→bim
  269. .
  270. <pre><code>foo→baz→→bim
  271. </code></pre>
  272. ````````````````````````````````
  273. ```````````````````````````````` example
  274. →foo→baz→→bim
  275. .
  276. <pre><code>foo→baz→→bim
  277. </code></pre>
  278. ````````````````````````````````
  279. ```````````````````````````````` example
  280. a→a
  281. ὐ→a
  282. .
  283. <pre><code>a→a
  284. ὐ→a
  285. </code></pre>
  286. ````````````````````````````````
  287. In the following example, a continuation paragraph of a list
  288. item is indented with a tab; this has exactly the same effect
  289. as indentation with four spaces would:
  290. ```````````````````````````````` example
  291. - foo
  292. →bar
  293. .
  294. <ul>
  295. <li>
  296. <p>foo</p>
  297. <p>bar</p>
  298. </li>
  299. </ul>
  300. ````````````````````````````````
  301. ```````````````````````````````` example
  302. - foo
  303. →→bar
  304. .
  305. <ul>
  306. <li>
  307. <p>foo</p>
  308. <pre><code> bar
  309. </code></pre>
  310. </li>
  311. </ul>
  312. ````````````````````````````````
  313. Normally the `>` that begins a block quote may be followed
  314. optionally by a space, which is not considered part of the
  315. content. In the following case `>` is followed by a tab,
  316. which is treated as if it were expanded into three spaces.
  317. Since one of these spaces is considered part of the
  318. delimiter, `foo` is considered to be indented six spaces
  319. inside the block quote context, so we get an indented
  320. code block starting with two spaces.
  321. ```````````````````````````````` example
  322. >→→foo
  323. .
  324. <blockquote>
  325. <pre><code> foo
  326. </code></pre>
  327. </blockquote>
  328. ````````````````````````````````
  329. ```````````````````````````````` example
  330. -→→foo
  331. .
  332. <ul>
  333. <li>
  334. <pre><code> foo
  335. </code></pre>
  336. </li>
  337. </ul>
  338. ````````````````````````````````
  339. ```````````````````````````````` example
  340. foo
  341. →bar
  342. .
  343. <pre><code>foo
  344. bar
  345. </code></pre>
  346. ````````````````````````````````
  347. ```````````````````````````````` example
  348. - foo
  349. - bar
  350. → - baz
  351. .
  352. <ul>
  353. <li>foo
  354. <ul>
  355. <li>bar
  356. <ul>
  357. <li>baz</li>
  358. </ul>
  359. </li>
  360. </ul>
  361. </li>
  362. </ul>
  363. ````````````````````````````````
  364. ```````````````````````````````` example
  365. #→Foo
  366. .
  367. <h1>Foo</h1>
  368. ````````````````````````````````
  369. ```````````````````````````````` example
  370. *→*→*→
  371. .
  372. <hr />
  373. ````````````````````````````````
  374. ## Insecure characters
  375. For security reasons, the Unicode character `U+0000` must be replaced
  376. with the REPLACEMENT CHARACTER (`U+FFFD`).
  377. # Blocks and inlines
  378. We can think of a document as a sequence of
  379. [blocks](@)---structural elements like paragraphs, block
  380. quotations, lists, headings, rules, and code blocks. Some blocks (like
  381. block quotes and list items) contain other blocks; others (like
  382. headings and paragraphs) contain [inline](@) content---text,
  383. links, emphasized text, images, code spans, and so on.
  384. ## Precedence
  385. Indicators of block structure always take precedence over indicators
  386. of inline structure. So, for example, the following is a list with
  387. two items, not a list with one item containing a code span:
  388. ```````````````````````````````` example
  389. - `one
  390. - two`
  391. .
  392. <ul>
  393. <li>`one</li>
  394. <li>two`</li>
  395. </ul>
  396. ````````````````````````````````
  397. This means that parsing can proceed in two steps: first, the block
  398. structure of the document can be discerned; second, text lines inside
  399. paragraphs, headings, and other block constructs can be parsed for inline
  400. structure. The second step requires information about link reference
  401. definitions that will be available only at the end of the first
  402. step. Note that the first step requires processing lines in sequence,
  403. but the second can be parallelized, since the inline parsing of
  404. one block element does not affect the inline parsing of any other.
  405. ## Container blocks and leaf blocks
  406. We can divide blocks into two types:
  407. [container blocks](@),
  408. which can contain other blocks, and [leaf blocks](@),
  409. which cannot.
  410. # Leaf blocks
  411. This section describes the different kinds of leaf block that make up a
  412. Markdown document.
  413. ## Thematic breaks
  414. A line consisting of 0-3 spaces of indentation, followed by a sequence
  415. of three or more matching `-`, `_`, or `*` characters, each followed
  416. optionally by any number of spaces or tabs, forms a
  417. [thematic break](@).
  418. ```````````````````````````````` example
  419. ***
  420. ---
  421. ___
  422. .
  423. <hr />
  424. <hr />
  425. <hr />
  426. ````````````````````````````````
  427. Wrong characters:
  428. ```````````````````````````````` example
  429. +++
  430. .
  431. <p>+++</p>
  432. ````````````````````````````````
  433. ```````````````````````````````` example
  434. ===
  435. .
  436. <p>===</p>
  437. ````````````````````````````````
  438. Not enough characters:
  439. ```````````````````````````````` example
  440. --
  441. **
  442. __
  443. .
  444. <p>--
  445. **
  446. __</p>
  447. ````````````````````````````````
  448. One to three spaces indent are allowed:
  449. ```````````````````````````````` example
  450. ***
  451. ***
  452. ***
  453. .
  454. <hr />
  455. <hr />
  456. <hr />
  457. ````````````````````````````````
  458. Four spaces is too many:
  459. ```````````````````````````````` example
  460. ***
  461. .
  462. <pre><code>***
  463. </code></pre>
  464. ````````````````````````````````
  465. ```````````````````````````````` example
  466. Foo
  467. ***
  468. .
  469. <p>Foo
  470. ***</p>
  471. ````````````````````````````````
  472. More than three characters may be used:
  473. ```````````````````````````````` example
  474. _____________________________________
  475. .
  476. <hr />
  477. ````````````````````````````````
  478. Spaces are allowed between the characters:
  479. ```````````````````````````````` example
  480. - - -
  481. .
  482. <hr />
  483. ````````````````````````````````
  484. ```````````````````````````````` example
  485. ** * ** * ** * **
  486. .
  487. <hr />
  488. ````````````````````````````````
  489. ```````````````````````````````` example
  490. - - - -
  491. .
  492. <hr />
  493. ````````````````````````````````
  494. Spaces are allowed at the end:
  495. ```````````````````````````````` example
  496. - - - -
  497. .
  498. <hr />
  499. ````````````````````````````````
  500. However, no other characters may occur in the line:
  501. ```````````````````````````````` example
  502. _ _ _ _ a
  503. a------
  504. ---a---
  505. .
  506. <p>_ _ _ _ a</p>
  507. <p>a------</p>
  508. <p>---a---</p>
  509. ````````````````````````````````
  510. It is required that all of the [non-whitespace characters] be the same.
  511. So, this is not a thematic break:
  512. ```````````````````````````````` example
  513. *-*
  514. .
  515. <p><em>-</em></p>
  516. ````````````````````````````````
  517. Thematic breaks do not need blank lines before or after:
  518. ```````````````````````````````` example
  519. - foo
  520. ***
  521. - bar
  522. .
  523. <ul>
  524. <li>foo</li>
  525. </ul>
  526. <hr />
  527. <ul>
  528. <li>bar</li>
  529. </ul>
  530. ````````````````````````````````
  531. Thematic breaks can interrupt a paragraph:
  532. ```````````````````````````````` example
  533. Foo
  534. ***
  535. bar
  536. .
  537. <p>Foo</p>
  538. <hr />
  539. <p>bar</p>
  540. ````````````````````````````````
  541. If a line of dashes that meets the above conditions for being a
  542. thematic break could also be interpreted as the underline of a [setext
  543. heading], the interpretation as a
  544. [setext heading] takes precedence. Thus, for example,
  545. this is a setext heading, not a paragraph followed by a thematic break:
  546. ```````````````````````````````` example
  547. Foo
  548. ---
  549. bar
  550. .
  551. <h2>Foo</h2>
  552. <p>bar</p>
  553. ````````````````````````````````
  554. When both a thematic break and a list item are possible
  555. interpretations of a line, the thematic break takes precedence:
  556. ```````````````````````````````` example
  557. * Foo
  558. * * *
  559. * Bar
  560. .
  561. <ul>
  562. <li>Foo</li>
  563. </ul>
  564. <hr />
  565. <ul>
  566. <li>Bar</li>
  567. </ul>
  568. ````````````````````````````````
  569. If you want a thematic break in a list item, use a different bullet:
  570. ```````````````````````````````` example
  571. - Foo
  572. - * * *
  573. .
  574. <ul>
  575. <li>Foo</li>
  576. <li>
  577. <hr />
  578. </li>
  579. </ul>
  580. ````````````````````````````````
  581. ## ATX headings
  582. An [ATX heading](@)
  583. consists of a string of characters, parsed as inline content, between an
  584. opening sequence of 1--6 unescaped `#` characters and an optional
  585. closing sequence of any number of unescaped `#` characters.
  586. The opening sequence of `#` characters must be followed by a
  587. [space] or by the end of line. The optional closing sequence of `#`s must be
  588. preceded by a [space] and may be followed by spaces only. The opening
  589. `#` character may be indented 0-3 spaces. The raw contents of the
  590. heading are stripped of leading and trailing spaces before being parsed
  591. as inline content. The heading level is equal to the number of `#`
  592. characters in the opening sequence.
  593. Simple headings:
  594. ```````````````````````````````` example
  595. # foo
  596. ## foo
  597. ### foo
  598. #### foo
  599. ##### foo
  600. ###### foo
  601. .
  602. <h1>foo</h1>
  603. <h2>foo</h2>
  604. <h3>foo</h3>
  605. <h4>foo</h4>
  606. <h5>foo</h5>
  607. <h6>foo</h6>
  608. ````````````````````````````````
  609. More than six `#` characters is not a heading:
  610. ```````````````````````````````` example
  611. ####### foo
  612. .
  613. <p>####### foo</p>
  614. ````````````````````````````````
  615. At least one space is required between the `#` characters and the
  616. heading's contents, unless the heading is empty. Note that many
  617. implementations currently do not require the space. However, the
  618. space was required by the
  619. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  620. and it helps prevent things like the following from being parsed as
  621. headings:
  622. ```````````````````````````````` example
  623. #5 bolt
  624. #hashtag
  625. .
  626. <p>#5 bolt</p>
  627. <p>#hashtag</p>
  628. ````````````````````````````````
  629. This is not a heading, because the first `#` is escaped:
  630. ```````````````````````````````` example
  631. \## foo
  632. .
  633. <p>## foo</p>
  634. ````````````````````````````````
  635. Contents are parsed as inlines:
  636. ```````````````````````````````` example
  637. # foo *bar* \*baz\*
  638. .
  639. <h1>foo <em>bar</em> *baz*</h1>
  640. ````````````````````````````````
  641. Leading and trailing [whitespace] is ignored in parsing inline content:
  642. ```````````````````````````````` example
  643. # foo
  644. .
  645. <h1>foo</h1>
  646. ````````````````````````````````
  647. One to three spaces indentation are allowed:
  648. ```````````````````````````````` example
  649. ### foo
  650. ## foo
  651. # foo
  652. .
  653. <h3>foo</h3>
  654. <h2>foo</h2>
  655. <h1>foo</h1>
  656. ````````````````````````````````
  657. Four spaces are too much:
  658. ```````````````````````````````` example
  659. # foo
  660. .
  661. <pre><code># foo
  662. </code></pre>
  663. ````````````````````````````````
  664. ```````````````````````````````` example
  665. foo
  666. # bar
  667. .
  668. <p>foo
  669. # bar</p>
  670. ````````````````````````````````
  671. A closing sequence of `#` characters is optional:
  672. ```````````````````````````````` example
  673. ## foo ##
  674. ### bar ###
  675. .
  676. <h2>foo</h2>
  677. <h3>bar</h3>
  678. ````````````````````````````````
  679. It need not be the same length as the opening sequence:
  680. ```````````````````````````````` example
  681. # foo ##################################
  682. ##### foo ##
  683. .
  684. <h1>foo</h1>
  685. <h5>foo</h5>
  686. ````````````````````````````````
  687. Spaces are allowed after the closing sequence:
  688. ```````````````````````````````` example
  689. ### foo ###
  690. .
  691. <h3>foo</h3>
  692. ````````````````````````````````
  693. A sequence of `#` characters with anything but [spaces] following it
  694. is not a closing sequence, but counts as part of the contents of the
  695. heading:
  696. ```````````````````````````````` example
  697. ### foo ### b
  698. .
  699. <h3>foo ### b</h3>
  700. ````````````````````````````````
  701. The closing sequence must be preceded by a space:
  702. ```````````````````````````````` example
  703. # foo#
  704. .
  705. <h1>foo#</h1>
  706. ````````````````````````````````
  707. Backslash-escaped `#` characters do not count as part
  708. of the closing sequence:
  709. ```````````````````````````````` example
  710. ### foo \###
  711. ## foo #\##
  712. # foo \#
  713. .
  714. <h3>foo ###</h3>
  715. <h2>foo ###</h2>
  716. <h1>foo #</h1>
  717. ````````````````````````````````
  718. ATX headings need not be separated from surrounding content by blank
  719. lines, and they can interrupt paragraphs:
  720. ```````````````````````````````` example
  721. ****
  722. ## foo
  723. ****
  724. .
  725. <hr />
  726. <h2>foo</h2>
  727. <hr />
  728. ````````````````````````````````
  729. ```````````````````````````````` example
  730. Foo bar
  731. # baz
  732. Bar foo
  733. .
  734. <p>Foo bar</p>
  735. <h1>baz</h1>
  736. <p>Bar foo</p>
  737. ````````````````````````````````
  738. ATX headings can be empty:
  739. ```````````````````````````````` example
  740. ##
  741. #
  742. ### ###
  743. .
  744. <h2></h2>
  745. <h1></h1>
  746. <h3></h3>
  747. ````````````````````````````````
  748. ## Setext headings
  749. A [setext heading](@) consists of one or more
  750. lines of text, each containing at least one [non-whitespace
  751. character], with no more than 3 spaces indentation, followed by
  752. a [setext heading underline]. The lines of text must be such
  753. that, were they not followed by the setext heading underline,
  754. they would be interpreted as a paragraph: they cannot be
  755. interpretable as a [code fence], [ATX heading][ATX headings],
  756. [block quote][block quotes], [thematic break][thematic breaks],
  757. [list item][list items], or [HTML block][HTML blocks].
  758. A [setext heading underline](@) is a sequence of
  759. `=` characters or a sequence of `-` characters, with no more than 3
  760. spaces indentation and any number of trailing spaces. If a line
  761. containing a single `-` can be interpreted as an
  762. empty [list items], it should be interpreted this way
  763. and not as a [setext heading underline].
  764. The heading is a level 1 heading if `=` characters are used in
  765. the [setext heading underline], and a level 2 heading if `-`
  766. characters are used. The contents of the heading are the result
  767. of parsing the preceding lines of text as CommonMark inline
  768. content.
  769. In general, a setext heading need not be preceded or followed by a
  770. blank line. However, it cannot interrupt a paragraph, so when a
  771. setext heading comes after a paragraph, a blank line is needed between
  772. them.
  773. Simple examples:
  774. ```````````````````````````````` example
  775. Foo *bar*
  776. =========
  777. Foo *bar*
  778. ---------
  779. .
  780. <h1>Foo <em>bar</em></h1>
  781. <h2>Foo <em>bar</em></h2>
  782. ````````````````````````````````
  783. The content of the header may span more than one line:
  784. ```````````````````````````````` example
  785. Foo *bar
  786. baz*
  787. ====
  788. .
  789. <h1>Foo <em>bar
  790. baz</em></h1>
  791. ````````````````````````````````
  792. The contents are the result of parsing the headings's raw
  793. content as inlines. The heading's raw content is formed by
  794. concatenating the lines and removing initial and final
  795. [whitespace].
  796. ```````````````````````````````` example
  797. Foo *bar
  798. baz*→
  799. ====
  800. .
  801. <h1>Foo <em>bar
  802. baz</em></h1>
  803. ````````````````````````````````
  804. The underlining can be any length:
  805. ```````````````````````````````` example
  806. Foo
  807. -------------------------
  808. Foo
  809. =
  810. .
  811. <h2>Foo</h2>
  812. <h1>Foo</h1>
  813. ````````````````````````````````
  814. The heading content can be indented up to three spaces, and need
  815. not line up with the underlining:
  816. ```````````````````````````````` example
  817. Foo
  818. ---
  819. Foo
  820. -----
  821. Foo
  822. ===
  823. .
  824. <h2>Foo</h2>
  825. <h2>Foo</h2>
  826. <h1>Foo</h1>
  827. ````````````````````````````````
  828. Four spaces indent is too much:
  829. ```````````````````````````````` example
  830. Foo
  831. ---
  832. Foo
  833. ---
  834. .
  835. <pre><code>Foo
  836. ---
  837. Foo
  838. </code></pre>
  839. <hr />
  840. ````````````````````````````````
  841. The setext heading underline can be indented up to three spaces, and
  842. may have trailing spaces:
  843. ```````````````````````````````` example
  844. Foo
  845. ----
  846. .
  847. <h2>Foo</h2>
  848. ````````````````````````````````
  849. Four spaces is too much:
  850. ```````````````````````````````` example
  851. Foo
  852. ---
  853. .
  854. <p>Foo
  855. ---</p>
  856. ````````````````````````````````
  857. The setext heading underline cannot contain internal spaces:
  858. ```````````````````````````````` example
  859. Foo
  860. = =
  861. Foo
  862. --- -
  863. .
  864. <p>Foo
  865. = =</p>
  866. <p>Foo</p>
  867. <hr />
  868. ````````````````````````````````
  869. Trailing spaces in the content line do not cause a line break:
  870. ```````````````````````````````` example
  871. Foo
  872. -----
  873. .
  874. <h2>Foo</h2>
  875. ````````````````````````````````
  876. Nor does a backslash at the end:
  877. ```````````````````````````````` example
  878. Foo\
  879. ----
  880. .
  881. <h2>Foo\</h2>
  882. ````````````````````````````````
  883. Since indicators of block structure take precedence over
  884. indicators of inline structure, the following are setext headings:
  885. ```````````````````````````````` example
  886. `Foo
  887. ----
  888. `
  889. <a title="a lot
  890. ---
  891. of dashes"/>
  892. .
  893. <h2>`Foo</h2>
  894. <p>`</p>
  895. <h2>&lt;a title=&quot;a lot</h2>
  896. <p>of dashes&quot;/&gt;</p>
  897. ````````````````````````````````
  898. The setext heading underline cannot be a [lazy continuation
  899. line] in a list item or block quote:
  900. ```````````````````````````````` example
  901. > Foo
  902. ---
  903. .
  904. <blockquote>
  905. <p>Foo</p>
  906. </blockquote>
  907. <hr />
  908. ````````````````````````````````
  909. ```````````````````````````````` example
  910. > foo
  911. bar
  912. ===
  913. .
  914. <blockquote>
  915. <p>foo
  916. bar
  917. ===</p>
  918. </blockquote>
  919. ````````````````````````````````
  920. ```````````````````````````````` example
  921. - Foo
  922. ---
  923. .
  924. <ul>
  925. <li>Foo</li>
  926. </ul>
  927. <hr />
  928. ````````````````````````````````
  929. A blank line is needed between a paragraph and a following
  930. setext heading, since otherwise the paragraph becomes part
  931. of the heading's content:
  932. ```````````````````````````````` example
  933. Foo
  934. Bar
  935. ---
  936. .
  937. <h2>Foo
  938. Bar</h2>
  939. ````````````````````````````````
  940. But in general a blank line is not required before or after
  941. setext headings:
  942. ```````````````````````````````` example
  943. ---
  944. Foo
  945. ---
  946. Bar
  947. ---
  948. Baz
  949. .
  950. <hr />
  951. <h2>Foo</h2>
  952. <h2>Bar</h2>
  953. <p>Baz</p>
  954. ````````````````````````````````
  955. Setext headings cannot be empty:
  956. ```````````````````````````````` example
  957. ====
  958. .
  959. <p>====</p>
  960. ````````````````````````````````
  961. Setext heading text lines must not be interpretable as block
  962. constructs other than paragraphs. So, the line of dashes
  963. in these examples gets interpreted as a thematic break:
  964. ```````````````````````````````` example
  965. ---
  966. ---
  967. .
  968. <hr />
  969. <hr />
  970. ````````````````````````````````
  971. ```````````````````````````````` example
  972. - foo
  973. -----
  974. .
  975. <ul>
  976. <li>foo</li>
  977. </ul>
  978. <hr />
  979. ````````````````````````````````
  980. ```````````````````````````````` example
  981. foo
  982. ---
  983. .
  984. <pre><code>foo
  985. </code></pre>
  986. <hr />
  987. ````````````````````````````````
  988. ```````````````````````````````` example
  989. > foo
  990. -----
  991. .
  992. <blockquote>
  993. <p>foo</p>
  994. </blockquote>
  995. <hr />
  996. ````````````````````````````````
  997. If you want a heading with `> foo` as its literal text, you can
  998. use backslash escapes:
  999. ```````````````````````````````` example
  1000. \> foo
  1001. ------
  1002. .
  1003. <h2>&gt; foo</h2>
  1004. ````````````````````````````````
  1005. **Compatibility note:** Most existing Markdown implementations
  1006. do not allow the text of setext headings to span multiple lines.
  1007. But there is no consensus about how to interpret
  1008. ``` markdown
  1009. Foo
  1010. bar
  1011. ---
  1012. baz
  1013. ```
  1014. One can find four different interpretations:
  1015. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1016. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1017. 3. paragraph "Foo bar --- baz"
  1018. 4. heading "Foo bar", paragraph "baz"
  1019. We find interpretation 4 most natural, and interpretation 4
  1020. increases the expressive power of CommonMark, by allowing
  1021. multiline headings. Authors who want interpretation 1 can
  1022. put a blank line after the first paragraph:
  1023. ```````````````````````````````` example
  1024. Foo
  1025. bar
  1026. ---
  1027. baz
  1028. .
  1029. <p>Foo</p>
  1030. <h2>bar</h2>
  1031. <p>baz</p>
  1032. ````````````````````````````````
  1033. Authors who want interpretation 2 can put blank lines around
  1034. the thematic break,
  1035. ```````````````````````````````` example
  1036. Foo
  1037. bar
  1038. ---
  1039. baz
  1040. .
  1041. <p>Foo
  1042. bar</p>
  1043. <hr />
  1044. <p>baz</p>
  1045. ````````````````````````````````
  1046. or use a thematic break that cannot count as a [setext heading
  1047. underline], such as
  1048. ```````````````````````````````` example
  1049. Foo
  1050. bar
  1051. * * *
  1052. baz
  1053. .
  1054. <p>Foo
  1055. bar</p>
  1056. <hr />
  1057. <p>baz</p>
  1058. ````````````````````````````````
  1059. Authors who want interpretation 3 can use backslash escapes:
  1060. ```````````````````````````````` example
  1061. Foo
  1062. bar
  1063. \---
  1064. baz
  1065. .
  1066. <p>Foo
  1067. bar
  1068. ---
  1069. baz</p>
  1070. ````````````````````````````````
  1071. ## Indented code blocks
  1072. An [indented code block](@) is composed of one or more
  1073. [indented chunks] separated by blank lines.
  1074. An [indented chunk](@) is a sequence of non-blank lines,
  1075. each indented four or more spaces. The contents of the code block are
  1076. the literal contents of the lines, including trailing
  1077. [line endings], minus four spaces of indentation.
  1078. An indented code block has no [info string].
  1079. An indented code block cannot interrupt a paragraph, so there must be
  1080. a blank line between a paragraph and a following indented code block.
  1081. (A blank line is not needed, however, between a code block and a following
  1082. paragraph.)
  1083. ```````````````````````````````` example
  1084. a simple
  1085. indented code block
  1086. .
  1087. <pre><code>a simple
  1088. indented code block
  1089. </code></pre>
  1090. ````````````````````````````````
  1091. If there is any ambiguity between an interpretation of indentation
  1092. as a code block and as indicating that material belongs to a [list
  1093. item][list items], the list item interpretation takes precedence:
  1094. ```````````````````````````````` example
  1095. - foo
  1096. bar
  1097. .
  1098. <ul>
  1099. <li>
  1100. <p>foo</p>
  1101. <p>bar</p>
  1102. </li>
  1103. </ul>
  1104. ````````````````````````````````
  1105. ```````````````````````````````` example
  1106. 1. foo
  1107. - bar
  1108. .
  1109. <ol>
  1110. <li>
  1111. <p>foo</p>
  1112. <ul>
  1113. <li>bar</li>
  1114. </ul>
  1115. </li>
  1116. </ol>
  1117. ````````````````````````````````
  1118. The contents of a code block are literal text, and do not get parsed
  1119. as Markdown:
  1120. ```````````````````````````````` example
  1121. <a/>
  1122. *hi*
  1123. - one
  1124. .
  1125. <pre><code>&lt;a/&gt;
  1126. *hi*
  1127. - one
  1128. </code></pre>
  1129. ````````````````````````````````
  1130. Here we have three chunks separated by blank lines:
  1131. ```````````````````````````````` example
  1132. chunk1
  1133. chunk2
  1134. chunk3
  1135. .
  1136. <pre><code>chunk1
  1137. chunk2
  1138. chunk3
  1139. </code></pre>
  1140. ````````````````````````````````
  1141. Any initial spaces beyond four will be included in the content, even
  1142. in interior blank lines:
  1143. ```````````````````````````````` example
  1144. chunk1
  1145. chunk2
  1146. .
  1147. <pre><code>chunk1
  1148. chunk2
  1149. </code></pre>
  1150. ````````````````````````````````
  1151. An indented code block cannot interrupt a paragraph. (This
  1152. allows hanging indents and the like.)
  1153. ```````````````````````````````` example
  1154. Foo
  1155. bar
  1156. .
  1157. <p>Foo
  1158. bar</p>
  1159. ````````````````````````````````
  1160. However, any non-blank line with fewer than four leading spaces ends
  1161. the code block immediately. So a paragraph may occur immediately
  1162. after indented code:
  1163. ```````````````````````````````` example
  1164. foo
  1165. bar
  1166. .
  1167. <pre><code>foo
  1168. </code></pre>
  1169. <p>bar</p>
  1170. ````````````````````````````````
  1171. And indented code can occur immediately before and after other kinds of
  1172. blocks:
  1173. ```````````````````````````````` example
  1174. # Heading
  1175. foo
  1176. Heading
  1177. ------
  1178. foo
  1179. ----
  1180. .
  1181. <h1>Heading</h1>
  1182. <pre><code>foo
  1183. </code></pre>
  1184. <h2>Heading</h2>
  1185. <pre><code>foo
  1186. </code></pre>
  1187. <hr />
  1188. ````````````````````````````````
  1189. The first line can be indented more than four spaces:
  1190. ```````````````````````````````` example
  1191. foo
  1192. bar
  1193. .
  1194. <pre><code> foo
  1195. bar
  1196. </code></pre>
  1197. ````````````````````````````````
  1198. Blank lines preceding or following an indented code block
  1199. are not included in it:
  1200. ```````````````````````````````` example
  1201. foo
  1202. .
  1203. <pre><code>foo
  1204. </code></pre>
  1205. ````````````````````````````````
  1206. Trailing spaces are included in the code block's content:
  1207. ```````````````````````````````` example
  1208. foo
  1209. .
  1210. <pre><code>foo
  1211. </code></pre>
  1212. ````````````````````````````````
  1213. ## Fenced code blocks
  1214. A [code fence](@) is a sequence
  1215. of at least three consecutive backtick characters (`` ` ``) or
  1216. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1217. A [fenced code block](@)
  1218. begins with a code fence, indented no more than three spaces.
  1219. The line with the opening code fence may optionally contain some text
  1220. following the code fence; this is trimmed of leading and trailing
  1221. whitespace and called the [info string](@). If the [info string] comes
  1222. after a backtick fence, it may not contain any backtick
  1223. characters. (The reason for this restriction is that otherwise
  1224. some inline code would be incorrectly interpreted as the
  1225. beginning of a fenced code block.)
  1226. The content of the code block consists of all subsequent lines, until
  1227. a closing [code fence] of the same type as the code block
  1228. began with (backticks or tildes), and with at least as many backticks
  1229. or tildes as the opening code fence. If the leading code fence is
  1230. indented N spaces, then up to N spaces of indentation are removed from
  1231. each line of the content (if present). (If a content line is not
  1232. indented, it is preserved unchanged. If it is indented less than N
  1233. spaces, all of the indentation is removed.)
  1234. The closing code fence may be indented up to three spaces, and may be
  1235. followed only by spaces, which are ignored. If the end of the
  1236. containing block (or document) is reached and no closing code fence
  1237. has been found, the code block contains all of the lines after the
  1238. opening code fence until the end of the containing block (or
  1239. document). (An alternative spec would require backtracking in the
  1240. event that a closing code fence is not found. But this makes parsing
  1241. much less efficient, and there seems to be no real down side to the
  1242. behavior described here.)
  1243. A fenced code block may interrupt a paragraph, and does not require
  1244. a blank line either before or after.
  1245. The content of a code fence is treated as literal text, not parsed
  1246. as inlines. The first word of the [info string] is typically used to
  1247. specify the language of the code sample, and rendered in the `class`
  1248. attribute of the `code` tag. However, this spec does not mandate any
  1249. particular treatment of the [info string].
  1250. Here is a simple example with backticks:
  1251. ```````````````````````````````` example
  1252. ```
  1253. <
  1254. >
  1255. ```
  1256. .
  1257. <pre><code>&lt;
  1258. &gt;
  1259. </code></pre>
  1260. ````````````````````````````````
  1261. With tildes:
  1262. ```````````````````````````````` example
  1263. ~~~
  1264. <
  1265. >
  1266. ~~~
  1267. .
  1268. <pre><code>&lt;
  1269. &gt;
  1270. </code></pre>
  1271. ````````````````````````````````
  1272. Fewer than three backticks is not enough:
  1273. ```````````````````````````````` example
  1274. ``
  1275. foo
  1276. ``
  1277. .
  1278. <p><code>foo</code></p>
  1279. ````````````````````````````````
  1280. The closing code fence must use the same character as the opening
  1281. fence:
  1282. ```````````````````````````````` example
  1283. ```
  1284. aaa
  1285. ~~~
  1286. ```
  1287. .
  1288. <pre><code>aaa
  1289. ~~~
  1290. </code></pre>
  1291. ````````````````````````````````
  1292. ```````````````````````````````` example
  1293. ~~~
  1294. aaa
  1295. ```
  1296. ~~~
  1297. .
  1298. <pre><code>aaa
  1299. ```
  1300. </code></pre>
  1301. ````````````````````````````````
  1302. The closing code fence must be at least as long as the opening fence:
  1303. ```````````````````````````````` example
  1304. ````
  1305. aaa
  1306. ```
  1307. ``````
  1308. .
  1309. <pre><code>aaa
  1310. ```
  1311. </code></pre>
  1312. ````````````````````````````````
  1313. ```````````````````````````````` example
  1314. ~~~~
  1315. aaa
  1316. ~~~
  1317. ~~~~
  1318. .
  1319. <pre><code>aaa
  1320. ~~~
  1321. </code></pre>
  1322. ````````````````````````````````
  1323. Unclosed code blocks are closed by the end of the document
  1324. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1325. ```````````````````````````````` example
  1326. ```
  1327. .
  1328. <pre><code></code></pre>
  1329. ````````````````````````````````
  1330. ```````````````````````````````` example
  1331. `````
  1332. ```
  1333. aaa
  1334. .
  1335. <pre><code>
  1336. ```
  1337. aaa
  1338. </code></pre>
  1339. ````````````````````````````````
  1340. ```````````````````````````````` example
  1341. > ```
  1342. > aaa
  1343. bbb
  1344. .
  1345. <blockquote>
  1346. <pre><code>aaa
  1347. </code></pre>
  1348. </blockquote>
  1349. <p>bbb</p>
  1350. ````````````````````````````````
  1351. A code block can have all empty lines as its content:
  1352. ```````````````````````````````` example
  1353. ```
  1354. ```
  1355. .
  1356. <pre><code>
  1357. </code></pre>
  1358. ````````````````````````````````
  1359. A code block can be empty:
  1360. ```````````````````````````````` example
  1361. ```
  1362. ```
  1363. .
  1364. <pre><code></code></pre>
  1365. ````````````````````````````````
  1366. Fences can be indented. If the opening fence is indented,
  1367. content lines will have equivalent opening indentation removed,
  1368. if present:
  1369. ```````````````````````````````` example
  1370. ```
  1371. aaa
  1372. aaa
  1373. ```
  1374. .
  1375. <pre><code>aaa
  1376. aaa
  1377. </code></pre>
  1378. ````````````````````````````````
  1379. ```````````````````````````````` example
  1380. ```
  1381. aaa
  1382. aaa
  1383. aaa
  1384. ```
  1385. .
  1386. <pre><code>aaa
  1387. aaa
  1388. aaa
  1389. </code></pre>
  1390. ````````````````````````````````
  1391. ```````````````````````````````` example
  1392. ```
  1393. aaa
  1394. aaa
  1395. aaa
  1396. ```
  1397. .
  1398. <pre><code>aaa
  1399. aaa
  1400. aaa
  1401. </code></pre>
  1402. ````````````````````````````````
  1403. Four spaces indentation produces an indented code block:
  1404. ```````````````````````````````` example
  1405. ```
  1406. aaa
  1407. ```
  1408. .
  1409. <pre><code>```
  1410. aaa
  1411. ```
  1412. </code></pre>
  1413. ````````````````````````````````
  1414. Closing fences may be indented by 0-3 spaces, and their indentation
  1415. need not match that of the opening fence:
  1416. ```````````````````````````````` example
  1417. ```
  1418. aaa
  1419. ```
  1420. .
  1421. <pre><code>aaa
  1422. </code></pre>
  1423. ````````````````````````````````
  1424. ```````````````````````````````` example
  1425. ```
  1426. aaa
  1427. ```
  1428. .
  1429. <pre><code>aaa
  1430. </code></pre>
  1431. ````````````````````````````````
  1432. This is not a closing fence, because it is indented 4 spaces:
  1433. ```````````````````````````````` example
  1434. ```
  1435. aaa
  1436. ```
  1437. .
  1438. <pre><code>aaa
  1439. ```
  1440. </code></pre>
  1441. ````````````````````````````````
  1442. Code fences (opening and closing) cannot contain internal spaces:
  1443. ```````````````````````````````` example
  1444. ``` ```
  1445. aaa
  1446. .
  1447. <p><code> </code>
  1448. aaa</p>
  1449. ````````````````````````````````
  1450. ```````````````````````````````` example
  1451. ~~~~~~
  1452. aaa
  1453. ~~~ ~~
  1454. .
  1455. <pre><code>aaa
  1456. ~~~ ~~
  1457. </code></pre>
  1458. ````````````````````````````````
  1459. Fenced code blocks can interrupt paragraphs, and can be followed
  1460. directly by paragraphs, without a blank line between:
  1461. ```````````````````````````````` example
  1462. foo
  1463. ```
  1464. bar
  1465. ```
  1466. baz
  1467. .
  1468. <p>foo</p>
  1469. <pre><code>bar
  1470. </code></pre>
  1471. <p>baz</p>
  1472. ````````````````````````````````
  1473. Other blocks can also occur before and after fenced code blocks
  1474. without an intervening blank line:
  1475. ```````````````````````````````` example
  1476. foo
  1477. ---
  1478. ~~~
  1479. bar
  1480. ~~~
  1481. # baz
  1482. .
  1483. <h2>foo</h2>
  1484. <pre><code>bar
  1485. </code></pre>
  1486. <h1>baz</h1>
  1487. ````````````````````````````````
  1488. An [info string] can be provided after the opening code fence.
  1489. Although this spec doesn't mandate any particular treatment of
  1490. the info string, the first word is typically used to specify
  1491. the language of the code block. In HTML output, the language is
  1492. normally indicated by adding a class to the `code` element consisting
  1493. of `language-` followed by the language name.
  1494. ```````````````````````````````` example
  1495. ```ruby
  1496. def foo(x)
  1497. return 3
  1498. end
  1499. ```
  1500. .
  1501. <pre><code class="language-ruby">def foo(x)
  1502. return 3
  1503. end
  1504. </code></pre>
  1505. ````````````````````````````````
  1506. ```````````````````````````````` example
  1507. ~~~~ ruby startline=3 $%@#$
  1508. def foo(x)
  1509. return 3
  1510. end
  1511. ~~~~~~~
  1512. .
  1513. <pre><code class="language-ruby">def foo(x)
  1514. return 3
  1515. end
  1516. </code></pre>
  1517. ````````````````````````````````
  1518. ```````````````````````````````` example
  1519. ````;
  1520. ````
  1521. .
  1522. <pre><code class="language-;"></code></pre>
  1523. ````````````````````````````````
  1524. [Info strings] for backtick code blocks cannot contain backticks:
  1525. ```````````````````````````````` example
  1526. ``` aa ```
  1527. foo
  1528. .
  1529. <p><code>aa</code>
  1530. foo</p>
  1531. ````````````````````````````````
  1532. [Info strings] for tilde code blocks can contain backticks and tildes:
  1533. ```````````````````````````````` example
  1534. ~~~ aa ``` ~~~
  1535. foo
  1536. ~~~
  1537. .
  1538. <pre><code class="language-aa">foo
  1539. </code></pre>
  1540. ````````````````````````````````
  1541. Closing code fences cannot have [info strings]:
  1542. ```````````````````````````````` example
  1543. ```
  1544. ``` aaa
  1545. ```
  1546. .
  1547. <pre><code>``` aaa
  1548. </code></pre>
  1549. ````````````````````````````````
  1550. ## HTML blocks
  1551. An [HTML block](@) is a group of lines that is treated
  1552. as raw HTML (and will not be escaped in HTML output).
  1553. There are seven kinds of [HTML block], which can be defined by their
  1554. start and end conditions. The block begins with a line that meets a
  1555. [start condition](@) (after up to three spaces optional indentation).
  1556. It ends with the first subsequent line that meets a matching [end
  1557. condition](@), or the last line of the document, or the last line of
  1558. the [container block](#container-blocks) containing the current HTML
  1559. block, if no line is encountered that meets the [end condition]. If
  1560. the first line meets both the [start condition] and the [end
  1561. condition], the block will contain just that line.
  1562. 1. **Start condition:** line begins with the string `<script`,
  1563. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1564. the string `>`, or the end of the line.\
  1565. **End condition:** line contains an end tag
  1566. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1567. need not match the start tag).
  1568. 2. **Start condition:** line begins with the string `<!--`.\
  1569. **End condition:** line contains the string `-->`.
  1570. 3. **Start condition:** line begins with the string `<?`.\
  1571. **End condition:** line contains the string `?>`.
  1572. 4. **Start condition:** line begins with the string `<!`
  1573. followed by an uppercase ASCII letter.\
  1574. **End condition:** line contains the character `>`.
  1575. 5. **Start condition:** line begins with the string
  1576. `<![CDATA[`.\
  1577. **End condition:** line contains the string `]]>`.
  1578. 6. **Start condition:** line begins the string `<` or `</`
  1579. followed by one of the strings (case-insensitive) `address`,
  1580. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1581. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1582. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1583. `footer`, `form`, `frame`, `frameset`,
  1584. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1585. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1586. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1587. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1588. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1589. by [whitespace], the end of the line, the string `>`, or
  1590. the string `/>`.\
  1591. **End condition:** line is followed by a [blank line].
  1592. 7. **Start condition:** line begins with a complete [open tag]
  1593. (with any [tag name] other than `script`,
  1594. `style`, or `pre`) or a complete [closing tag],
  1595. followed only by [whitespace] or the end of the line.\
  1596. **End condition:** line is followed by a [blank line].
  1597. HTML blocks continue until they are closed by their appropriate
  1598. [end condition], or the last line of the document or other [container
  1599. block](#container-blocks). This means any HTML **within an HTML
  1600. block** that might otherwise be recognised as a start condition will
  1601. be ignored by the parser and passed through as-is, without changing
  1602. the parser's state.
  1603. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1604. the parser state; as the HTML block was started in by start condition 6, it
  1605. will end at any blank line. This can be surprising:
  1606. ```````````````````````````````` example
  1607. <table><tr><td>
  1608. <pre>
  1609. **Hello**,
  1610. _world_.
  1611. </pre>
  1612. </td></tr></table>
  1613. .
  1614. <table><tr><td>
  1615. <pre>
  1616. **Hello**,
  1617. <p><em>world</em>.
  1618. </pre></p>
  1619. </td></tr></table>
  1620. ````````````````````````````````
  1621. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1622. text remains verbatim — and regular parsing resumes, with a paragraph,
  1623. emphasised `world` and inline and block HTML following.
  1624. All types of [HTML blocks] except type 7 may interrupt
  1625. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1626. (This restriction is intended to prevent unwanted interpretation
  1627. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1628. Some simple examples follow. Here are some basic HTML blocks
  1629. of type 6:
  1630. ```````````````````````````````` example
  1631. <table>
  1632. <tr>
  1633. <td>
  1634. hi
  1635. </td>
  1636. </tr>
  1637. </table>
  1638. okay.
  1639. .
  1640. <table>
  1641. <tr>
  1642. <td>
  1643. hi
  1644. </td>
  1645. </tr>
  1646. </table>
  1647. <p>okay.</p>
  1648. ````````````````````````````````
  1649. ```````````````````````````````` example
  1650. <div>
  1651. *hello*
  1652. <foo><a>
  1653. .
  1654. <div>
  1655. *hello*
  1656. <foo><a>
  1657. ````````````````````````````````
  1658. A block can also start with a closing tag:
  1659. ```````````````````````````````` example
  1660. </div>
  1661. *foo*
  1662. .
  1663. </div>
  1664. *foo*
  1665. ````````````````````````````````
  1666. Here we have two HTML blocks with a Markdown paragraph between them:
  1667. ```````````````````````````````` example
  1668. <DIV CLASS="foo">
  1669. *Markdown*
  1670. </DIV>
  1671. .
  1672. <DIV CLASS="foo">
  1673. <p><em>Markdown</em></p>
  1674. </DIV>
  1675. ````````````````````````````````
  1676. The tag on the first line can be partial, as long
  1677. as it is split where there would be whitespace:
  1678. ```````````````````````````````` example
  1679. <div id="foo"
  1680. class="bar">
  1681. </div>
  1682. .
  1683. <div id="foo"
  1684. class="bar">
  1685. </div>
  1686. ````````````````````````````````
  1687. ```````````````````````````````` example
  1688. <div id="foo" class="bar
  1689. baz">
  1690. </div>
  1691. .
  1692. <div id="foo" class="bar
  1693. baz">
  1694. </div>
  1695. ````````````````````````````````
  1696. An open tag need not be closed:
  1697. ```````````````````````````````` example
  1698. <div>
  1699. *foo*
  1700. *bar*
  1701. .
  1702. <div>
  1703. *foo*
  1704. <p><em>bar</em></p>
  1705. ````````````````````````````````
  1706. A partial tag need not even be completed (garbage
  1707. in, garbage out):
  1708. ```````````````````````````````` example
  1709. <div id="foo"
  1710. *hi*
  1711. .
  1712. <div id="foo"
  1713. *hi*
  1714. ````````````````````````````````
  1715. ```````````````````````````````` example
  1716. <div class
  1717. foo
  1718. .
  1719. <div class
  1720. foo
  1721. ````````````````````````````````
  1722. The initial tag doesn't even need to be a valid
  1723. tag, as long as it starts like one:
  1724. ```````````````````````````````` example
  1725. <div *???-&&&-<---
  1726. *foo*
  1727. .
  1728. <div *???-&&&-<---
  1729. *foo*
  1730. ````````````````````````````````
  1731. In type 6 blocks, the initial tag need not be on a line by
  1732. itself:
  1733. ```````````````````````````````` example
  1734. <div><a href="bar">*foo*</a></div>
  1735. .
  1736. <div><a href="bar">*foo*</a></div>
  1737. ````````````````````````````````
  1738. ```````````````````````````````` example
  1739. <table><tr><td>
  1740. foo
  1741. </td></tr></table>
  1742. .
  1743. <table><tr><td>
  1744. foo
  1745. </td></tr></table>
  1746. ````````````````````````````````
  1747. Everything until the next blank line or end of document
  1748. gets included in the HTML block. So, in the following
  1749. example, what looks like a Markdown code block
  1750. is actually part of the HTML block, which continues until a blank
  1751. line or the end of the document is reached:
  1752. ```````````````````````````````` example
  1753. <div></div>
  1754. ``` c
  1755. int x = 33;
  1756. ```
  1757. .
  1758. <div></div>
  1759. ``` c
  1760. int x = 33;
  1761. ```
  1762. ````````````````````````````````
  1763. To start an [HTML block] with a tag that is *not* in the
  1764. list of block-level tags in (6), you must put the tag by
  1765. itself on the first line (and it must be complete):
  1766. ```````````````````````````````` example
  1767. <a href="foo">
  1768. *bar*
  1769. </a>
  1770. .
  1771. <a href="foo">
  1772. *bar*
  1773. </a>
  1774. ````````````````````````````````
  1775. In type 7 blocks, the [tag name] can be anything:
  1776. ```````````````````````````````` example
  1777. <Warning>
  1778. *bar*
  1779. </Warning>
  1780. .
  1781. <Warning>
  1782. *bar*
  1783. </Warning>
  1784. ````````````````````````````````
  1785. ```````````````````````````````` example
  1786. <i class="foo">
  1787. *bar*
  1788. </i>
  1789. .
  1790. <i class="foo">
  1791. *bar*
  1792. </i>
  1793. ````````````````````````````````
  1794. ```````````````````````````````` example
  1795. </ins>
  1796. *bar*
  1797. .
  1798. </ins>
  1799. *bar*
  1800. ````````````````````````````````
  1801. These rules are designed to allow us to work with tags that
  1802. can function as either block-level or inline-level tags.
  1803. The `<del>` tag is a nice example. We can surround content with
  1804. `<del>` tags in three different ways. In this case, we get a raw
  1805. HTML block, because the `<del>` tag is on a line by itself:
  1806. ```````````````````````````````` example
  1807. <del>
  1808. *foo*
  1809. </del>
  1810. .
  1811. <del>
  1812. *foo*
  1813. </del>
  1814. ````````````````````````````````
  1815. In this case, we get a raw HTML block that just includes
  1816. the `<del>` tag (because it ends with the following blank
  1817. line). So the contents get interpreted as CommonMark:
  1818. ```````````````````````````````` example
  1819. <del>
  1820. *foo*
  1821. </del>
  1822. .
  1823. <del>
  1824. <p><em>foo</em></p>
  1825. </del>
  1826. ````````````````````````````````
  1827. Finally, in this case, the `<del>` tags are interpreted
  1828. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1829. the tag is not on a line by itself, we get inline HTML
  1830. rather than an [HTML block].)
  1831. ```````````````````````````````` example
  1832. <del>*foo*</del>
  1833. .
  1834. <p><del><em>foo</em></del></p>
  1835. ````````````````````````````````
  1836. HTML tags designed to contain literal content
  1837. (`script`, `style`, `pre`), comments, processing instructions,
  1838. and declarations are treated somewhat differently.
  1839. Instead of ending at the first blank line, these blocks
  1840. end at the first line containing a corresponding end tag.
  1841. As a result, these blocks can contain blank lines:
  1842. A pre tag (type 1):
  1843. ```````````````````````````````` example
  1844. <pre language="haskell"><code>
  1845. import Text.HTML.TagSoup
  1846. main :: IO ()
  1847. main = print $ parseTags tags
  1848. </code></pre>
  1849. okay
  1850. .
  1851. <pre language="haskell"><code>
  1852. import Text.HTML.TagSoup
  1853. main :: IO ()
  1854. main = print $ parseTags tags
  1855. </code></pre>
  1856. <p>okay</p>
  1857. ````````````````````````````````
  1858. A script tag (type 1):
  1859. ```````````````````````````````` example
  1860. <script type="text/javascript">
  1861. // JavaScript example
  1862. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1863. </script>
  1864. okay
  1865. .
  1866. <script type="text/javascript">
  1867. // JavaScript example
  1868. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1869. </script>
  1870. <p>okay</p>
  1871. ````````````````````````````````
  1872. A style tag (type 1):
  1873. ```````````````````````````````` example
  1874. <style
  1875. type="text/css">
  1876. h1 {color:red;}
  1877. p {color:blue;}
  1878. </style>
  1879. okay
  1880. .
  1881. <style
  1882. type="text/css">
  1883. h1 {color:red;}
  1884. p {color:blue;}
  1885. </style>
  1886. <p>okay</p>
  1887. ````````````````````````````````
  1888. If there is no matching end tag, the block will end at the
  1889. end of the document (or the enclosing [block quote][block quotes]
  1890. or [list item][list items]):
  1891. ```````````````````````````````` example
  1892. <style
  1893. type="text/css">
  1894. foo
  1895. .
  1896. <style
  1897. type="text/css">
  1898. foo
  1899. ````````````````````````````````
  1900. ```````````````````````````````` example
  1901. > <div>
  1902. > foo
  1903. bar
  1904. .
  1905. <blockquote>
  1906. <div>
  1907. foo
  1908. </blockquote>
  1909. <p>bar</p>
  1910. ````````````````````````````````
  1911. ```````````````````````````````` example
  1912. - <div>
  1913. - foo
  1914. .
  1915. <ul>
  1916. <li>
  1917. <div>
  1918. </li>
  1919. <li>foo</li>
  1920. </ul>
  1921. ````````````````````````````````
  1922. The end tag can occur on the same line as the start tag:
  1923. ```````````````````````````````` example
  1924. <style>p{color:red;}</style>
  1925. *foo*
  1926. .
  1927. <style>p{color:red;}</style>
  1928. <p><em>foo</em></p>
  1929. ````````````````````````````````
  1930. ```````````````````````````````` example
  1931. <!-- foo -->*bar*
  1932. *baz*
  1933. .
  1934. <!-- foo -->*bar*
  1935. <p><em>baz</em></p>
  1936. ````````````````````````````````
  1937. Note that anything on the last line after the
  1938. end tag will be included in the [HTML block]:
  1939. ```````````````````````````````` example
  1940. <script>
  1941. foo
  1942. </script>1. *bar*
  1943. .
  1944. <script>
  1945. foo
  1946. </script>1. *bar*
  1947. ````````````````````````````````
  1948. A comment (type 2):
  1949. ```````````````````````````````` example
  1950. <!-- Foo
  1951. bar
  1952. baz -->
  1953. okay
  1954. .
  1955. <!-- Foo
  1956. bar
  1957. baz -->
  1958. <p>okay</p>
  1959. ````````````````````````````````
  1960. A processing instruction (type 3):
  1961. ```````````````````````````````` example
  1962. <?php
  1963. echo '>';
  1964. ?>
  1965. okay
  1966. .
  1967. <?php
  1968. echo '>';
  1969. ?>
  1970. <p>okay</p>
  1971. ````````````````````````````````
  1972. A declaration (type 4):
  1973. ```````````````````````````````` example
  1974. <!DOCTYPE html>
  1975. .
  1976. <!DOCTYPE html>
  1977. ````````````````````````````````
  1978. CDATA (type 5):
  1979. ```````````````````````````````` example
  1980. <![CDATA[
  1981. function matchwo(a,b)
  1982. {
  1983. if (a < b && a < 0) then {
  1984. return 1;
  1985. } else {
  1986. return 0;
  1987. }
  1988. }
  1989. ]]>
  1990. okay
  1991. .
  1992. <![CDATA[
  1993. function matchwo(a,b)
  1994. {
  1995. if (a < b && a < 0) then {
  1996. return 1;
  1997. } else {
  1998. return 0;
  1999. }
  2000. }
  2001. ]]>
  2002. <p>okay</p>
  2003. ````````````````````````````````
  2004. The opening tag can be indented 1-3 spaces, but not 4:
  2005. ```````````````````````````````` example
  2006. <!-- foo -->
  2007. <!-- foo -->
  2008. .
  2009. <!-- foo -->
  2010. <pre><code>&lt;!-- foo --&gt;
  2011. </code></pre>
  2012. ````````````````````````````````
  2013. ```````````````````````````````` example
  2014. <div>
  2015. <div>
  2016. .
  2017. <div>
  2018. <pre><code>&lt;div&gt;
  2019. </code></pre>
  2020. ````````````````````````````````
  2021. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2022. preceded by a blank line.
  2023. ```````````````````````````````` example
  2024. Foo
  2025. <div>
  2026. bar
  2027. </div>
  2028. .
  2029. <p>Foo</p>
  2030. <div>
  2031. bar
  2032. </div>
  2033. ````````````````````````````````
  2034. However, a following blank line is needed, except at the end of
  2035. a document, and except for blocks of types 1--5, [above][HTML
  2036. block]:
  2037. ```````````````````````````````` example
  2038. <div>
  2039. bar
  2040. </div>
  2041. *foo*
  2042. .
  2043. <div>
  2044. bar
  2045. </div>
  2046. *foo*
  2047. ````````````````````````````````
  2048. HTML blocks of type 7 cannot interrupt a paragraph:
  2049. ```````````````````````````````` example
  2050. Foo
  2051. <a href="bar">
  2052. baz
  2053. .
  2054. <p>Foo
  2055. <a href="bar">
  2056. baz</p>
  2057. ````````````````````````````````
  2058. This rule differs from John Gruber's original Markdown syntax
  2059. specification, which says:
  2060. > The only restrictions are that block-level HTML elements —
  2061. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2062. > surrounding content by blank lines, and the start and end tags of the
  2063. > block should not be indented with tabs or spaces.
  2064. In some ways Gruber's rule is more restrictive than the one given
  2065. here:
  2066. - It requires that an HTML block be preceded by a blank line.
  2067. - It does not allow the start tag to be indented.
  2068. - It requires a matching end tag, which it also does not allow to
  2069. be indented.
  2070. Most Markdown implementations (including some of Gruber's own) do not
  2071. respect all of these restrictions.
  2072. There is one respect, however, in which Gruber's rule is more liberal
  2073. than the one given here, since it allows blank lines to occur inside
  2074. an HTML block. There are two reasons for disallowing them here.
  2075. First, it removes the need to parse balanced tags, which is
  2076. expensive and can require backtracking from the end of the document
  2077. if no matching end tag is found. Second, it provides a very simple
  2078. and flexible way of including Markdown content inside HTML tags:
  2079. simply separate the Markdown from the HTML using blank lines:
  2080. Compare:
  2081. ```````````````````````````````` example
  2082. <div>
  2083. *Emphasized* text.
  2084. </div>
  2085. .
  2086. <div>
  2087. <p><em>Emphasized</em> text.</p>
  2088. </div>
  2089. ````````````````````````````````
  2090. ```````````````````````````````` example
  2091. <div>
  2092. *Emphasized* text.
  2093. </div>
  2094. .
  2095. <div>
  2096. *Emphasized* text.
  2097. </div>
  2098. ````````````````````````````````
  2099. Some Markdown implementations have adopted a convention of
  2100. interpreting content inside tags as text if the open tag has
  2101. the attribute `markdown=1`. The rule given above seems a simpler and
  2102. more elegant way of achieving the same expressive power, which is also
  2103. much simpler to parse.
  2104. The main potential drawback is that one can no longer paste HTML
  2105. blocks into Markdown documents with 100% reliability. However,
  2106. *in most cases* this will work fine, because the blank lines in
  2107. HTML are usually followed by HTML block tags. For example:
  2108. ```````````````````````````````` example
  2109. <table>
  2110. <tr>
  2111. <td>
  2112. Hi
  2113. </td>
  2114. </tr>
  2115. </table>
  2116. .
  2117. <table>
  2118. <tr>
  2119. <td>
  2120. Hi
  2121. </td>
  2122. </tr>
  2123. </table>
  2124. ````````````````````````````````
  2125. There are problems, however, if the inner tags are indented
  2126. *and* separated by spaces, as then they will be interpreted as
  2127. an indented code block:
  2128. ```````````````````````````````` example
  2129. <table>
  2130. <tr>
  2131. <td>
  2132. Hi
  2133. </td>
  2134. </tr>
  2135. </table>
  2136. .
  2137. <table>
  2138. <tr>
  2139. <pre><code>&lt;td&gt;
  2140. Hi
  2141. &lt;/td&gt;
  2142. </code></pre>
  2143. </tr>
  2144. </table>
  2145. ````````````````````````````````
  2146. Fortunately, blank lines are usually not necessary and can be
  2147. deleted. The exception is inside `<pre>` tags, but as described
  2148. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2149. *can* contain blank lines.
  2150. ## Link reference definitions
  2151. A [link reference definition](@)
  2152. consists of a [link label], indented up to three spaces, followed
  2153. by a colon (`:`), optional [whitespace] (including up to one
  2154. [line ending]), a [link destination],
  2155. optional [whitespace] (including up to one
  2156. [line ending]), and an optional [link
  2157. title], which if it is present must be separated
  2158. from the [link destination] by [whitespace].
  2159. No further [non-whitespace characters] may occur on the line.
  2160. A [link reference definition]
  2161. does not correspond to a structural element of a document. Instead, it
  2162. defines a label which can be used in [reference links]
  2163. and reference-style [images] elsewhere in the document. [Link
  2164. reference definitions] can come either before or after the links that use
  2165. them.
  2166. ```````````````````````````````` example
  2167. [foo]: /url "title"
  2168. [foo]
  2169. .
  2170. <p><a href="/url" title="title">foo</a></p>
  2171. ````````````````````````````````
  2172. ```````````````````````````````` example
  2173. [foo]:
  2174. /url
  2175. 'the title'
  2176. [foo]
  2177. .
  2178. <p><a href="/url" title="the title">foo</a></p>
  2179. ````````````````````````````````
  2180. ```````````````````````````````` example
  2181. [Foo*bar\]]:my_(url) 'title (with parens)'
  2182. [Foo*bar\]]
  2183. .
  2184. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2185. ````````````````````````````````
  2186. ```````````````````````````````` example
  2187. [Foo bar]:
  2188. <my url>
  2189. 'title'
  2190. [Foo bar]
  2191. .
  2192. <p><a href="my%20url" title="title">Foo bar</a></p>
  2193. ````````````````````````````````
  2194. The title may extend over multiple lines:
  2195. ```````````````````````````````` example
  2196. [foo]: /url '
  2197. title
  2198. line1
  2199. line2
  2200. '
  2201. [foo]
  2202. .
  2203. <p><a href="/url" title="
  2204. title
  2205. line1
  2206. line2
  2207. ">foo</a></p>
  2208. ````````````````````````````````
  2209. However, it may not contain a [blank line]:
  2210. ```````````````````````````````` example
  2211. [foo]: /url 'title
  2212. with blank line'
  2213. [foo]
  2214. .
  2215. <p>[foo]: /url 'title</p>
  2216. <p>with blank line'</p>
  2217. <p>[foo]</p>
  2218. ````````````````````````````````
  2219. The title may be omitted:
  2220. ```````````````````````````````` example
  2221. [foo]:
  2222. /url
  2223. [foo]
  2224. .
  2225. <p><a href="/url">foo</a></p>
  2226. ````````````````````````````````
  2227. The link destination may not be omitted:
  2228. ```````````````````````````````` example
  2229. [foo]:
  2230. [foo]
  2231. .
  2232. <p>[foo]:</p>
  2233. <p>[foo]</p>
  2234. ````````````````````````````````
  2235. However, an empty link destination may be specified using
  2236. angle brackets:
  2237. ```````````````````````````````` example
  2238. [foo]: <>
  2239. [foo]
  2240. .
  2241. <p><a href="">foo</a></p>
  2242. ````````````````````````````````
  2243. The title must be separated from the link destination by
  2244. whitespace:
  2245. ```````````````````````````````` example
  2246. [foo]: <bar>(baz)
  2247. [foo]
  2248. .
  2249. <p>[foo]: <bar>(baz)</p>
  2250. <p>[foo]</p>
  2251. ````````````````````````````````
  2252. Both title and destination can contain backslash escapes
  2253. and literal backslashes:
  2254. ```````````````````````````````` example
  2255. [foo]: /url\bar\*baz "foo\"bar\baz"
  2256. [foo]
  2257. .
  2258. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2259. ````````````````````````````````
  2260. A link can come before its corresponding definition:
  2261. ```````````````````````````````` example
  2262. [foo]
  2263. [foo]: url
  2264. .
  2265. <p><a href="url">foo</a></p>
  2266. ````````````````````````````````
  2267. If there are several matching definitions, the first one takes
  2268. precedence:
  2269. ```````````````````````````````` example
  2270. [foo]
  2271. [foo]: first
  2272. [foo]: second
  2273. .
  2274. <p><a href="first">foo</a></p>
  2275. ````````````````````````````````
  2276. As noted in the section on [Links], matching of labels is
  2277. case-insensitive (see [matches]).
  2278. ```````````````````````````````` example
  2279. [FOO]: /url
  2280. [Foo]
  2281. .
  2282. <p><a href="/url">Foo</a></p>
  2283. ````````````````````````````````
  2284. ```````````````````````````````` example
  2285. [ΑΓΩ]: /φου
  2286. [αγω]
  2287. .
  2288. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2289. ````````````````````````````````
  2290. Here is a link reference definition with no corresponding link.
  2291. It contributes nothing to the document.
  2292. ```````````````````````````````` example
  2293. [foo]: /url
  2294. .
  2295. ````````````````````````````````
  2296. Here is another one:
  2297. ```````````````````````````````` example
  2298. [
  2299. foo
  2300. ]: /url
  2301. bar
  2302. .
  2303. <p>bar</p>
  2304. ````````````````````````````````
  2305. This is not a link reference definition, because there are
  2306. [non-whitespace characters] after the title:
  2307. ```````````````````````````````` example
  2308. [foo]: /url "title" ok
  2309. .
  2310. <p>[foo]: /url &quot;title&quot; ok</p>
  2311. ````````````````````````````````
  2312. This is a link reference definition, but it has no title:
  2313. ```````````````````````````````` example
  2314. [foo]: /url
  2315. "title" ok
  2316. .
  2317. <p>&quot;title&quot; ok</p>
  2318. ````````````````````````````````
  2319. This is not a link reference definition, because it is indented
  2320. four spaces:
  2321. ```````````````````````````````` example
  2322. [foo]: /url "title"
  2323. [foo]
  2324. .
  2325. <pre><code>[foo]: /url &quot;title&quot;
  2326. </code></pre>
  2327. <p>[foo]</p>
  2328. ````````````````````````````````
  2329. This is not a link reference definition, because it occurs inside
  2330. a code block:
  2331. ```````````````````````````````` example
  2332. ```
  2333. [foo]: /url
  2334. ```
  2335. [foo]
  2336. .
  2337. <pre><code>[foo]: /url
  2338. </code></pre>
  2339. <p>[foo]</p>
  2340. ````````````````````````````````
  2341. A [link reference definition] cannot interrupt a paragraph.
  2342. ```````````````````````````````` example
  2343. Foo
  2344. [bar]: /baz
  2345. [bar]
  2346. .
  2347. <p>Foo
  2348. [bar]: /baz</p>
  2349. <p>[bar]</p>
  2350. ````````````````````````````````
  2351. However, it can directly follow other block elements, such as headings
  2352. and thematic breaks, and it need not be followed by a blank line.
  2353. ```````````````````````````````` example
  2354. # [Foo]
  2355. [foo]: /url
  2356. > bar
  2357. .
  2358. <h1><a href="/url">Foo</a></h1>
  2359. <blockquote>
  2360. <p>bar</p>
  2361. </blockquote>
  2362. ````````````````````````````````
  2363. ```````````````````````````````` example
  2364. [foo]: /url
  2365. bar
  2366. ===
  2367. [foo]
  2368. .
  2369. <h1>bar</h1>
  2370. <p><a href="/url">foo</a></p>
  2371. ````````````````````````````````
  2372. ```````````````````````````````` example
  2373. [foo]: /url
  2374. ===
  2375. [foo]
  2376. .
  2377. <p>===
  2378. <a href="/url">foo</a></p>
  2379. ````````````````````````````````
  2380. Several [link reference definitions]
  2381. can occur one after another, without intervening blank lines.
  2382. ```````````````````````````````` example
  2383. [foo]: /foo-url "foo"
  2384. [bar]: /bar-url
  2385. "bar"
  2386. [baz]: /baz-url
  2387. [foo],
  2388. [bar],
  2389. [baz]
  2390. .
  2391. <p><a href="/foo-url" title="foo">foo</a>,
  2392. <a href="/bar-url" title="bar">bar</a>,
  2393. <a href="/baz-url">baz</a></p>
  2394. ````````````````````````````````
  2395. [Link reference definitions] can occur
  2396. inside block containers, like lists and block quotations. They
  2397. affect the entire document, not just the container in which they
  2398. are defined:
  2399. ```````````````````````````````` example
  2400. [foo]
  2401. > [foo]: /url
  2402. .
  2403. <p><a href="/url">foo</a></p>
  2404. <blockquote>
  2405. </blockquote>
  2406. ````````````````````````````````
  2407. Whether something is a [link reference definition] is
  2408. independent of whether the link reference it defines is
  2409. used in the document. Thus, for example, the following
  2410. document contains just a link reference definition, and
  2411. no visible content:
  2412. ```````````````````````````````` example
  2413. [foo]: /url
  2414. .
  2415. ````````````````````````````````
  2416. ## Paragraphs
  2417. A sequence of non-blank lines that cannot be interpreted as other
  2418. kinds of blocks forms a [paragraph](@).
  2419. The contents of the paragraph are the result of parsing the
  2420. paragraph's raw content as inlines. The paragraph's raw content
  2421. is formed by concatenating the lines and removing initial and final
  2422. [whitespace].
  2423. A simple example with two paragraphs:
  2424. ```````````````````````````````` example
  2425. aaa
  2426. bbb
  2427. .
  2428. <p>aaa</p>
  2429. <p>bbb</p>
  2430. ````````````````````````````````
  2431. Paragraphs can contain multiple lines, but no blank lines:
  2432. ```````````````````````````````` example
  2433. aaa
  2434. bbb
  2435. ccc
  2436. ddd
  2437. .
  2438. <p>aaa
  2439. bbb</p>
  2440. <p>ccc
  2441. ddd</p>
  2442. ````````````````````````````````
  2443. Multiple blank lines between paragraph have no effect:
  2444. ```````````````````````````````` example
  2445. aaa
  2446. bbb
  2447. .
  2448. <p>aaa</p>
  2449. <p>bbb</p>
  2450. ````````````````````````````````
  2451. Leading spaces are skipped:
  2452. ```````````````````````````````` example
  2453. aaa
  2454. bbb
  2455. .
  2456. <p>aaa
  2457. bbb</p>
  2458. ````````````````````````````````
  2459. Lines after the first may be indented any amount, since indented
  2460. code blocks cannot interrupt paragraphs.
  2461. ```````````````````````````````` example
  2462. aaa
  2463. bbb
  2464. ccc
  2465. .
  2466. <p>aaa
  2467. bbb
  2468. ccc</p>
  2469. ````````````````````````````````
  2470. However, the first line may be indented at most three spaces,
  2471. or an indented code block will be triggered:
  2472. ```````````````````````````````` example
  2473. aaa
  2474. bbb
  2475. .
  2476. <p>aaa
  2477. bbb</p>
  2478. ````````````````````````````````
  2479. ```````````````````````````````` example
  2480. aaa
  2481. bbb
  2482. .
  2483. <pre><code>aaa
  2484. </code></pre>
  2485. <p>bbb</p>
  2486. ````````````````````````````````
  2487. Final spaces are stripped before inline parsing, so a paragraph
  2488. that ends with two or more spaces will not end with a [hard line
  2489. break]:
  2490. ```````````````````````````````` example
  2491. aaa
  2492. bbb
  2493. .
  2494. <p>aaa<br />
  2495. bbb</p>
  2496. ````````````````````````````````
  2497. ## Blank lines
  2498. [Blank lines] between block-level elements are ignored,
  2499. except for the role they play in determining whether a [list]
  2500. is [tight] or [loose].
  2501. Blank lines at the beginning and end of the document are also ignored.
  2502. ```````````````````````````````` example
  2503. aaa
  2504. # aaa
  2505. .
  2506. <p>aaa</p>
  2507. <h1>aaa</h1>
  2508. ````````````````````````````````
  2509. # Container blocks
  2510. A [container block](#container-blocks) is a block that has other
  2511. blocks as its contents. There are two basic kinds of container blocks:
  2512. [block quotes] and [list items].
  2513. [Lists] are meta-containers for [list items].
  2514. We define the syntax for container blocks recursively. The general
  2515. form of the definition is:
  2516. > If X is a sequence of blocks, then the result of
  2517. > transforming X in such-and-such a way is a container of type Y
  2518. > with these blocks as its content.
  2519. So, we explain what counts as a block quote or list item by explaining
  2520. how these can be *generated* from their contents. This should suffice
  2521. to define the syntax, although it does not give a recipe for *parsing*
  2522. these constructions. (A recipe is provided below in the section entitled
  2523. [A parsing strategy](#appendix-a-parsing-strategy).)
  2524. ## Block quotes
  2525. A [block quote marker](@)
  2526. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2527. with a following space, or (b) a single character `>` not followed by a space.
  2528. The following rules define [block quotes]:
  2529. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2530. of blocks *Bs*, then the result of prepending a [block quote
  2531. marker] to the beginning of each line in *Ls*
  2532. is a [block quote](#block-quotes) containing *Bs*.
  2533. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2534. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2535. the initial [block quote marker] from one or
  2536. more lines in which the next [non-whitespace character] after the [block
  2537. quote marker] is [paragraph continuation
  2538. text] is a block quote with *Bs* as its content.
  2539. [Paragraph continuation text](@) is text
  2540. that will be parsed as part of the content of a paragraph, but does
  2541. not occur at the beginning of the paragraph.
  2542. 3. **Consecutiveness.** A document cannot contain two [block
  2543. quotes] in a row unless there is a [blank line] between them.
  2544. Nothing else counts as a [block quote](#block-quotes).
  2545. Here is a simple example:
  2546. ```````````````````````````````` example
  2547. > # Foo
  2548. > bar
  2549. > baz
  2550. .
  2551. <blockquote>
  2552. <h1>Foo</h1>
  2553. <p>bar
  2554. baz</p>
  2555. </blockquote>
  2556. ````````````````````````````````
  2557. The spaces after the `>` characters can be omitted:
  2558. ```````````````````````````````` example
  2559. ># Foo
  2560. >bar
  2561. > baz
  2562. .
  2563. <blockquote>
  2564. <h1>Foo</h1>
  2565. <p>bar
  2566. baz</p>
  2567. </blockquote>
  2568. ````````````````````````````````
  2569. The `>` characters can be indented 1-3 spaces:
  2570. ```````````````````````````````` example
  2571. > # Foo
  2572. > bar
  2573. > baz
  2574. .
  2575. <blockquote>
  2576. <h1>Foo</h1>
  2577. <p>bar
  2578. baz</p>
  2579. </blockquote>
  2580. ````````````````````````````````
  2581. Four spaces gives us a code block:
  2582. ```````````````````````````````` example
  2583. > # Foo
  2584. > bar
  2585. > baz
  2586. .
  2587. <pre><code>&gt; # Foo
  2588. &gt; bar
  2589. &gt; baz
  2590. </code></pre>
  2591. ````````````````````````````````
  2592. The Laziness clause allows us to omit the `>` before
  2593. [paragraph continuation text]:
  2594. ```````````````````````````````` example
  2595. > # Foo
  2596. > bar
  2597. baz
  2598. .
  2599. <blockquote>
  2600. <h1>Foo</h1>
  2601. <p>bar
  2602. baz</p>
  2603. </blockquote>
  2604. ````````````````````````````````
  2605. A block quote can contain some lazy and some non-lazy
  2606. continuation lines:
  2607. ```````````````````````````````` example
  2608. > bar
  2609. baz
  2610. > foo
  2611. .
  2612. <blockquote>
  2613. <p>bar
  2614. baz
  2615. foo</p>
  2616. </blockquote>
  2617. ````````````````````````````````
  2618. Laziness only applies to lines that would have been continuations of
  2619. paragraphs had they been prepended with [block quote markers].
  2620. For example, the `> ` cannot be omitted in the second line of
  2621. ``` markdown
  2622. > foo
  2623. > ---
  2624. ```
  2625. without changing the meaning:
  2626. ```````````````````````````````` example
  2627. > foo
  2628. ---
  2629. .
  2630. <blockquote>
  2631. <p>foo</p>
  2632. </blockquote>
  2633. <hr />
  2634. ````````````````````````````````
  2635. Similarly, if we omit the `> ` in the second line of
  2636. ``` markdown
  2637. > - foo
  2638. > - bar
  2639. ```
  2640. then the block quote ends after the first line:
  2641. ```````````````````````````````` example
  2642. > - foo
  2643. - bar
  2644. .
  2645. <blockquote>
  2646. <ul>
  2647. <li>foo</li>
  2648. </ul>
  2649. </blockquote>
  2650. <ul>
  2651. <li>bar</li>
  2652. </ul>
  2653. ````````````````````````````````
  2654. For the same reason, we can't omit the `> ` in front of
  2655. subsequent lines of an indented or fenced code block:
  2656. ```````````````````````````````` example
  2657. > foo
  2658. bar
  2659. .
  2660. <blockquote>
  2661. <pre><code>foo
  2662. </code></pre>
  2663. </blockquote>
  2664. <pre><code>bar
  2665. </code></pre>
  2666. ````````````````````````````````
  2667. ```````````````````````````````` example
  2668. > ```
  2669. foo
  2670. ```
  2671. .
  2672. <blockquote>
  2673. <pre><code></code></pre>
  2674. </blockquote>
  2675. <p>foo</p>
  2676. <pre><code></code></pre>
  2677. ````````````````````````````````
  2678. Note that in the following case, we have a [lazy
  2679. continuation line]:
  2680. ```````````````````````````````` example
  2681. > foo
  2682. - bar
  2683. .
  2684. <blockquote>
  2685. <p>foo
  2686. - bar</p>
  2687. </blockquote>
  2688. ````````````````````````````````
  2689. To see why, note that in
  2690. ```markdown
  2691. > foo
  2692. > - bar
  2693. ```
  2694. the `- bar` is indented too far to start a list, and can't
  2695. be an indented code block because indented code blocks cannot
  2696. interrupt paragraphs, so it is [paragraph continuation text].
  2697. A block quote can be empty:
  2698. ```````````````````````````````` example
  2699. >
  2700. .
  2701. <blockquote>
  2702. </blockquote>
  2703. ````````````````````````````````
  2704. ```````````````````````````````` example
  2705. >
  2706. >
  2707. >
  2708. .
  2709. <blockquote>
  2710. </blockquote>
  2711. ````````````````````````````````
  2712. A block quote can have initial or final blank lines:
  2713. ```````````````````````````````` example
  2714. >
  2715. > foo
  2716. >
  2717. .
  2718. <blockquote>
  2719. <p>foo</p>
  2720. </blockquote>
  2721. ````````````````````````````````
  2722. A blank line always separates block quotes:
  2723. ```````````````````````````````` example
  2724. > foo
  2725. > bar
  2726. .
  2727. <blockquote>
  2728. <p>foo</p>
  2729. </blockquote>
  2730. <blockquote>
  2731. <p>bar</p>
  2732. </blockquote>
  2733. ````````````````````````````````
  2734. (Most current Markdown implementations, including John Gruber's
  2735. original `Markdown.pl`, will parse this example as a single block quote
  2736. with two paragraphs. But it seems better to allow the author to decide
  2737. whether two block quotes or one are wanted.)
  2738. Consecutiveness means that if we put these block quotes together,
  2739. we get a single block quote:
  2740. ```````````````````````````````` example
  2741. > foo
  2742. > bar
  2743. .
  2744. <blockquote>
  2745. <p>foo
  2746. bar</p>
  2747. </blockquote>
  2748. ````````````````````````````````
  2749. To get a block quote with two paragraphs, use:
  2750. ```````````````````````````````` example
  2751. > foo
  2752. >
  2753. > bar
  2754. .
  2755. <blockquote>
  2756. <p>foo</p>
  2757. <p>bar</p>
  2758. </blockquote>
  2759. ````````````````````````````````
  2760. Block quotes can interrupt paragraphs:
  2761. ```````````````````````````````` example
  2762. foo
  2763. > bar
  2764. .
  2765. <p>foo</p>
  2766. <blockquote>
  2767. <p>bar</p>
  2768. </blockquote>
  2769. ````````````````````````````````
  2770. In general, blank lines are not needed before or after block
  2771. quotes:
  2772. ```````````````````````````````` example
  2773. > aaa
  2774. ***
  2775. > bbb
  2776. .
  2777. <blockquote>
  2778. <p>aaa</p>
  2779. </blockquote>
  2780. <hr />
  2781. <blockquote>
  2782. <p>bbb</p>
  2783. </blockquote>
  2784. ````````````````````````````````
  2785. However, because of laziness, a blank line is needed between
  2786. a block quote and a following paragraph:
  2787. ```````````````````````````````` example
  2788. > bar
  2789. baz
  2790. .
  2791. <blockquote>
  2792. <p>bar
  2793. baz</p>
  2794. </blockquote>
  2795. ````````````````````````````````
  2796. ```````````````````````````````` example
  2797. > bar
  2798. baz
  2799. .
  2800. <blockquote>
  2801. <p>bar</p>
  2802. </blockquote>
  2803. <p>baz</p>
  2804. ````````````````````````````````
  2805. ```````````````````````````````` example
  2806. > bar
  2807. >
  2808. baz
  2809. .
  2810. <blockquote>
  2811. <p>bar</p>
  2812. </blockquote>
  2813. <p>baz</p>
  2814. ````````````````````````````````
  2815. It is a consequence of the Laziness rule that any number
  2816. of initial `>`s may be omitted on a continuation line of a
  2817. nested block quote:
  2818. ```````````````````````````````` example
  2819. > > > foo
  2820. bar
  2821. .
  2822. <blockquote>
  2823. <blockquote>
  2824. <blockquote>
  2825. <p>foo
  2826. bar</p>
  2827. </blockquote>
  2828. </blockquote>
  2829. </blockquote>
  2830. ````````````````````````````````
  2831. ```````````````````````````````` example
  2832. >>> foo
  2833. > bar
  2834. >>baz
  2835. .
  2836. <blockquote>
  2837. <blockquote>
  2838. <blockquote>
  2839. <p>foo
  2840. bar
  2841. baz</p>
  2842. </blockquote>
  2843. </blockquote>
  2844. </blockquote>
  2845. ````````````````````````````````
  2846. When including an indented code block in a block quote,
  2847. remember that the [block quote marker] includes
  2848. both the `>` and a following space. So *five spaces* are needed after
  2849. the `>`:
  2850. ```````````````````````````````` example
  2851. > code
  2852. > not code
  2853. .
  2854. <blockquote>
  2855. <pre><code>code
  2856. </code></pre>
  2857. </blockquote>
  2858. <blockquote>
  2859. <p>not code</p>
  2860. </blockquote>
  2861. ````````````````````````````````
  2862. ## List items
  2863. A [list marker](@) is a
  2864. [bullet list marker] or an [ordered list marker].
  2865. A [bullet list marker](@)
  2866. is a `-`, `+`, or `*` character.
  2867. An [ordered list marker](@)
  2868. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2869. `.` character or a `)` character. (The reason for the length
  2870. limit is that with 10 digits we start seeing integer overflows
  2871. in some browsers.)
  2872. The following rules define [list items]:
  2873. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2874. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  2875. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2876. of prepending *M* and the following spaces to the first line of
  2877. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2878. list item with *Bs* as its contents. The type of the list item
  2879. (bullet or ordered) is determined by the type of its list marker.
  2880. If the list item is ordered, then it is also assigned a start
  2881. number, based on the ordered list marker.
  2882. Exceptions:
  2883. 1. When the first list item in a [list] interrupts
  2884. a paragraph---that is, when it starts on a line that would
  2885. otherwise count as [paragraph continuation text]---then (a)
  2886. the lines *Ls* must not begin with a blank line, and (b) if
  2887. the list item is ordered, the start number must be 1.
  2888. 2. If any line is a [thematic break][thematic breaks] then
  2889. that line is not a list item.
  2890. For example, let *Ls* be the lines
  2891. ```````````````````````````````` example
  2892. A paragraph
  2893. with two lines.
  2894. indented code
  2895. > A block quote.
  2896. .
  2897. <p>A paragraph
  2898. with two lines.</p>
  2899. <pre><code>indented code
  2900. </code></pre>
  2901. <blockquote>
  2902. <p>A block quote.</p>
  2903. </blockquote>
  2904. ````````````````````````````````
  2905. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2906. that the following is an ordered list item with start number 1,
  2907. and the same contents as *Ls*:
  2908. ```````````````````````````````` example
  2909. 1. A paragraph
  2910. with two lines.
  2911. indented code
  2912. > A block quote.
  2913. .
  2914. <ol>
  2915. <li>
  2916. <p>A paragraph
  2917. with two lines.</p>
  2918. <pre><code>indented code
  2919. </code></pre>
  2920. <blockquote>
  2921. <p>A block quote.</p>
  2922. </blockquote>
  2923. </li>
  2924. </ol>
  2925. ````````````````````````````````
  2926. The most important thing to notice is that the position of
  2927. the text after the list marker determines how much indentation
  2928. is needed in subsequent blocks in the list item. If the list
  2929. marker takes up two spaces, and there are three spaces between
  2930. the list marker and the next [non-whitespace character], then blocks
  2931. must be indented five spaces in order to fall under the list
  2932. item.
  2933. Here are some examples showing how far content must be indented to be
  2934. put under the list item:
  2935. ```````````````````````````````` example
  2936. - one
  2937. two
  2938. .
  2939. <ul>
  2940. <li>one</li>
  2941. </ul>
  2942. <p>two</p>
  2943. ````````````````````````````````
  2944. ```````````````````````````````` example
  2945. - one
  2946. two
  2947. .
  2948. <ul>
  2949. <li>
  2950. <p>one</p>
  2951. <p>two</p>
  2952. </li>
  2953. </ul>
  2954. ````````````````````````````````
  2955. ```````````````````````````````` example
  2956. - one
  2957. two
  2958. .
  2959. <ul>
  2960. <li>one</li>
  2961. </ul>
  2962. <pre><code> two
  2963. </code></pre>
  2964. ````````````````````````````````
  2965. ```````````````````````````````` example
  2966. - one
  2967. two
  2968. .
  2969. <ul>
  2970. <li>
  2971. <p>one</p>
  2972. <p>two</p>
  2973. </li>
  2974. </ul>
  2975. ````````````````````````````````
  2976. It is tempting to think of this in terms of columns: the continuation
  2977. blocks must be indented at least to the column of the first
  2978. [non-whitespace character] after the list marker. However, that is not quite right.
  2979. The spaces after the list marker determine how much relative indentation
  2980. is needed. Which column this indentation reaches will depend on
  2981. how the list item is embedded in other constructions, as shown by
  2982. this example:
  2983. ```````````````````````````````` example
  2984. > > 1. one
  2985. >>
  2986. >> two
  2987. .
  2988. <blockquote>
  2989. <blockquote>
  2990. <ol>
  2991. <li>
  2992. <p>one</p>
  2993. <p>two</p>
  2994. </li>
  2995. </ol>
  2996. </blockquote>
  2997. </blockquote>
  2998. ````````````````````````````````
  2999. Here `two` occurs in the same column as the list marker `1.`,
  3000. but is actually contained in the list item, because there is
  3001. sufficient indentation after the last containing blockquote marker.
  3002. The converse is also possible. In the following example, the word `two`
  3003. occurs far to the right of the initial text of the list item, `one`, but
  3004. it is not considered part of the list item, because it is not indented
  3005. far enough past the blockquote marker:
  3006. ```````````````````````````````` example
  3007. >>- one
  3008. >>
  3009. > > two
  3010. .
  3011. <blockquote>
  3012. <blockquote>
  3013. <ul>
  3014. <li>one</li>
  3015. </ul>
  3016. <p>two</p>
  3017. </blockquote>
  3018. </blockquote>
  3019. ````````````````````````````````
  3020. Note that at least one space is needed between the list marker and
  3021. any following content, so these are not list items:
  3022. ```````````````````````````````` example
  3023. -one
  3024. 2.two
  3025. .
  3026. <p>-one</p>
  3027. <p>2.two</p>
  3028. ````````````````````````````````
  3029. A list item may contain blocks that are separated by more than
  3030. one blank line.
  3031. ```````````````````````````````` example
  3032. - foo
  3033. bar
  3034. .
  3035. <ul>
  3036. <li>
  3037. <p>foo</p>
  3038. <p>bar</p>
  3039. </li>
  3040. </ul>
  3041. ````````````````````````````````
  3042. A list item may contain any kind of block:
  3043. ```````````````````````````````` example
  3044. 1. foo
  3045. ```
  3046. bar
  3047. ```
  3048. baz
  3049. > bam
  3050. .
  3051. <ol>
  3052. <li>
  3053. <p>foo</p>
  3054. <pre><code>bar
  3055. </code></pre>
  3056. <p>baz</p>
  3057. <blockquote>
  3058. <p>bam</p>
  3059. </blockquote>
  3060. </li>
  3061. </ol>
  3062. ````````````````````````````````
  3063. A list item that contains an indented code block will preserve
  3064. empty lines within the code block verbatim.
  3065. ```````````````````````````````` example
  3066. - Foo
  3067. bar
  3068. baz
  3069. .
  3070. <ul>
  3071. <li>
  3072. <p>Foo</p>
  3073. <pre><code>bar
  3074. baz
  3075. </code></pre>
  3076. </li>
  3077. </ul>
  3078. ````````````````````````````````
  3079. Note that ordered list start numbers must be nine digits or less:
  3080. ```````````````````````````````` example
  3081. 123456789. ok
  3082. .
  3083. <ol start="123456789">
  3084. <li>ok</li>
  3085. </ol>
  3086. ````````````````````````````````
  3087. ```````````````````````````````` example
  3088. 1234567890. not ok
  3089. .
  3090. <p>1234567890. not ok</p>
  3091. ````````````````````````````````
  3092. A start number may begin with 0s:
  3093. ```````````````````````````````` example
  3094. 0. ok
  3095. .
  3096. <ol start="0">
  3097. <li>ok</li>
  3098. </ol>
  3099. ````````````````````````````````
  3100. ```````````````````````````````` example
  3101. 003. ok
  3102. .
  3103. <ol start="3">
  3104. <li>ok</li>
  3105. </ol>
  3106. ````````````````````````````````
  3107. A start number may not be negative:
  3108. ```````````````````````````````` example
  3109. -1. not ok
  3110. .
  3111. <p>-1. not ok</p>
  3112. ````````````````````````````````
  3113. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3114. constitute a sequence of blocks *Bs* starting with an indented code
  3115. block, and *M* is a list marker of width *W* followed by
  3116. one space, then the result of prepending *M* and the following
  3117. space to the first line of *Ls*, and indenting subsequent lines of
  3118. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3119. If a line is empty, then it need not be indented. The type of the
  3120. list item (bullet or ordered) is determined by the type of its list
  3121. marker. If the list item is ordered, then it is also assigned a
  3122. start number, based on the ordered list marker.
  3123. An indented code block will have to be indented four spaces beyond
  3124. the edge of the region where text will be included in the list item.
  3125. In the following case that is 6 spaces:
  3126. ```````````````````````````````` example
  3127. - foo
  3128. bar
  3129. .
  3130. <ul>
  3131. <li>
  3132. <p>foo</p>
  3133. <pre><code>bar
  3134. </code></pre>
  3135. </li>
  3136. </ul>
  3137. ````````````````````````````````
  3138. And in this case it is 11 spaces:
  3139. ```````````````````````````````` example
  3140. 10. foo
  3141. bar
  3142. .
  3143. <ol start="10">
  3144. <li>
  3145. <p>foo</p>
  3146. <pre><code>bar
  3147. </code></pre>
  3148. </li>
  3149. </ol>
  3150. ````````````````````````````````
  3151. If the *first* block in the list item is an indented code block,
  3152. then by rule #2, the contents must be indented *one* space after the
  3153. list marker:
  3154. ```````````````````````````````` example
  3155. indented code
  3156. paragraph
  3157. more code
  3158. .
  3159. <pre><code>indented code
  3160. </code></pre>
  3161. <p>paragraph</p>
  3162. <pre><code>more code
  3163. </code></pre>
  3164. ````````````````````````````````
  3165. ```````````````````````````````` example
  3166. 1. indented code
  3167. paragraph
  3168. more code
  3169. .
  3170. <ol>
  3171. <li>
  3172. <pre><code>indented code
  3173. </code></pre>
  3174. <p>paragraph</p>
  3175. <pre><code>more code
  3176. </code></pre>
  3177. </li>
  3178. </ol>
  3179. ````````````````````````````````
  3180. Note that an additional space indent is interpreted as space
  3181. inside the code block:
  3182. ```````````````````````````````` example
  3183. 1. indented code
  3184. paragraph
  3185. more code
  3186. .
  3187. <ol>
  3188. <li>
  3189. <pre><code> indented code
  3190. </code></pre>
  3191. <p>paragraph</p>
  3192. <pre><code>more code
  3193. </code></pre>
  3194. </li>
  3195. </ol>
  3196. ````````````````````````````````
  3197. Note that rules #1 and #2 only apply to two cases: (a) cases
  3198. in which the lines to be included in a list item begin with a
  3199. [non-whitespace character], and (b) cases in which
  3200. they begin with an indented code
  3201. block. In a case like the following, where the first block begins with
  3202. a three-space indent, the rules do not allow us to form a list item by
  3203. indenting the whole thing and prepending a list marker:
  3204. ```````````````````````````````` example
  3205. foo
  3206. bar
  3207. .
  3208. <p>foo</p>
  3209. <p>bar</p>
  3210. ````````````````````````````````
  3211. ```````````````````````````````` example
  3212. - foo
  3213. bar
  3214. .
  3215. <ul>
  3216. <li>foo</li>
  3217. </ul>
  3218. <p>bar</p>
  3219. ````````````````````````````````
  3220. This is not a significant restriction, because when a block begins
  3221. with 1-3 spaces indent, the indentation can always be removed without
  3222. a change in interpretation, allowing rule #1 to be applied. So, in
  3223. the above case:
  3224. ```````````````````````````````` example
  3225. - foo
  3226. bar
  3227. .
  3228. <ul>
  3229. <li>
  3230. <p>foo</p>
  3231. <p>bar</p>
  3232. </li>
  3233. </ul>
  3234. ````````````````````````````````
  3235. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3236. starting with a single [blank line] constitute a (possibly empty)
  3237. sequence of blocks *Bs*, not separated from each other by more than
  3238. one blank line, and *M* is a list marker of width *W*,
  3239. then the result of prepending *M* to the first line of *Ls*, and
  3240. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3241. item with *Bs* as its contents.
  3242. If a line is empty, then it need not be indented. The type of the
  3243. list item (bullet or ordered) is determined by the type of its list
  3244. marker. If the list item is ordered, then it is also assigned a
  3245. start number, based on the ordered list marker.
  3246. Here are some list items that start with a blank line but are not empty:
  3247. ```````````````````````````````` example
  3248. -
  3249. foo
  3250. -
  3251. ```
  3252. bar
  3253. ```
  3254. -
  3255. baz
  3256. .
  3257. <ul>
  3258. <li>foo</li>
  3259. <li>
  3260. <pre><code>bar
  3261. </code></pre>
  3262. </li>
  3263. <li>
  3264. <pre><code>baz
  3265. </code></pre>
  3266. </li>
  3267. </ul>
  3268. ````````````````````````````````
  3269. When the list item starts with a blank line, the number of spaces
  3270. following the list marker doesn't change the required indentation:
  3271. ```````````````````````````````` example
  3272. -
  3273. foo
  3274. .
  3275. <ul>
  3276. <li>foo</li>
  3277. </ul>
  3278. ````````````````````````````````
  3279. A list item can begin with at most one blank line.
  3280. In the following example, `foo` is not part of the list
  3281. item:
  3282. ```````````````````````````````` example
  3283. -
  3284. foo
  3285. .
  3286. <ul>
  3287. <li></li>
  3288. </ul>
  3289. <p>foo</p>
  3290. ````````````````````````````````
  3291. Here is an empty bullet list item:
  3292. ```````````````````````````````` example
  3293. - foo
  3294. -
  3295. - bar
  3296. .
  3297. <ul>
  3298. <li>foo</li>
  3299. <li></li>
  3300. <li>bar</li>
  3301. </ul>
  3302. ````````````````````````````````
  3303. It does not matter whether there are spaces following the [list marker]:
  3304. ```````````````````````````````` example
  3305. - foo
  3306. -
  3307. - bar
  3308. .
  3309. <ul>
  3310. <li>foo</li>
  3311. <li></li>
  3312. <li>bar</li>
  3313. </ul>
  3314. ````````````````````````````````
  3315. Here is an empty ordered list item:
  3316. ```````````````````````````````` example
  3317. 1. foo
  3318. 2.
  3319. 3. bar
  3320. .
  3321. <ol>
  3322. <li>foo</li>
  3323. <li></li>
  3324. <li>bar</li>
  3325. </ol>
  3326. ````````````````````````````````
  3327. A list may start or end with an empty list item:
  3328. ```````````````````````````````` example
  3329. *
  3330. .
  3331. <ul>
  3332. <li></li>
  3333. </ul>
  3334. ````````````````````````````````
  3335. However, an empty list item cannot interrupt a paragraph:
  3336. ```````````````````````````````` example
  3337. foo
  3338. *
  3339. foo
  3340. 1.
  3341. .
  3342. <p>foo
  3343. *</p>
  3344. <p>foo
  3345. 1.</p>
  3346. ````````````````````````````````
  3347. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3348. according to rule #1, #2, or #3, then the result of indenting each line
  3349. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3350. list item with the same contents and attributes. If a line is
  3351. empty, then it need not be indented.
  3352. Indented one space:
  3353. ```````````````````````````````` example
  3354. 1. A paragraph
  3355. with two lines.
  3356. indented code
  3357. > A block quote.
  3358. .
  3359. <ol>
  3360. <li>
  3361. <p>A paragraph
  3362. with two lines.</p>
  3363. <pre><code>indented code
  3364. </code></pre>
  3365. <blockquote>
  3366. <p>A block quote.</p>
  3367. </blockquote>
  3368. </li>
  3369. </ol>
  3370. ````````````````````````````````
  3371. Indented two spaces:
  3372. ```````````````````````````````` example
  3373. 1. A paragraph
  3374. with two lines.
  3375. indented code
  3376. > A block quote.
  3377. .
  3378. <ol>
  3379. <li>
  3380. <p>A paragraph
  3381. with two lines.</p>
  3382. <pre><code>indented code
  3383. </code></pre>
  3384. <blockquote>
  3385. <p>A block quote.</p>
  3386. </blockquote>
  3387. </li>
  3388. </ol>
  3389. ````````````````````````````````
  3390. Indented three spaces:
  3391. ```````````````````````````````` example
  3392. 1. A paragraph
  3393. with two lines.
  3394. indented code
  3395. > A block quote.
  3396. .
  3397. <ol>
  3398. <li>
  3399. <p>A paragraph
  3400. with two lines.</p>
  3401. <pre><code>indented code
  3402. </code></pre>
  3403. <blockquote>
  3404. <p>A block quote.</p>
  3405. </blockquote>
  3406. </li>
  3407. </ol>
  3408. ````````````````````````````````
  3409. Four spaces indent gives a code block:
  3410. ```````````````````````````````` example
  3411. 1. A paragraph
  3412. with two lines.
  3413. indented code
  3414. > A block quote.
  3415. .
  3416. <pre><code>1. A paragraph
  3417. with two lines.
  3418. indented code
  3419. &gt; A block quote.
  3420. </code></pre>
  3421. ````````````````````````````````
  3422. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3423. item](#list-items) with contents *Bs*, then the result of deleting
  3424. some or all of the indentation from one or more lines in which the
  3425. next [non-whitespace character] after the indentation is
  3426. [paragraph continuation text] is a
  3427. list item with the same contents and attributes. The unindented
  3428. lines are called
  3429. [lazy continuation line](@)s.
  3430. Here is an example with [lazy continuation lines]:
  3431. ```````````````````````````````` example
  3432. 1. A paragraph
  3433. with two lines.
  3434. indented code
  3435. > A block quote.
  3436. .
  3437. <ol>
  3438. <li>
  3439. <p>A paragraph
  3440. with two lines.</p>
  3441. <pre><code>indented code
  3442. </code></pre>
  3443. <blockquote>
  3444. <p>A block quote.</p>
  3445. </blockquote>
  3446. </li>
  3447. </ol>
  3448. ````````````````````````````````
  3449. Indentation can be partially deleted:
  3450. ```````````````````````````````` example
  3451. 1. A paragraph
  3452. with two lines.
  3453. .
  3454. <ol>
  3455. <li>A paragraph
  3456. with two lines.</li>
  3457. </ol>
  3458. ````````````````````````````````
  3459. These examples show how laziness can work in nested structures:
  3460. ```````````````````````````````` example
  3461. > 1. > Blockquote
  3462. continued here.
  3463. .
  3464. <blockquote>
  3465. <ol>
  3466. <li>
  3467. <blockquote>
  3468. <p>Blockquote
  3469. continued here.</p>
  3470. </blockquote>
  3471. </li>
  3472. </ol>
  3473. </blockquote>
  3474. ````````````````````````````````
  3475. ```````````````````````````````` example
  3476. > 1. > Blockquote
  3477. > continued here.
  3478. .
  3479. <blockquote>
  3480. <ol>
  3481. <li>
  3482. <blockquote>
  3483. <p>Blockquote
  3484. continued here.</p>
  3485. </blockquote>
  3486. </li>
  3487. </ol>
  3488. </blockquote>
  3489. ````````````````````````````````
  3490. 6. **That's all.** Nothing that is not counted as a list item by rules
  3491. #1--5 counts as a [list item](#list-items).
  3492. The rules for sublists follow from the general rules
  3493. [above][List items]. A sublist must be indented the same number
  3494. of spaces a paragraph would need to be in order to be included
  3495. in the list item.
  3496. So, in this case we need two spaces indent:
  3497. ```````````````````````````````` example
  3498. - foo
  3499. - bar
  3500. - baz
  3501. - boo
  3502. .
  3503. <ul>
  3504. <li>foo
  3505. <ul>
  3506. <li>bar
  3507. <ul>
  3508. <li>baz
  3509. <ul>
  3510. <li>boo</li>
  3511. </ul>
  3512. </li>
  3513. </ul>
  3514. </li>
  3515. </ul>
  3516. </li>
  3517. </ul>
  3518. ````````````````````````````````
  3519. One is not enough:
  3520. ```````````````````````````````` example
  3521. - foo
  3522. - bar
  3523. - baz
  3524. - boo
  3525. .
  3526. <ul>
  3527. <li>foo</li>
  3528. <li>bar</li>
  3529. <li>baz</li>
  3530. <li>boo</li>
  3531. </ul>
  3532. ````````````````````````````````
  3533. Here we need four, because the list marker is wider:
  3534. ```````````````````````````````` example
  3535. 10) foo
  3536. - bar
  3537. .
  3538. <ol start="10">
  3539. <li>foo
  3540. <ul>
  3541. <li>bar</li>
  3542. </ul>
  3543. </li>
  3544. </ol>
  3545. ````````````````````````````````
  3546. Three is not enough:
  3547. ```````````````````````````````` example
  3548. 10) foo
  3549. - bar
  3550. .
  3551. <ol start="10">
  3552. <li>foo</li>
  3553. </ol>
  3554. <ul>
  3555. <li>bar</li>
  3556. </ul>
  3557. ````````````````````````````````
  3558. A list may be the first block in a list item:
  3559. ```````````````````````````````` example
  3560. - - foo
  3561. .
  3562. <ul>
  3563. <li>
  3564. <ul>
  3565. <li>foo</li>
  3566. </ul>
  3567. </li>
  3568. </ul>
  3569. ````````````````````````````````
  3570. ```````````````````````````````` example
  3571. 1. - 2. foo
  3572. .
  3573. <ol>
  3574. <li>
  3575. <ul>
  3576. <li>
  3577. <ol start="2">
  3578. <li>foo</li>
  3579. </ol>
  3580. </li>
  3581. </ul>
  3582. </li>
  3583. </ol>
  3584. ````````````````````````````````
  3585. A list item can contain a heading:
  3586. ```````````````````````````````` example
  3587. - # Foo
  3588. - Bar
  3589. ---
  3590. baz
  3591. .
  3592. <ul>
  3593. <li>
  3594. <h1>Foo</h1>
  3595. </li>
  3596. <li>
  3597. <h2>Bar</h2>
  3598. baz</li>
  3599. </ul>
  3600. ````````````````````````````````
  3601. ### Motivation
  3602. John Gruber's Markdown spec says the following about list items:
  3603. 1. "List markers typically start at the left margin, but may be indented
  3604. by up to three spaces. List markers must be followed by one or more
  3605. spaces or a tab."
  3606. 2. "To make lists look nice, you can wrap items with hanging indents....
  3607. But if you don't want to, you don't have to."
  3608. 3. "List items may consist of multiple paragraphs. Each subsequent
  3609. paragraph in a list item must be indented by either 4 spaces or one
  3610. tab."
  3611. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3612. but here again, Markdown will allow you to be lazy."
  3613. 5. "To put a blockquote within a list item, the blockquote's `>`
  3614. delimiters need to be indented."
  3615. 6. "To put a code block within a list item, the code block needs to be
  3616. indented twice — 8 spaces or two tabs."
  3617. These rules specify that a paragraph under a list item must be indented
  3618. four spaces (presumably, from the left margin, rather than the start of
  3619. the list marker, but this is not said), and that code under a list item
  3620. must be indented eight spaces instead of the usual four. They also say
  3621. that a block quote must be indented, but not by how much; however, the
  3622. example given has four spaces indentation. Although nothing is said
  3623. about other kinds of block-level content, it is certainly reasonable to
  3624. infer that *all* block elements under a list item, including other
  3625. lists, must be indented four spaces. This principle has been called the
  3626. *four-space rule*.
  3627. The four-space rule is clear and principled, and if the reference
  3628. implementation `Markdown.pl` had followed it, it probably would have
  3629. become the standard. However, `Markdown.pl` allowed paragraphs and
  3630. sublists to start with only two spaces indentation, at least on the
  3631. outer level. Worse, its behavior was inconsistent: a sublist of an
  3632. outer-level list needed two spaces indentation, but a sublist of this
  3633. sublist needed three spaces. It is not surprising, then, that different
  3634. implementations of Markdown have developed very different rules for
  3635. determining what comes under a list item. (Pandoc and python-Markdown,
  3636. for example, stuck with Gruber's syntax description and the four-space
  3637. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3638. followed `Markdown.pl`'s behavior more closely.)
  3639. Unfortunately, given the divergences between implementations, there
  3640. is no way to give a spec for list items that will be guaranteed not
  3641. to break any existing documents. However, the spec given here should
  3642. correctly handle lists formatted with either the four-space rule or
  3643. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3644. in a way that is natural for a human to read.
  3645. The strategy here is to let the width and indentation of the list marker
  3646. determine the indentation necessary for blocks to fall under the list
  3647. item, rather than having a fixed and arbitrary number. The writer can
  3648. think of the body of the list item as a unit which gets indented to the
  3649. right enough to fit the list marker (and any indentation on the list
  3650. marker). (The laziness rule, #5, then allows continuation lines to be
  3651. unindented if needed.)
  3652. This rule is superior, we claim, to any rule requiring a fixed level of
  3653. indentation from the margin. The four-space rule is clear but
  3654. unnatural. It is quite unintuitive that
  3655. ``` markdown
  3656. - foo
  3657. bar
  3658. - baz
  3659. ```
  3660. should be parsed as two lists with an intervening paragraph,
  3661. ``` html
  3662. <ul>
  3663. <li>foo</li>
  3664. </ul>
  3665. <p>bar</p>
  3666. <ul>
  3667. <li>baz</li>
  3668. </ul>
  3669. ```
  3670. as the four-space rule demands, rather than a single list,
  3671. ``` html
  3672. <ul>
  3673. <li>
  3674. <p>foo</p>
  3675. <p>bar</p>
  3676. <ul>
  3677. <li>baz</li>
  3678. </ul>
  3679. </li>
  3680. </ul>
  3681. ```
  3682. The choice of four spaces is arbitrary. It can be learned, but it is
  3683. not likely to be guessed, and it trips up beginners regularly.
  3684. Would it help to adopt a two-space rule? The problem is that such
  3685. a rule, together with the rule allowing 1--3 spaces indentation of the
  3686. initial list marker, allows text that is indented *less than* the
  3687. original list marker to be included in the list item. For example,
  3688. `Markdown.pl` parses
  3689. ``` markdown
  3690. - one
  3691. two
  3692. ```
  3693. as a single list item, with `two` a continuation paragraph:
  3694. ``` html
  3695. <ul>
  3696. <li>
  3697. <p>one</p>
  3698. <p>two</p>
  3699. </li>
  3700. </ul>
  3701. ```
  3702. and similarly
  3703. ``` markdown
  3704. > - one
  3705. >
  3706. > two
  3707. ```
  3708. as
  3709. ``` html
  3710. <blockquote>
  3711. <ul>
  3712. <li>
  3713. <p>one</p>
  3714. <p>two</p>
  3715. </li>
  3716. </ul>
  3717. </blockquote>
  3718. ```
  3719. This is extremely unintuitive.
  3720. Rather than requiring a fixed indent from the margin, we could require
  3721. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3722. may itself be indented). This proposal would remove the last anomaly
  3723. discussed. Unlike the spec presented above, it would count the following
  3724. as a list item with a subparagraph, even though the paragraph `bar`
  3725. is not indented as far as the first paragraph `foo`:
  3726. ``` markdown
  3727. 10. foo
  3728. bar
  3729. ```
  3730. Arguably this text does read like a list item with `bar` as a subparagraph,
  3731. which may count in favor of the proposal. However, on this proposal indented
  3732. code would have to be indented six spaces after the list marker. And this
  3733. would break a lot of existing Markdown, which has the pattern:
  3734. ``` markdown
  3735. 1. foo
  3736. indented code
  3737. ```
  3738. where the code is indented eight spaces. The spec above, by contrast, will
  3739. parse this text as expected, since the code block's indentation is measured
  3740. from the beginning of `foo`.
  3741. The one case that needs special treatment is a list item that *starts*
  3742. with indented code. How much indentation is required in that case, since
  3743. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3744. that in such cases, we require one space indentation from the list marker
  3745. (and then the normal four spaces for the indented code). This will match the
  3746. four-space rule in cases where the list marker plus its initial indentation
  3747. takes four spaces (a common case), but diverge in other cases.
  3748. ## Lists
  3749. A [list](@) is a sequence of one or more
  3750. list items [of the same type]. The list items
  3751. may be separated by any number of blank lines.
  3752. Two list items are [of the same type](@)
  3753. if they begin with a [list marker] of the same type.
  3754. Two list markers are of the
  3755. same type if (a) they are bullet list markers using the same character
  3756. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3757. delimiter (either `.` or `)`).
  3758. A list is an [ordered list](@)
  3759. if its constituent list items begin with
  3760. [ordered list markers], and a
  3761. [bullet list](@) if its constituent list
  3762. items begin with [bullet list markers].
  3763. The [start number](@)
  3764. of an [ordered list] is determined by the list number of
  3765. its initial list item. The numbers of subsequent list items are
  3766. disregarded.
  3767. A list is [loose](@) if any of its constituent
  3768. list items are separated by blank lines, or if any of its constituent
  3769. list items directly contain two block-level elements with a blank line
  3770. between them. Otherwise a list is [tight](@).
  3771. (The difference in HTML output is that paragraphs in a loose list are
  3772. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3773. Changing the bullet or ordered list delimiter starts a new list:
  3774. ```````````````````````````````` example
  3775. - foo
  3776. - bar
  3777. + baz
  3778. .
  3779. <ul>
  3780. <li>foo</li>
  3781. <li>bar</li>
  3782. </ul>
  3783. <ul>
  3784. <li>baz</li>
  3785. </ul>
  3786. ````````````````````````````````
  3787. ```````````````````````````````` example
  3788. 1. foo
  3789. 2. bar
  3790. 3) baz
  3791. .
  3792. <ol>
  3793. <li>foo</li>
  3794. <li>bar</li>
  3795. </ol>
  3796. <ol start="3">
  3797. <li>baz</li>
  3798. </ol>
  3799. ````````````````````````````````
  3800. In CommonMark, a list can interrupt a paragraph. That is,
  3801. no blank line is needed to separate a paragraph from a following
  3802. list:
  3803. ```````````````````````````````` example
  3804. Foo
  3805. - bar
  3806. - baz
  3807. .
  3808. <p>Foo</p>
  3809. <ul>
  3810. <li>bar</li>
  3811. <li>baz</li>
  3812. </ul>
  3813. ````````````````````````````````
  3814. `Markdown.pl` does not allow this, through fear of triggering a list
  3815. via a numeral in a hard-wrapped line:
  3816. ``` markdown
  3817. The number of windows in my house is
  3818. 14. The number of doors is 6.
  3819. ```
  3820. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3821. interrupt a paragraph, even though the same considerations might
  3822. apply.
  3823. In CommonMark, we do allow lists to interrupt paragraphs, for
  3824. two reasons. First, it is natural and not uncommon for people
  3825. to start lists without blank lines:
  3826. ``` markdown
  3827. I need to buy
  3828. - new shoes
  3829. - a coat
  3830. - a plane ticket
  3831. ```
  3832. Second, we are attracted to a
  3833. > [principle of uniformity](@):
  3834. > if a chunk of text has a certain
  3835. > meaning, it will continue to have the same meaning when put into a
  3836. > container block (such as a list item or blockquote).
  3837. (Indeed, the spec for [list items] and [block quotes] presupposes
  3838. this principle.) This principle implies that if
  3839. ``` markdown
  3840. * I need to buy
  3841. - new shoes
  3842. - a coat
  3843. - a plane ticket
  3844. ```
  3845. is a list item containing a paragraph followed by a nested sublist,
  3846. as all Markdown implementations agree it is (though the paragraph
  3847. may be rendered without `<p>` tags, since the list is "tight"),
  3848. then
  3849. ``` markdown
  3850. I need to buy
  3851. - new shoes
  3852. - a coat
  3853. - a plane ticket
  3854. ```
  3855. by itself should be a paragraph followed by a nested sublist.
  3856. Since it is well established Markdown practice to allow lists to
  3857. interrupt paragraphs inside list items, the [principle of
  3858. uniformity] requires us to allow this outside list items as
  3859. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3860. takes a different approach, requiring blank lines before lists
  3861. even inside other list items.)
  3862. In order to solve of unwanted lists in paragraphs with
  3863. hard-wrapped numerals, we allow only lists starting with `1` to
  3864. interrupt paragraphs. Thus,
  3865. ```````````````````````````````` example
  3866. The number of windows in my house is
  3867. 14. The number of doors is 6.
  3868. .
  3869. <p>The number of windows in my house is
  3870. 14. The number of doors is 6.</p>
  3871. ````````````````````````````````
  3872. We may still get an unintended result in cases like
  3873. ```````````````````````````````` example
  3874. The number of windows in my house is
  3875. 1. The number of doors is 6.
  3876. .
  3877. <p>The number of windows in my house is</p>
  3878. <ol>
  3879. <li>The number of doors is 6.</li>
  3880. </ol>
  3881. ````````````````````````````````
  3882. but this rule should prevent most spurious list captures.
  3883. There can be any number of blank lines between items:
  3884. ```````````````````````````````` example
  3885. - foo
  3886. - bar
  3887. - baz
  3888. .
  3889. <ul>
  3890. <li>
  3891. <p>foo</p>
  3892. </li>
  3893. <li>
  3894. <p>bar</p>
  3895. </li>
  3896. <li>
  3897. <p>baz</p>
  3898. </li>
  3899. </ul>
  3900. ````````````````````````````````
  3901. ```````````````````````````````` example
  3902. - foo
  3903. - bar
  3904. - baz
  3905. bim
  3906. .
  3907. <ul>
  3908. <li>foo
  3909. <ul>
  3910. <li>bar
  3911. <ul>
  3912. <li>
  3913. <p>baz</p>
  3914. <p>bim</p>
  3915. </li>
  3916. </ul>
  3917. </li>
  3918. </ul>
  3919. </li>
  3920. </ul>
  3921. ````````````````````````````````
  3922. To separate consecutive lists of the same type, or to separate a
  3923. list from an indented code block that would otherwise be parsed
  3924. as a subparagraph of the final list item, you can insert a blank HTML
  3925. comment:
  3926. ```````````````````````````````` example
  3927. - foo
  3928. - bar
  3929. <!-- -->
  3930. - baz
  3931. - bim
  3932. .
  3933. <ul>
  3934. <li>foo</li>
  3935. <li>bar</li>
  3936. </ul>
  3937. <!-- -->
  3938. <ul>
  3939. <li>baz</li>
  3940. <li>bim</li>
  3941. </ul>
  3942. ````````````````````````````````
  3943. ```````````````````````````````` example
  3944. - foo
  3945. notcode
  3946. - foo
  3947. <!-- -->
  3948. code
  3949. .
  3950. <ul>
  3951. <li>
  3952. <p>foo</p>
  3953. <p>notcode</p>
  3954. </li>
  3955. <li>
  3956. <p>foo</p>
  3957. </li>
  3958. </ul>
  3959. <!-- -->
  3960. <pre><code>code
  3961. </code></pre>
  3962. ````````````````````````````````
  3963. List items need not be indented to the same level. The following
  3964. list items will be treated as items at the same list level,
  3965. since none is indented enough to belong to the previous list
  3966. item:
  3967. ```````````````````````````````` example
  3968. - a
  3969. - b
  3970. - c
  3971. - d
  3972. - e
  3973. - f
  3974. - g
  3975. .
  3976. <ul>
  3977. <li>a</li>
  3978. <li>b</li>
  3979. <li>c</li>
  3980. <li>d</li>
  3981. <li>e</li>
  3982. <li>f</li>
  3983. <li>g</li>
  3984. </ul>
  3985. ````````````````````````````````
  3986. ```````````````````````````````` example
  3987. 1. a
  3988. 2. b
  3989. 3. c
  3990. .
  3991. <ol>
  3992. <li>
  3993. <p>a</p>
  3994. </li>
  3995. <li>
  3996. <p>b</p>
  3997. </li>
  3998. <li>
  3999. <p>c</p>
  4000. </li>
  4001. </ol>
  4002. ````````````````````````````````
  4003. Note, however, that list items may not be indented more than
  4004. three spaces. Here `- e` is treated as a paragraph continuation
  4005. line, because it is indented more than three spaces:
  4006. ```````````````````````````````` example
  4007. - a
  4008. - b
  4009. - c
  4010. - d
  4011. - e
  4012. .
  4013. <ul>
  4014. <li>a</li>
  4015. <li>b</li>
  4016. <li>c</li>
  4017. <li>d
  4018. - e</li>
  4019. </ul>
  4020. ````````````````````````````````
  4021. And here, `3. c` is treated as in indented code block,
  4022. because it is indented four spaces and preceded by a
  4023. blank line.
  4024. ```````````````````````````````` example
  4025. 1. a
  4026. 2. b
  4027. 3. c
  4028. .
  4029. <ol>
  4030. <li>
  4031. <p>a</p>
  4032. </li>
  4033. <li>
  4034. <p>b</p>
  4035. </li>
  4036. </ol>
  4037. <pre><code>3. c
  4038. </code></pre>
  4039. ````````````````````````````````
  4040. This is a loose list, because there is a blank line between
  4041. two of the list items:
  4042. ```````````````````````````````` example
  4043. - a
  4044. - b
  4045. - c
  4046. .
  4047. <ul>
  4048. <li>
  4049. <p>a</p>
  4050. </li>
  4051. <li>
  4052. <p>b</p>
  4053. </li>
  4054. <li>
  4055. <p>c</p>
  4056. </li>
  4057. </ul>
  4058. ````````````````````````````````
  4059. So is this, with a empty second item:
  4060. ```````````````````````````````` example
  4061. * a
  4062. *
  4063. * c
  4064. .
  4065. <ul>
  4066. <li>
  4067. <p>a</p>
  4068. </li>
  4069. <li></li>
  4070. <li>
  4071. <p>c</p>
  4072. </li>
  4073. </ul>
  4074. ````````````````````````````````
  4075. These are loose lists, even though there is no space between the items,
  4076. because one of the items directly contains two block-level elements
  4077. with a blank line between them:
  4078. ```````````````````````````````` example
  4079. - a
  4080. - b
  4081. c
  4082. - d
  4083. .
  4084. <ul>
  4085. <li>
  4086. <p>a</p>
  4087. </li>
  4088. <li>
  4089. <p>b</p>
  4090. <p>c</p>
  4091. </li>
  4092. <li>
  4093. <p>d</p>
  4094. </li>
  4095. </ul>
  4096. ````````````````````````````````
  4097. ```````````````````````````````` example
  4098. - a
  4099. - b
  4100. [ref]: /url
  4101. - d
  4102. .
  4103. <ul>
  4104. <li>
  4105. <p>a</p>
  4106. </li>
  4107. <li>
  4108. <p>b</p>
  4109. </li>
  4110. <li>
  4111. <p>d</p>
  4112. </li>
  4113. </ul>
  4114. ````````````````````````````````
  4115. This is a tight list, because the blank lines are in a code block:
  4116. ```````````````````````````````` example
  4117. - a
  4118. - ```
  4119. b
  4120. ```
  4121. - c
  4122. .
  4123. <ul>
  4124. <li>a</li>
  4125. <li>
  4126. <pre><code>b
  4127. </code></pre>
  4128. </li>
  4129. <li>c</li>
  4130. </ul>
  4131. ````````````````````````````````
  4132. This is a tight list, because the blank line is between two
  4133. paragraphs of a sublist. So the sublist is loose while
  4134. the outer list is tight:
  4135. ```````````````````````````````` example
  4136. - a
  4137. - b
  4138. c
  4139. - d
  4140. .
  4141. <ul>
  4142. <li>a
  4143. <ul>
  4144. <li>
  4145. <p>b</p>
  4146. <p>c</p>
  4147. </li>
  4148. </ul>
  4149. </li>
  4150. <li>d</li>
  4151. </ul>
  4152. ````````````````````````````````
  4153. This is a tight list, because the blank line is inside the
  4154. block quote:
  4155. ```````````````````````````````` example
  4156. * a
  4157. > b
  4158. >
  4159. * c
  4160. .
  4161. <ul>
  4162. <li>a
  4163. <blockquote>
  4164. <p>b</p>
  4165. </blockquote>
  4166. </li>
  4167. <li>c</li>
  4168. </ul>
  4169. ````````````````````````````````
  4170. This list is tight, because the consecutive block elements
  4171. are not separated by blank lines:
  4172. ```````````````````````````````` example
  4173. - a
  4174. > b
  4175. ```
  4176. c
  4177. ```
  4178. - d
  4179. .
  4180. <ul>
  4181. <li>a
  4182. <blockquote>
  4183. <p>b</p>
  4184. </blockquote>
  4185. <pre><code>c
  4186. </code></pre>
  4187. </li>
  4188. <li>d</li>
  4189. </ul>
  4190. ````````````````````````````````
  4191. A single-paragraph list is tight:
  4192. ```````````````````````````````` example
  4193. - a
  4194. .
  4195. <ul>
  4196. <li>a</li>
  4197. </ul>
  4198. ````````````````````````````````
  4199. ```````````````````````````````` example
  4200. - a
  4201. - b
  4202. .
  4203. <ul>
  4204. <li>a
  4205. <ul>
  4206. <li>b</li>
  4207. </ul>
  4208. </li>
  4209. </ul>
  4210. ````````````````````````````````
  4211. This list is loose, because of the blank line between the
  4212. two block elements in the list item:
  4213. ```````````````````````````````` example
  4214. 1. ```
  4215. foo
  4216. ```
  4217. bar
  4218. .
  4219. <ol>
  4220. <li>
  4221. <pre><code>foo
  4222. </code></pre>
  4223. <p>bar</p>
  4224. </li>
  4225. </ol>
  4226. ````````````````````````````````
  4227. Here the outer list is loose, the inner list tight:
  4228. ```````````````````````````````` example
  4229. * foo
  4230. * bar
  4231. baz
  4232. .
  4233. <ul>
  4234. <li>
  4235. <p>foo</p>
  4236. <ul>
  4237. <li>bar</li>
  4238. </ul>
  4239. <p>baz</p>
  4240. </li>
  4241. </ul>
  4242. ````````````````````````````````
  4243. ```````````````````````````````` example
  4244. - a
  4245. - b
  4246. - c
  4247. - d
  4248. - e
  4249. - f
  4250. .
  4251. <ul>
  4252. <li>
  4253. <p>a</p>
  4254. <ul>
  4255. <li>b</li>
  4256. <li>c</li>
  4257. </ul>
  4258. </li>
  4259. <li>
  4260. <p>d</p>
  4261. <ul>
  4262. <li>e</li>
  4263. <li>f</li>
  4264. </ul>
  4265. </li>
  4266. </ul>
  4267. ````````````````````````````````
  4268. # Inlines
  4269. Inlines are parsed sequentially from the beginning of the character
  4270. stream to the end (left to right, in left-to-right languages).
  4271. Thus, for example, in
  4272. ```````````````````````````````` example
  4273. `hi`lo`
  4274. .
  4275. <p><code>hi</code>lo`</p>
  4276. ````````````````````````````````
  4277. `hi` is parsed as code, leaving the backtick at the end as a literal
  4278. backtick.
  4279. ## Backslash escapes
  4280. Any ASCII punctuation character may be backslash-escaped:
  4281. ```````````````````````````````` example
  4282. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4283. .
  4284. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4285. ````````````````````````````````
  4286. Backslashes before other characters are treated as literal
  4287. backslashes:
  4288. ```````````````````````````````` example
  4289. \→\A\a\ \3\φ\«
  4290. .
  4291. <p>\→\A\a\ \3\φ\«</p>
  4292. ````````````````````````````````
  4293. Escaped characters are treated as regular characters and do
  4294. not have their usual Markdown meanings:
  4295. ```````````````````````````````` example
  4296. \*not emphasized*
  4297. \<br/> not a tag
  4298. \[not a link](/foo)
  4299. \`not code`
  4300. 1\. not a list
  4301. \* not a list
  4302. \# not a heading
  4303. \[foo]: /url "not a reference"
  4304. \&ouml; not a character entity
  4305. .
  4306. <p>*not emphasized*
  4307. &lt;br/&gt; not a tag
  4308. [not a link](/foo)
  4309. `not code`
  4310. 1. not a list
  4311. * not a list
  4312. # not a heading
  4313. [foo]: /url &quot;not a reference&quot;
  4314. &amp;ouml; not a character entity</p>
  4315. ````````````````````````````````
  4316. If a backslash is itself escaped, the following character is not:
  4317. ```````````````````````````````` example
  4318. \\*emphasis*
  4319. .
  4320. <p>\<em>emphasis</em></p>
  4321. ````````````````````````````````
  4322. A backslash at the end of the line is a [hard line break]:
  4323. ```````````````````````````````` example
  4324. foo\
  4325. bar
  4326. .
  4327. <p>foo<br />
  4328. bar</p>
  4329. ````````````````````````````````
  4330. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4331. raw HTML:
  4332. ```````````````````````````````` example
  4333. `` \[\` ``
  4334. .
  4335. <p><code>\[\`</code></p>
  4336. ````````````````````````````````
  4337. ```````````````````````````````` example
  4338. \[\]
  4339. .
  4340. <pre><code>\[\]
  4341. </code></pre>
  4342. ````````````````````````````````
  4343. ```````````````````````````````` example
  4344. ~~~
  4345. \[\]
  4346. ~~~
  4347. .
  4348. <pre><code>\[\]
  4349. </code></pre>
  4350. ````````````````````````````````
  4351. ```````````````````````````````` example
  4352. <http://example.com?find=\*>
  4353. .
  4354. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4355. ````````````````````````````````
  4356. ```````````````````````````````` example
  4357. <a href="/bar\/)">
  4358. .
  4359. <a href="/bar\/)">
  4360. ````````````````````````````````
  4361. But they work in all other contexts, including URLs and link titles,
  4362. link references, and [info strings] in [fenced code blocks]:
  4363. ```````````````````````````````` example
  4364. [foo](/bar\* "ti\*tle")
  4365. .
  4366. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4367. ````````````````````````````````
  4368. ```````````````````````````````` example
  4369. [foo]
  4370. [foo]: /bar\* "ti\*tle"
  4371. .
  4372. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4373. ````````````````````````````````
  4374. ```````````````````````````````` example
  4375. ``` foo\+bar
  4376. foo
  4377. ```
  4378. .
  4379. <pre><code class="language-foo+bar">foo
  4380. </code></pre>
  4381. ````````````````````````````````
  4382. ## Entity and numeric character references
  4383. Valid HTML entity references and numeric character references
  4384. can be used in place of the corresponding Unicode character,
  4385. with the following exceptions:
  4386. - Entity and character references are not recognized in code
  4387. blocks and code spans.
  4388. - Entity and character references cannot stand in place of
  4389. special characters that define structural elements in
  4390. CommonMark. For example, although `&#42;` can be used
  4391. in place of a literal `*` character, `&#42;` cannot replace
  4392. `*` in emphasis delimiters, bullet list markers, or thematic
  4393. breaks.
  4394. Conforming CommonMark parsers need not store information about
  4395. whether a particular character was represented in the source
  4396. using a Unicode character or an entity reference.
  4397. [Entity references](@) consist of `&` + any of the valid
  4398. HTML5 entity names + `;`. The
  4399. document <https://html.spec.whatwg.org/multipage/entities.json>
  4400. is used as an authoritative source for the valid entity
  4401. references and their corresponding code points.
  4402. ```````````````````````````````` example
  4403. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4404. &frac34; &HilbertSpace; &DifferentialD;
  4405. &ClockwiseContourIntegral; &ngE;
  4406. .
  4407. <p>  &amp; © Æ Ď
  4408. ¾ ℋ ⅆ
  4409. ∲ ≧̸</p>
  4410. ````````````````````````````````
  4411. [Decimal numeric character
  4412. references](@)
  4413. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4414. numeric character reference is parsed as the corresponding
  4415. Unicode character. Invalid Unicode code points will be replaced by
  4416. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4417. the code point `U+0000` will also be replaced by `U+FFFD`.
  4418. ```````````````````````````````` example
  4419. &#35; &#1234; &#992; &#0;
  4420. .
  4421. <p># Ӓ Ϡ �</p>
  4422. ````````````````````````````````
  4423. [Hexadecimal numeric character
  4424. references](@) consist of `&#` +
  4425. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4426. They too are parsed as the corresponding Unicode character (this
  4427. time specified with a hexadecimal numeral instead of decimal).
  4428. ```````````````````````````````` example
  4429. &#X22; &#XD06; &#xcab;
  4430. .
  4431. <p>&quot; ആ ಫ</p>
  4432. ````````````````````````````````
  4433. Here are some nonentities:
  4434. ```````````````````````````````` example
  4435. &nbsp &x; &#; &#x;
  4436. &#87654321;
  4437. &#abcdef0;
  4438. &ThisIsNotDefined; &hi?;
  4439. .
  4440. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4441. &amp;#87654321;
  4442. &amp;#abcdef0;
  4443. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4444. ````````````````````````````````
  4445. Although HTML5 does accept some entity references
  4446. without a trailing semicolon (such as `&copy`), these are not
  4447. recognized here, because it makes the grammar too ambiguous:
  4448. ```````````````````````````````` example
  4449. &copy
  4450. .
  4451. <p>&amp;copy</p>
  4452. ````````````````````````````````
  4453. Strings that are not on the list of HTML5 named entities are not
  4454. recognized as entity references either:
  4455. ```````````````````````````````` example
  4456. &MadeUpEntity;
  4457. .
  4458. <p>&amp;MadeUpEntity;</p>
  4459. ````````````````````````````````
  4460. Entity and numeric character references are recognized in any
  4461. context besides code spans or code blocks, including
  4462. URLs, [link titles], and [fenced code block][] [info strings]:
  4463. ```````````````````````````````` example
  4464. <a href="&ouml;&ouml;.html">
  4465. .
  4466. <a href="&ouml;&ouml;.html">
  4467. ````````````````````````````````
  4468. ```````````````````````````````` example
  4469. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4470. .
  4471. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4472. ````````````````````````````````
  4473. ```````````````````````````````` example
  4474. [foo]
  4475. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4476. .
  4477. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4478. ````````````````````````````````
  4479. ```````````````````````````````` example
  4480. ``` f&ouml;&ouml;
  4481. foo
  4482. ```
  4483. .
  4484. <pre><code class="language-föö">foo
  4485. </code></pre>
  4486. ````````````````````````````````
  4487. Entity and numeric character references are treated as literal
  4488. text in code spans and code blocks:
  4489. ```````````````````````````````` example
  4490. `f&ouml;&ouml;`
  4491. .
  4492. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4493. ````````````````````````````````
  4494. ```````````````````````````````` example
  4495. f&ouml;f&ouml;
  4496. .
  4497. <pre><code>f&amp;ouml;f&amp;ouml;
  4498. </code></pre>
  4499. ````````````````````````````````
  4500. Entity and numeric character references cannot be used
  4501. in place of symbols indicating structure in CommonMark
  4502. documents.
  4503. ```````````````````````````````` example
  4504. &#42;foo&#42;
  4505. *foo*
  4506. .
  4507. <p>*foo*
  4508. <em>foo</em></p>
  4509. ````````````````````````````````
  4510. ```````````````````````````````` example
  4511. &#42; foo
  4512. * foo
  4513. .
  4514. <p>* foo</p>
  4515. <ul>
  4516. <li>foo</li>
  4517. </ul>
  4518. ````````````````````````````````
  4519. ```````````````````````````````` example
  4520. foo&#10;&#10;bar
  4521. .
  4522. <p>foo
  4523. bar</p>
  4524. ````````````````````````````````
  4525. ```````````````````````````````` example
  4526. &#9;foo
  4527. .
  4528. <p>→foo</p>
  4529. ````````````````````````````````
  4530. ```````````````````````````````` example
  4531. [a](url &quot;tit&quot;)
  4532. .
  4533. <p>[a](url &quot;tit&quot;)</p>
  4534. ````````````````````````````````
  4535. ## Code spans
  4536. A [backtick string](@)
  4537. is a string of one or more backtick characters (`` ` ``) that is neither
  4538. preceded nor followed by a backtick.
  4539. A [code span](@) begins with a backtick string and ends with
  4540. a backtick string of equal length. The contents of the code span are
  4541. the characters between these two backtick strings, normalized in the
  4542. following ways:
  4543. - First, [line endings] are converted to [spaces].
  4544. - If the resulting string both begins *and* ends with a [space]
  4545. character, but does not consist entirely of [space]
  4546. characters, a single [space] character is removed from the
  4547. front and back. This allows you to include code that begins
  4548. or ends with backtick characters, which must be separated by
  4549. whitespace from the opening or closing backtick strings.
  4550. This is a simple code span:
  4551. ```````````````````````````````` example
  4552. `foo`
  4553. .
  4554. <p><code>foo</code></p>
  4555. ````````````````````````````````
  4556. Here two backticks are used, because the code contains a backtick.
  4557. This example also illustrates stripping of a single leading and
  4558. trailing space:
  4559. ```````````````````````````````` example
  4560. `` foo ` bar ``
  4561. .
  4562. <p><code>foo ` bar</code></p>
  4563. ````````````````````````````````
  4564. This example shows the motivation for stripping leading and trailing
  4565. spaces:
  4566. ```````````````````````````````` example
  4567. ` `` `
  4568. .
  4569. <p><code>``</code></p>
  4570. ````````````````````````````````
  4571. Note that only *one* space is stripped:
  4572. ```````````````````````````````` example
  4573. ` `` `
  4574. .
  4575. <p><code> `` </code></p>
  4576. ````````````````````````````````
  4577. The stripping only happens if the space is on both
  4578. sides of the string:
  4579. ```````````````````````````````` example
  4580. ` a`
  4581. .
  4582. <p><code> a</code></p>
  4583. ````````````````````````````````
  4584. Only [spaces], and not [unicode whitespace] in general, are
  4585. stripped in this way:
  4586. ```````````````````````````````` example
  4587. ` b `
  4588. .
  4589. <p><code> b </code></p>
  4590. ````````````````````````````````
  4591. No stripping occurs if the code span contains only spaces:
  4592. ```````````````````````````````` example
  4593. ` `
  4594. ` `
  4595. .
  4596. <p><code> </code>
  4597. <code> </code></p>
  4598. ````````````````````````````````
  4599. [Line endings] are treated like spaces:
  4600. ```````````````````````````````` example
  4601. ``
  4602. foo
  4603. bar
  4604. baz
  4605. ``
  4606. .
  4607. <p><code>foo bar baz</code></p>
  4608. ````````````````````````````````
  4609. ```````````````````````````````` example
  4610. ``
  4611. foo
  4612. ``
  4613. .
  4614. <p><code>foo </code></p>
  4615. ````````````````````````````````
  4616. Interior spaces are not collapsed:
  4617. ```````````````````````````````` example
  4618. `foo bar
  4619. baz`
  4620. .
  4621. <p><code>foo bar baz</code></p>
  4622. ````````````````````````````````
  4623. Note that browsers will typically collapse consecutive spaces
  4624. when rendering `<code>` elements, so it is recommended that
  4625. the following CSS be used:
  4626. code{white-space: pre-wrap;}
  4627. Note that backslash escapes do not work in code spans. All backslashes
  4628. are treated literally:
  4629. ```````````````````````````````` example
  4630. `foo\`bar`
  4631. .
  4632. <p><code>foo\</code>bar`</p>
  4633. ````````````````````````````````
  4634. Backslash escapes are never needed, because one can always choose a
  4635. string of *n* backtick characters as delimiters, where the code does
  4636. not contain any strings of exactly *n* backtick characters.
  4637. ```````````````````````````````` example
  4638. ``foo`bar``
  4639. .
  4640. <p><code>foo`bar</code></p>
  4641. ````````````````````````````````
  4642. ```````````````````````````````` example
  4643. ` foo `` bar `
  4644. .
  4645. <p><code>foo `` bar</code></p>
  4646. ````````````````````````````````
  4647. Code span backticks have higher precedence than any other inline
  4648. constructs except HTML tags and autolinks. Thus, for example, this is
  4649. not parsed as emphasized text, since the second `*` is part of a code
  4650. span:
  4651. ```````````````````````````````` example
  4652. *foo`*`
  4653. .
  4654. <p>*foo<code>*</code></p>
  4655. ````````````````````````````````
  4656. And this is not parsed as a link:
  4657. ```````````````````````````````` example
  4658. [not a `link](/foo`)
  4659. .
  4660. <p>[not a <code>link](/foo</code>)</p>
  4661. ````````````````````````````````
  4662. Code spans, HTML tags, and autolinks have the same precedence.
  4663. Thus, this is code:
  4664. ```````````````````````````````` example
  4665. `<a href="`">`
  4666. .
  4667. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4668. ````````````````````````````````
  4669. But this is an HTML tag:
  4670. ```````````````````````````````` example
  4671. <a href="`">`
  4672. .
  4673. <p><a href="`">`</p>
  4674. ````````````````````````````````
  4675. And this is code:
  4676. ```````````````````````````````` example
  4677. `<http://foo.bar.`baz>`
  4678. .
  4679. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4680. ````````````````````````````````
  4681. But this is an autolink:
  4682. ```````````````````````````````` example
  4683. <http://foo.bar.`baz>`
  4684. .
  4685. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4686. ````````````````````````````````
  4687. When a backtick string is not closed by a matching backtick string,
  4688. we just have literal backticks:
  4689. ```````````````````````````````` example
  4690. ```foo``
  4691. .
  4692. <p>```foo``</p>
  4693. ````````````````````````````````
  4694. ```````````````````````````````` example
  4695. `foo
  4696. .
  4697. <p>`foo</p>
  4698. ````````````````````````````````
  4699. The following case also illustrates the need for opening and
  4700. closing backtick strings to be equal in length:
  4701. ```````````````````````````````` example
  4702. `foo``bar``
  4703. .
  4704. <p>`foo<code>bar</code></p>
  4705. ````````````````````````````````
  4706. ## Emphasis and strong emphasis
  4707. John Gruber's original [Markdown syntax
  4708. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4709. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4710. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4711. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4712. > tag.
  4713. This is enough for most users, but these rules leave much undecided,
  4714. especially when it comes to nested emphasis. The original
  4715. `Markdown.pl` test suite makes it clear that triple `***` and
  4716. `___` delimiters can be used for strong emphasis, and most
  4717. implementations have also allowed the following patterns:
  4718. ``` markdown
  4719. ***strong emph***
  4720. ***strong** in emph*
  4721. ***emph* in strong**
  4722. **in strong *emph***
  4723. *in emph **strong***
  4724. ```
  4725. The following patterns are less widely supported, but the intent
  4726. is clear and they are useful (especially in contexts like bibliography
  4727. entries):
  4728. ``` markdown
  4729. *emph *with emph* in it*
  4730. **strong **with strong** in it**
  4731. ```
  4732. Many implementations have also restricted intraword emphasis to
  4733. the `*` forms, to avoid unwanted emphasis in words containing
  4734. internal underscores. (It is best practice to put these in code
  4735. spans, but users often do not.)
  4736. ``` markdown
  4737. internal emphasis: foo*bar*baz
  4738. no emphasis: foo_bar_baz
  4739. ```
  4740. The rules given below capture all of these patterns, while allowing
  4741. for efficient parsing strategies that do not backtrack.
  4742. First, some definitions. A [delimiter run](@) is either
  4743. a sequence of one or more `*` characters that is not preceded or
  4744. followed by a non-backslash-escaped `*` character, or a sequence
  4745. of one or more `_` characters that is not preceded or followed by
  4746. a non-backslash-escaped `_` character.
  4747. A [left-flanking delimiter run](@) is
  4748. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4749. and either (2a) not followed by a [punctuation character], or
  4750. (2b) followed by a [punctuation character] and
  4751. preceded by [Unicode whitespace] or a [punctuation character].
  4752. For purposes of this definition, the beginning and the end of
  4753. the line count as Unicode whitespace.
  4754. A [right-flanking delimiter run](@) is
  4755. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4756. and either (2a) not preceded by a [punctuation character], or
  4757. (2b) preceded by a [punctuation character] and
  4758. followed by [Unicode whitespace] or a [punctuation character].
  4759. For purposes of this definition, the beginning and the end of
  4760. the line count as Unicode whitespace.
  4761. Here are some examples of delimiter runs.
  4762. - left-flanking but not right-flanking:
  4763. ```
  4764. ***abc
  4765. _abc
  4766. **"abc"
  4767. _"abc"
  4768. ```
  4769. - right-flanking but not left-flanking:
  4770. ```
  4771. abc***
  4772. abc_
  4773. "abc"**
  4774. "abc"_
  4775. ```
  4776. - Both left and right-flanking:
  4777. ```
  4778. abc***def
  4779. "abc"_"def"
  4780. ```
  4781. - Neither left nor right-flanking:
  4782. ```
  4783. abc *** def
  4784. a _ b
  4785. ```
  4786. (The idea of distinguishing left-flanking and right-flanking
  4787. delimiter runs based on the character before and the character
  4788. after comes from Roopesh Chander's
  4789. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4790. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4791. run," and its rules for distinguishing left- and right-flanking runs
  4792. are a bit more complex than the ones given here.)
  4793. The following rules define emphasis and strong emphasis:
  4794. 1. A single `*` character [can open emphasis](@)
  4795. iff (if and only if) it is part of a [left-flanking delimiter run].
  4796. 2. A single `_` character [can open emphasis] iff
  4797. it is part of a [left-flanking delimiter run]
  4798. and either (a) not part of a [right-flanking delimiter run]
  4799. or (b) part of a [right-flanking delimiter run]
  4800. preceded by punctuation.
  4801. 3. A single `*` character [can close emphasis](@)
  4802. iff it is part of a [right-flanking delimiter run].
  4803. 4. A single `_` character [can close emphasis] iff
  4804. it is part of a [right-flanking delimiter run]
  4805. and either (a) not part of a [left-flanking delimiter run]
  4806. or (b) part of a [left-flanking delimiter run]
  4807. followed by punctuation.
  4808. 5. A double `**` [can open strong emphasis](@)
  4809. iff it is part of a [left-flanking delimiter run].
  4810. 6. A double `__` [can open strong emphasis] iff
  4811. it is part of a [left-flanking delimiter run]
  4812. and either (a) not part of a [right-flanking delimiter run]
  4813. or (b) part of a [right-flanking delimiter run]
  4814. preceded by punctuation.
  4815. 7. A double `**` [can close strong emphasis](@)
  4816. iff it is part of a [right-flanking delimiter run].
  4817. 8. A double `__` [can close strong emphasis] iff
  4818. it is part of a [right-flanking delimiter run]
  4819. and either (a) not part of a [left-flanking delimiter run]
  4820. or (b) part of a [left-flanking delimiter run]
  4821. followed by punctuation.
  4822. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4823. with a delimiter that [can close emphasis], and that uses the same
  4824. character (`_` or `*`) as the opening delimiter. The
  4825. opening and closing delimiters must belong to separate
  4826. [delimiter runs]. If one of the delimiters can both
  4827. open and close emphasis, then the sum of the lengths of the
  4828. delimiter runs containing the opening and closing delimiters
  4829. must not be a multiple of 3 unless both lengths are
  4830. multiples of 3.
  4831. 10. Strong emphasis begins with a delimiter that
  4832. [can open strong emphasis] and ends with a delimiter that
  4833. [can close strong emphasis], and that uses the same character
  4834. (`_` or `*`) as the opening delimiter. The
  4835. opening and closing delimiters must belong to separate
  4836. [delimiter runs]. If one of the delimiters can both open
  4837. and close strong emphasis, then the sum of the lengths of
  4838. the delimiter runs containing the opening and closing
  4839. delimiters must not be a multiple of 3 unless both lengths
  4840. are multiples of 3.
  4841. 11. A literal `*` character cannot occur at the beginning or end of
  4842. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4843. is backslash-escaped.
  4844. 12. A literal `_` character cannot occur at the beginning or end of
  4845. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4846. is backslash-escaped.
  4847. Where rules 1--12 above are compatible with multiple parsings,
  4848. the following principles resolve ambiguity:
  4849. 13. The number of nestings should be minimized. Thus, for example,
  4850. an interpretation `<strong>...</strong>` is always preferred to
  4851. `<em><em>...</em></em>`.
  4852. 14. An interpretation `<em><strong>...</strong></em>` is always
  4853. preferred to `<strong><em>...</em></strong>`.
  4854. 15. When two potential emphasis or strong emphasis spans overlap,
  4855. so that the second begins before the first ends and ends after
  4856. the first ends, the first takes precedence. Thus, for example,
  4857. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4858. than `*foo <em>bar* baz</em>`.
  4859. 16. When there are two potential emphasis or strong emphasis spans
  4860. with the same closing delimiter, the shorter one (the one that
  4861. opens later) takes precedence. Thus, for example,
  4862. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4863. rather than `<strong>foo **bar baz</strong>`.
  4864. 17. Inline code spans, links, images, and HTML tags group more tightly
  4865. than emphasis. So, when there is a choice between an interpretation
  4866. that contains one of these elements and one that does not, the
  4867. former always wins. Thus, for example, `*[foo*](bar)` is
  4868. parsed as `*<a href="bar">foo*</a>` rather than as
  4869. `<em>[foo</em>](bar)`.
  4870. These rules can be illustrated through a series of examples.
  4871. Rule 1:
  4872. ```````````````````````````````` example
  4873. *foo bar*
  4874. .
  4875. <p><em>foo bar</em></p>
  4876. ````````````````````````````````
  4877. This is not emphasis, because the opening `*` is followed by
  4878. whitespace, and hence not part of a [left-flanking delimiter run]:
  4879. ```````````````````````````````` example
  4880. a * foo bar*
  4881. .
  4882. <p>a * foo bar*</p>
  4883. ````````````````````````````````
  4884. This is not emphasis, because the opening `*` is preceded
  4885. by an alphanumeric and followed by punctuation, and hence
  4886. not part of a [left-flanking delimiter run]:
  4887. ```````````````````````````````` example
  4888. a*"foo"*
  4889. .
  4890. <p>a*&quot;foo&quot;*</p>
  4891. ````````````````````````````````
  4892. Unicode nonbreaking spaces count as whitespace, too:
  4893. ```````````````````````````````` example
  4894. * a *
  4895. .
  4896. <p>* a *</p>
  4897. ````````````````````````````````
  4898. Intraword emphasis with `*` is permitted:
  4899. ```````````````````````````````` example
  4900. foo*bar*
  4901. .
  4902. <p>foo<em>bar</em></p>
  4903. ````````````````````````````````
  4904. ```````````````````````````````` example
  4905. 5*6*78
  4906. .
  4907. <p>5<em>6</em>78</p>
  4908. ````````````````````````````````
  4909. Rule 2:
  4910. ```````````````````````````````` example
  4911. _foo bar_
  4912. .
  4913. <p><em>foo bar</em></p>
  4914. ````````````````````````````````
  4915. This is not emphasis, because the opening `_` is followed by
  4916. whitespace:
  4917. ```````````````````````````````` example
  4918. _ foo bar_
  4919. .
  4920. <p>_ foo bar_</p>
  4921. ````````````````````````````````
  4922. This is not emphasis, because the opening `_` is preceded
  4923. by an alphanumeric and followed by punctuation:
  4924. ```````````````````````````````` example
  4925. a_"foo"_
  4926. .
  4927. <p>a_&quot;foo&quot;_</p>
  4928. ````````````````````````````````
  4929. Emphasis with `_` is not allowed inside words:
  4930. ```````````````````````````````` example
  4931. foo_bar_
  4932. .
  4933. <p>foo_bar_</p>
  4934. ````````````````````````````````
  4935. ```````````````````````````````` example
  4936. 5_6_78
  4937. .
  4938. <p>5_6_78</p>
  4939. ````````````````````````````````
  4940. ```````````````````````````````` example
  4941. пристаням_стремятся_
  4942. .
  4943. <p>пристаням_стремятся_</p>
  4944. ````````````````````````````````
  4945. Here `_` does not generate emphasis, because the first delimiter run
  4946. is right-flanking and the second left-flanking:
  4947. ```````````````````````````````` example
  4948. aa_"bb"_cc
  4949. .
  4950. <p>aa_&quot;bb&quot;_cc</p>
  4951. ````````````````````````````````
  4952. This is emphasis, even though the opening delimiter is
  4953. both left- and right-flanking, because it is preceded by
  4954. punctuation:
  4955. ```````````````````````````````` example
  4956. foo-_(bar)_
  4957. .
  4958. <p>foo-<em>(bar)</em></p>
  4959. ````````````````````````````````
  4960. Rule 3:
  4961. This is not emphasis, because the closing delimiter does
  4962. not match the opening delimiter:
  4963. ```````````````````````````````` example
  4964. _foo*
  4965. .
  4966. <p>_foo*</p>
  4967. ````````````````````````````````
  4968. This is not emphasis, because the closing `*` is preceded by
  4969. whitespace:
  4970. ```````````````````````````````` example
  4971. *foo bar *
  4972. .
  4973. <p>*foo bar *</p>
  4974. ````````````````````````````````
  4975. A newline also counts as whitespace:
  4976. ```````````````````````````````` example
  4977. *foo bar
  4978. *
  4979. .
  4980. <p>*foo bar
  4981. *</p>
  4982. ````````````````````````````````
  4983. This is not emphasis, because the second `*` is
  4984. preceded by punctuation and followed by an alphanumeric
  4985. (hence it is not part of a [right-flanking delimiter run]:
  4986. ```````````````````````````````` example
  4987. *(*foo)
  4988. .
  4989. <p>*(*foo)</p>
  4990. ````````````````````````````````
  4991. The point of this restriction is more easily appreciated
  4992. with this example:
  4993. ```````````````````````````````` example
  4994. *(*foo*)*
  4995. .
  4996. <p><em>(<em>foo</em>)</em></p>
  4997. ````````````````````````````````
  4998. Intraword emphasis with `*` is allowed:
  4999. ```````````````````````````````` example
  5000. *foo*bar
  5001. .
  5002. <p><em>foo</em>bar</p>
  5003. ````````````````````````````````
  5004. Rule 4:
  5005. This is not emphasis, because the closing `_` is preceded by
  5006. whitespace:
  5007. ```````````````````````````````` example
  5008. _foo bar _
  5009. .
  5010. <p>_foo bar _</p>
  5011. ````````````````````````````````
  5012. This is not emphasis, because the second `_` is
  5013. preceded by punctuation and followed by an alphanumeric:
  5014. ```````````````````````````````` example
  5015. _(_foo)
  5016. .
  5017. <p>_(_foo)</p>
  5018. ````````````````````````````````
  5019. This is emphasis within emphasis:
  5020. ```````````````````````````````` example
  5021. _(_foo_)_
  5022. .
  5023. <p><em>(<em>foo</em>)</em></p>
  5024. ````````````````````````````````
  5025. Intraword emphasis is disallowed for `_`:
  5026. ```````````````````````````````` example
  5027. _foo_bar
  5028. .
  5029. <p>_foo_bar</p>
  5030. ````````````````````````````````
  5031. ```````````````````````````````` example
  5032. _пристаням_стремятся
  5033. .
  5034. <p>_пристаням_стремятся</p>
  5035. ````````````````````````````````
  5036. ```````````````````````````````` example
  5037. _foo_bar_baz_
  5038. .
  5039. <p><em>foo_bar_baz</em></p>
  5040. ````````````````````````````````
  5041. This is emphasis, even though the closing delimiter is
  5042. both left- and right-flanking, because it is followed by
  5043. punctuation:
  5044. ```````````````````````````````` example
  5045. _(bar)_.
  5046. .
  5047. <p><em>(bar)</em>.</p>
  5048. ````````````````````````````````
  5049. Rule 5:
  5050. ```````````````````````````````` example
  5051. **foo bar**
  5052. .
  5053. <p><strong>foo bar</strong></p>
  5054. ````````````````````````````````
  5055. This is not strong emphasis, because the opening delimiter is
  5056. followed by whitespace:
  5057. ```````````````````````````````` example
  5058. ** foo bar**
  5059. .
  5060. <p>** foo bar**</p>
  5061. ````````````````````````````````
  5062. This is not strong emphasis, because the opening `**` is preceded
  5063. by an alphanumeric and followed by punctuation, and hence
  5064. not part of a [left-flanking delimiter run]:
  5065. ```````````````````````````````` example
  5066. a**"foo"**
  5067. .
  5068. <p>a**&quot;foo&quot;**</p>
  5069. ````````````````````````````````
  5070. Intraword strong emphasis with `**` is permitted:
  5071. ```````````````````````````````` example
  5072. foo**bar**
  5073. .
  5074. <p>foo<strong>bar</strong></p>
  5075. ````````````````````````````````
  5076. Rule 6:
  5077. ```````````````````````````````` example
  5078. __foo bar__
  5079. .
  5080. <p><strong>foo bar</strong></p>
  5081. ````````````````````````````````
  5082. This is not strong emphasis, because the opening delimiter is
  5083. followed by whitespace:
  5084. ```````````````````````````````` example
  5085. __ foo bar__
  5086. .
  5087. <p>__ foo bar__</p>
  5088. ````````````````````````````````
  5089. A newline counts as whitespace:
  5090. ```````````````````````````````` example
  5091. __
  5092. foo bar__
  5093. .
  5094. <p>__
  5095. foo bar__</p>
  5096. ````````````````````````````````
  5097. This is not strong emphasis, because the opening `__` is preceded
  5098. by an alphanumeric and followed by punctuation:
  5099. ```````````````````````````````` example
  5100. a__"foo"__
  5101. .
  5102. <p>a__&quot;foo&quot;__</p>
  5103. ````````````````````````````````
  5104. Intraword strong emphasis is forbidden with `__`:
  5105. ```````````````````````````````` example
  5106. foo__bar__
  5107. .
  5108. <p>foo__bar__</p>
  5109. ````````````````````````````````
  5110. ```````````````````````````````` example
  5111. 5__6__78
  5112. .
  5113. <p>5__6__78</p>
  5114. ````````````````````````````````
  5115. ```````````````````````````````` example
  5116. пристаням__стремятся__
  5117. .
  5118. <p>пристаням__стремятся__</p>
  5119. ````````````````````````````````
  5120. ```````````````````````````````` example
  5121. __foo, __bar__, baz__
  5122. .
  5123. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5124. ````````````````````````````````
  5125. This is strong emphasis, even though the opening delimiter is
  5126. both left- and right-flanking, because it is preceded by
  5127. punctuation:
  5128. ```````````````````````````````` example
  5129. foo-__(bar)__
  5130. .
  5131. <p>foo-<strong>(bar)</strong></p>
  5132. ````````````````````````````````
  5133. Rule 7:
  5134. This is not strong emphasis, because the closing delimiter is preceded
  5135. by whitespace:
  5136. ```````````````````````````````` example
  5137. **foo bar **
  5138. .
  5139. <p>**foo bar **</p>
  5140. ````````````````````````````````
  5141. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5142. Rule 11.)
  5143. This is not strong emphasis, because the second `**` is
  5144. preceded by punctuation and followed by an alphanumeric:
  5145. ```````````````````````````````` example
  5146. **(**foo)
  5147. .
  5148. <p>**(**foo)</p>
  5149. ````````````````````````````````
  5150. The point of this restriction is more easily appreciated
  5151. with these examples:
  5152. ```````````````````````````````` example
  5153. *(**foo**)*
  5154. .
  5155. <p><em>(<strong>foo</strong>)</em></p>
  5156. ````````````````````````````````
  5157. ```````````````````````````````` example
  5158. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5159. *Asclepias physocarpa*)**
  5160. .
  5161. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5162. <em>Asclepias physocarpa</em>)</strong></p>
  5163. ````````````````````````````````
  5164. ```````````````````````````````` example
  5165. **foo "*bar*" foo**
  5166. .
  5167. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5168. ````````````````````````````````
  5169. Intraword emphasis:
  5170. ```````````````````````````````` example
  5171. **foo**bar
  5172. .
  5173. <p><strong>foo</strong>bar</p>
  5174. ````````````````````````````````
  5175. Rule 8:
  5176. This is not strong emphasis, because the closing delimiter is
  5177. preceded by whitespace:
  5178. ```````````````````````````````` example
  5179. __foo bar __
  5180. .
  5181. <p>__foo bar __</p>
  5182. ````````````````````````````````
  5183. This is not strong emphasis, because the second `__` is
  5184. preceded by punctuation and followed by an alphanumeric:
  5185. ```````````````````````````````` example
  5186. __(__foo)
  5187. .
  5188. <p>__(__foo)</p>
  5189. ````````````````````````````````
  5190. The point of this restriction is more easily appreciated
  5191. with this example:
  5192. ```````````````````````````````` example
  5193. _(__foo__)_
  5194. .
  5195. <p><em>(<strong>foo</strong>)</em></p>
  5196. ````````````````````````````````
  5197. Intraword strong emphasis is forbidden with `__`:
  5198. ```````````````````````````````` example
  5199. __foo__bar
  5200. .
  5201. <p>__foo__bar</p>
  5202. ````````````````````````````````
  5203. ```````````````````````````````` example
  5204. __пристаням__стремятся
  5205. .
  5206. <p>__пристаням__стремятся</p>
  5207. ````````````````````````````````
  5208. ```````````````````````````````` example
  5209. __foo__bar__baz__
  5210. .
  5211. <p><strong>foo__bar__baz</strong></p>
  5212. ````````````````````````````````
  5213. This is strong emphasis, even though the closing delimiter is
  5214. both left- and right-flanking, because it is followed by
  5215. punctuation:
  5216. ```````````````````````````````` example
  5217. __(bar)__.
  5218. .
  5219. <p><strong>(bar)</strong>.</p>
  5220. ````````````````````````````````
  5221. Rule 9:
  5222. Any nonempty sequence of inline elements can be the contents of an
  5223. emphasized span.
  5224. ```````````````````````````````` example
  5225. *foo [bar](/url)*
  5226. .
  5227. <p><em>foo <a href="/url">bar</a></em></p>
  5228. ````````````````````````````````
  5229. ```````````````````````````````` example
  5230. *foo
  5231. bar*
  5232. .
  5233. <p><em>foo
  5234. bar</em></p>
  5235. ````````````````````````````````
  5236. In particular, emphasis and strong emphasis can be nested
  5237. inside emphasis:
  5238. ```````````````````````````````` example
  5239. _foo __bar__ baz_
  5240. .
  5241. <p><em>foo <strong>bar</strong> baz</em></p>
  5242. ````````````````````````````````
  5243. ```````````````````````````````` example
  5244. _foo _bar_ baz_
  5245. .
  5246. <p><em>foo <em>bar</em> baz</em></p>
  5247. ````````````````````````````````
  5248. ```````````````````````````````` example
  5249. __foo_ bar_
  5250. .
  5251. <p><em><em>foo</em> bar</em></p>
  5252. ````````````````````````````````
  5253. ```````````````````````````````` example
  5254. *foo *bar**
  5255. .
  5256. <p><em>foo <em>bar</em></em></p>
  5257. ````````````````````````````````
  5258. ```````````````````````````````` example
  5259. *foo **bar** baz*
  5260. .
  5261. <p><em>foo <strong>bar</strong> baz</em></p>
  5262. ````````````````````````````````
  5263. ```````````````````````````````` example
  5264. *foo**bar**baz*
  5265. .
  5266. <p><em>foo<strong>bar</strong>baz</em></p>
  5267. ````````````````````````````````
  5268. Note that in the preceding case, the interpretation
  5269. ``` markdown
  5270. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5271. ```
  5272. is precluded by the condition that a delimiter that
  5273. can both open and close (like the `*` after `foo`)
  5274. cannot form emphasis if the sum of the lengths of
  5275. the delimiter runs containing the opening and
  5276. closing delimiters is a multiple of 3 unless
  5277. both lengths are multiples of 3.
  5278. For the same reason, we don't get two consecutive
  5279. emphasis sections in this example:
  5280. ```````````````````````````````` example
  5281. *foo**bar*
  5282. .
  5283. <p><em>foo**bar</em></p>
  5284. ````````````````````````````````
  5285. The same condition ensures that the following
  5286. cases are all strong emphasis nested inside
  5287. emphasis, even when the interior spaces are
  5288. omitted:
  5289. ```````````````````````````````` example
  5290. ***foo** bar*
  5291. .
  5292. <p><em><strong>foo</strong> bar</em></p>
  5293. ````````````````````````````````
  5294. ```````````````````````````````` example
  5295. *foo **bar***
  5296. .
  5297. <p><em>foo <strong>bar</strong></em></p>
  5298. ````````````````````````````````
  5299. ```````````````````````````````` example
  5300. *foo**bar***
  5301. .
  5302. <p><em>foo<strong>bar</strong></em></p>
  5303. ````````````````````````````````
  5304. When the lengths of the interior closing and opening
  5305. delimiter runs are *both* multiples of 3, though,
  5306. they can match to create emphasis:
  5307. ```````````````````````````````` example
  5308. foo***bar***baz
  5309. .
  5310. <p>foo<em><strong>bar</strong></em>baz</p>
  5311. ````````````````````````````````
  5312. ```````````````````````````````` example
  5313. foo******bar*********baz
  5314. .
  5315. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5316. ````````````````````````````````
  5317. Indefinite levels of nesting are possible:
  5318. ```````````````````````````````` example
  5319. *foo **bar *baz* bim** bop*
  5320. .
  5321. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5322. ````````````````````````````````
  5323. ```````````````````````````````` example
  5324. *foo [*bar*](/url)*
  5325. .
  5326. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5327. ````````````````````````````````
  5328. There can be no empty emphasis or strong emphasis:
  5329. ```````````````````````````````` example
  5330. ** is not an empty emphasis
  5331. .
  5332. <p>** is not an empty emphasis</p>
  5333. ````````````````````````````````
  5334. ```````````````````````````````` example
  5335. **** is not an empty strong emphasis
  5336. .
  5337. <p>**** is not an empty strong emphasis</p>
  5338. ````````````````````````````````
  5339. Rule 10:
  5340. Any nonempty sequence of inline elements can be the contents of an
  5341. strongly emphasized span.
  5342. ```````````````````````````````` example
  5343. **foo [bar](/url)**
  5344. .
  5345. <p><strong>foo <a href="/url">bar</a></strong></p>
  5346. ````````````````````````````````
  5347. ```````````````````````````````` example
  5348. **foo
  5349. bar**
  5350. .
  5351. <p><strong>foo
  5352. bar</strong></p>
  5353. ````````````````````````````````
  5354. In particular, emphasis and strong emphasis can be nested
  5355. inside strong emphasis:
  5356. ```````````````````````````````` example
  5357. __foo _bar_ baz__
  5358. .
  5359. <p><strong>foo <em>bar</em> baz</strong></p>
  5360. ````````````````````````````````
  5361. ```````````````````````````````` example
  5362. __foo __bar__ baz__
  5363. .
  5364. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5365. ````````````````````````````````
  5366. ```````````````````````````````` example
  5367. ____foo__ bar__
  5368. .
  5369. <p><strong><strong>foo</strong> bar</strong></p>
  5370. ````````````````````````````````
  5371. ```````````````````````````````` example
  5372. **foo **bar****
  5373. .
  5374. <p><strong>foo <strong>bar</strong></strong></p>
  5375. ````````````````````````````````
  5376. ```````````````````````````````` example
  5377. **foo *bar* baz**
  5378. .
  5379. <p><strong>foo <em>bar</em> baz</strong></p>
  5380. ````````````````````````````````
  5381. ```````````````````````````````` example
  5382. **foo*bar*baz**
  5383. .
  5384. <p><strong>foo<em>bar</em>baz</strong></p>
  5385. ````````````````````````````````
  5386. ```````````````````````````````` example
  5387. ***foo* bar**
  5388. .
  5389. <p><strong><em>foo</em> bar</strong></p>
  5390. ````````````````````````````````
  5391. ```````````````````````````````` example
  5392. **foo *bar***
  5393. .
  5394. <p><strong>foo <em>bar</em></strong></p>
  5395. ````````````````````````````````
  5396. Indefinite levels of nesting are possible:
  5397. ```````````````````````````````` example
  5398. **foo *bar **baz**
  5399. bim* bop**
  5400. .
  5401. <p><strong>foo <em>bar <strong>baz</strong>
  5402. bim</em> bop</strong></p>
  5403. ````````````````````````````````
  5404. ```````````````````````````````` example
  5405. **foo [*bar*](/url)**
  5406. .
  5407. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5408. ````````````````````````````````
  5409. There can be no empty emphasis or strong emphasis:
  5410. ```````````````````````````````` example
  5411. __ is not an empty emphasis
  5412. .
  5413. <p>__ is not an empty emphasis</p>
  5414. ````````````````````````````````
  5415. ```````````````````````````````` example
  5416. ____ is not an empty strong emphasis
  5417. .
  5418. <p>____ is not an empty strong emphasis</p>
  5419. ````````````````````````````````
  5420. Rule 11:
  5421. ```````````````````````````````` example
  5422. foo ***
  5423. .
  5424. <p>foo ***</p>
  5425. ````````````````````````````````
  5426. ```````````````````````````````` example
  5427. foo *\**
  5428. .
  5429. <p>foo <em>*</em></p>
  5430. ````````````````````````````````
  5431. ```````````````````````````````` example
  5432. foo *_*
  5433. .
  5434. <p>foo <em>_</em></p>
  5435. ````````````````````````````````
  5436. ```````````````````````````````` example
  5437. foo *****
  5438. .
  5439. <p>foo *****</p>
  5440. ````````````````````````````````
  5441. ```````````````````````````````` example
  5442. foo **\***
  5443. .
  5444. <p>foo <strong>*</strong></p>
  5445. ````````````````````````````````
  5446. ```````````````````````````````` example
  5447. foo **_**
  5448. .
  5449. <p>foo <strong>_</strong></p>
  5450. ````````````````````````````````
  5451. Note that when delimiters do not match evenly, Rule 11 determines
  5452. that the excess literal `*` characters will appear outside of the
  5453. emphasis, rather than inside it:
  5454. ```````````````````````````````` example
  5455. **foo*
  5456. .
  5457. <p>*<em>foo</em></p>
  5458. ````````````````````````````````
  5459. ```````````````````````````````` example
  5460. *foo**
  5461. .
  5462. <p><em>foo</em>*</p>
  5463. ````````````````````````````````
  5464. ```````````````````````````````` example
  5465. ***foo**
  5466. .
  5467. <p>*<strong>foo</strong></p>
  5468. ````````````````````````````````
  5469. ```````````````````````````````` example
  5470. ****foo*
  5471. .
  5472. <p>***<em>foo</em></p>
  5473. ````````````````````````````````
  5474. ```````````````````````````````` example
  5475. **foo***
  5476. .
  5477. <p><strong>foo</strong>*</p>
  5478. ````````````````````````````````
  5479. ```````````````````````````````` example
  5480. *foo****
  5481. .
  5482. <p><em>foo</em>***</p>
  5483. ````````````````````````````````
  5484. Rule 12:
  5485. ```````````````````````````````` example
  5486. foo ___
  5487. .
  5488. <p>foo ___</p>
  5489. ````````````````````````````````
  5490. ```````````````````````````````` example
  5491. foo _\__
  5492. .
  5493. <p>foo <em>_</em></p>
  5494. ````````````````````````````````
  5495. ```````````````````````````````` example
  5496. foo _*_
  5497. .
  5498. <p>foo <em>*</em></p>
  5499. ````````````````````````````````
  5500. ```````````````````````````````` example
  5501. foo _____
  5502. .
  5503. <p>foo _____</p>
  5504. ````````````````````````````````
  5505. ```````````````````````````````` example
  5506. foo __\___
  5507. .
  5508. <p>foo <strong>_</strong></p>
  5509. ````````````````````````````````
  5510. ```````````````````````````````` example
  5511. foo __*__
  5512. .
  5513. <p>foo <strong>*</strong></p>
  5514. ````````````````````````````````
  5515. ```````````````````````````````` example
  5516. __foo_
  5517. .
  5518. <p>_<em>foo</em></p>
  5519. ````````````````````````````````
  5520. Note that when delimiters do not match evenly, Rule 12 determines
  5521. that the excess literal `_` characters will appear outside of the
  5522. emphasis, rather than inside it:
  5523. ```````````````````````````````` example
  5524. _foo__
  5525. .
  5526. <p><em>foo</em>_</p>
  5527. ````````````````````````````````
  5528. ```````````````````````````````` example
  5529. ___foo__
  5530. .
  5531. <p>_<strong>foo</strong></p>
  5532. ````````````````````````````````
  5533. ```````````````````````````````` example
  5534. ____foo_
  5535. .
  5536. <p>___<em>foo</em></p>
  5537. ````````````````````````````````
  5538. ```````````````````````````````` example
  5539. __foo___
  5540. .
  5541. <p><strong>foo</strong>_</p>
  5542. ````````````````````````````````
  5543. ```````````````````````````````` example
  5544. _foo____
  5545. .
  5546. <p><em>foo</em>___</p>
  5547. ````````````````````````````````
  5548. Rule 13 implies that if you want emphasis nested directly inside
  5549. emphasis, you must use different delimiters:
  5550. ```````````````````````````````` example
  5551. **foo**
  5552. .
  5553. <p><strong>foo</strong></p>
  5554. ````````````````````````````````
  5555. ```````````````````````````````` example
  5556. *_foo_*
  5557. .
  5558. <p><em><em>foo</em></em></p>
  5559. ````````````````````````````````
  5560. ```````````````````````````````` example
  5561. __foo__
  5562. .
  5563. <p><strong>foo</strong></p>
  5564. ````````````````````````````````
  5565. ```````````````````````````````` example
  5566. _*foo*_
  5567. .
  5568. <p><em><em>foo</em></em></p>
  5569. ````````````````````````````````
  5570. However, strong emphasis within strong emphasis is possible without
  5571. switching delimiters:
  5572. ```````````````````````````````` example
  5573. ****foo****
  5574. .
  5575. <p><strong><strong>foo</strong></strong></p>
  5576. ````````````````````````````````
  5577. ```````````````````````````````` example
  5578. ____foo____
  5579. .
  5580. <p><strong><strong>foo</strong></strong></p>
  5581. ````````````````````````````````
  5582. Rule 13 can be applied to arbitrarily long sequences of
  5583. delimiters:
  5584. ```````````````````````````````` example
  5585. ******foo******
  5586. .
  5587. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5588. ````````````````````````````````
  5589. Rule 14:
  5590. ```````````````````````````````` example
  5591. ***foo***
  5592. .
  5593. <p><em><strong>foo</strong></em></p>
  5594. ````````````````````````````````
  5595. ```````````````````````````````` example
  5596. _____foo_____
  5597. .
  5598. <p><em><strong><strong>foo</strong></strong></em></p>
  5599. ````````````````````````````````
  5600. Rule 15:
  5601. ```````````````````````````````` example
  5602. *foo _bar* baz_
  5603. .
  5604. <p><em>foo _bar</em> baz_</p>
  5605. ````````````````````````````````
  5606. ```````````````````````````````` example
  5607. *foo __bar *baz bim__ bam*
  5608. .
  5609. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5610. ````````````````````````````````
  5611. Rule 16:
  5612. ```````````````````````````````` example
  5613. **foo **bar baz**
  5614. .
  5615. <p>**foo <strong>bar baz</strong></p>
  5616. ````````````````````````````````
  5617. ```````````````````````````````` example
  5618. *foo *bar baz*
  5619. .
  5620. <p>*foo <em>bar baz</em></p>
  5621. ````````````````````````````````
  5622. Rule 17:
  5623. ```````````````````````````````` example
  5624. *[bar*](/url)
  5625. .
  5626. <p>*<a href="/url">bar*</a></p>
  5627. ````````````````````````````````
  5628. ```````````````````````````````` example
  5629. _foo [bar_](/url)
  5630. .
  5631. <p>_foo <a href="/url">bar_</a></p>
  5632. ````````````````````````````````
  5633. ```````````````````````````````` example
  5634. *<img src="foo" title="*"/>
  5635. .
  5636. <p>*<img src="foo" title="*"/></p>
  5637. ````````````````````````````````
  5638. ```````````````````````````````` example
  5639. **<a href="**">
  5640. .
  5641. <p>**<a href="**"></p>
  5642. ````````````````````````````````
  5643. ```````````````````````````````` example
  5644. __<a href="__">
  5645. .
  5646. <p>__<a href="__"></p>
  5647. ````````````````````````````````
  5648. ```````````````````````````````` example
  5649. *a `*`*
  5650. .
  5651. <p><em>a <code>*</code></em></p>
  5652. ````````````````````````````````
  5653. ```````````````````````````````` example
  5654. _a `_`_
  5655. .
  5656. <p><em>a <code>_</code></em></p>
  5657. ````````````````````````````````
  5658. ```````````````````````````````` example
  5659. **a<http://foo.bar/?q=**>
  5660. .
  5661. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5662. ````````````````````````````````
  5663. ```````````````````````````````` example
  5664. __a<http://foo.bar/?q=__>
  5665. .
  5666. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5667. ````````````````````````````````
  5668. ## Links
  5669. A link contains [link text] (the visible text), a [link destination]
  5670. (the URI that is the link destination), and optionally a [link title].
  5671. There are two basic kinds of links in Markdown. In [inline links] the
  5672. destination and title are given immediately after the link text. In
  5673. [reference links] the destination and title are defined elsewhere in
  5674. the document.
  5675. A [link text](@) consists of a sequence of zero or more
  5676. inline elements enclosed by square brackets (`[` and `]`). The
  5677. following rules apply:
  5678. - Links may not contain other links, at any level of nesting. If
  5679. multiple otherwise valid link definitions appear nested inside each
  5680. other, the inner-most definition is used.
  5681. - Brackets are allowed in the [link text] only if (a) they
  5682. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5683. with an open bracket `[`, a sequence of zero or more inlines, and
  5684. a close bracket `]`.
  5685. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5686. than the brackets in link text. Thus, for example,
  5687. `` [foo`]` `` could not be a link text, since the second `]`
  5688. is part of a code span.
  5689. - The brackets in link text bind more tightly than markers for
  5690. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5691. A [link destination](@) consists of either
  5692. - a sequence of zero or more characters between an opening `<` and a
  5693. closing `>` that contains no line breaks or unescaped
  5694. `<` or `>` characters, or
  5695. - a nonempty sequence of characters that does not start with
  5696. `<`, does not include ASCII space or control characters, and
  5697. includes parentheses only if (a) they are backslash-escaped or
  5698. (b) they are part of a balanced pair of unescaped parentheses.
  5699. (Implementations may impose limits on parentheses nesting to
  5700. avoid performance issues, but at least three levels of nesting
  5701. should be supported.)
  5702. A [link title](@) consists of either
  5703. - a sequence of zero or more characters between straight double-quote
  5704. characters (`"`), including a `"` character only if it is
  5705. backslash-escaped, or
  5706. - a sequence of zero or more characters between straight single-quote
  5707. characters (`'`), including a `'` character only if it is
  5708. backslash-escaped, or
  5709. - a sequence of zero or more characters between matching parentheses
  5710. (`(...)`), including a `(` or `)` character only if it is
  5711. backslash-escaped.
  5712. Although [link titles] may span multiple lines, they may not contain
  5713. a [blank line].
  5714. An [inline link](@) consists of a [link text] followed immediately
  5715. by a left parenthesis `(`, optional [whitespace], an optional
  5716. [link destination], an optional [link title] separated from the link
  5717. destination by [whitespace], optional [whitespace], and a right
  5718. parenthesis `)`. The link's text consists of the inlines contained
  5719. in the [link text] (excluding the enclosing square brackets).
  5720. The link's URI consists of the link destination, excluding enclosing
  5721. `<...>` if present, with backslash-escapes in effect as described
  5722. above. The link's title consists of the link title, excluding its
  5723. enclosing delimiters, with backslash-escapes in effect as described
  5724. above.
  5725. Here is a simple inline link:
  5726. ```````````````````````````````` example
  5727. [link](/uri "title")
  5728. .
  5729. <p><a href="/uri" title="title">link</a></p>
  5730. ````````````````````````````````
  5731. The title may be omitted:
  5732. ```````````````````````````````` example
  5733. [link](/uri)
  5734. .
  5735. <p><a href="/uri">link</a></p>
  5736. ````````````````````````````````
  5737. Both the title and the destination may be omitted:
  5738. ```````````````````````````````` example
  5739. [link]()
  5740. .
  5741. <p><a href="">link</a></p>
  5742. ````````````````````````````````
  5743. ```````````````````````````````` example
  5744. [link](<>)
  5745. .
  5746. <p><a href="">link</a></p>
  5747. ````````````````````````````````
  5748. The destination can only contain spaces if it is
  5749. enclosed in pointy brackets:
  5750. ```````````````````````````````` example
  5751. [link](/my uri)
  5752. .
  5753. <p>[link](/my uri)</p>
  5754. ````````````````````````````````
  5755. ```````````````````````````````` example
  5756. [link](</my uri>)
  5757. .
  5758. <p><a href="/my%20uri">link</a></p>
  5759. ````````````````````````````````
  5760. The destination cannot contain line breaks,
  5761. even if enclosed in pointy brackets:
  5762. ```````````````````````````````` example
  5763. [link](foo
  5764. bar)
  5765. .
  5766. <p>[link](foo
  5767. bar)</p>
  5768. ````````````````````````````````
  5769. ```````````````````````````````` example
  5770. [link](<foo
  5771. bar>)
  5772. .
  5773. <p>[link](<foo
  5774. bar>)</p>
  5775. ````````````````````````````````
  5776. The destination can contain `)` if it is enclosed
  5777. in pointy brackets:
  5778. ```````````````````````````````` example
  5779. [a](<b)c>)
  5780. .
  5781. <p><a href="b)c">a</a></p>
  5782. ````````````````````````````````
  5783. Pointy brackets that enclose links must be unescaped:
  5784. ```````````````````````````````` example
  5785. [link](<foo\>)
  5786. .
  5787. <p>[link](&lt;foo&gt;)</p>
  5788. ````````````````````````````````
  5789. These are not links, because the opening pointy bracket
  5790. is not matched properly:
  5791. ```````````````````````````````` example
  5792. [a](<b)c
  5793. [a](<b)c>
  5794. [a](<b>c)
  5795. .
  5796. <p>[a](&lt;b)c
  5797. [a](&lt;b)c&gt;
  5798. [a](<b>c)</p>
  5799. ````````````````````````````````
  5800. Parentheses inside the link destination may be escaped:
  5801. ```````````````````````````````` example
  5802. [link](\(foo\))
  5803. .
  5804. <p><a href="(foo)">link</a></p>
  5805. ````````````````````````````````
  5806. Any number of parentheses are allowed without escaping, as long as they are
  5807. balanced:
  5808. ```````````````````````````````` example
  5809. [link](foo(and(bar)))
  5810. .
  5811. <p><a href="foo(and(bar))">link</a></p>
  5812. ````````````````````````````````
  5813. However, if you have unbalanced parentheses, you need to escape or use the
  5814. `<...>` form:
  5815. ```````````````````````````````` example
  5816. [link](foo\(and\(bar\))
  5817. .
  5818. <p><a href="foo(and(bar)">link</a></p>
  5819. ````````````````````````````````
  5820. ```````````````````````````````` example
  5821. [link](<foo(and(bar)>)
  5822. .
  5823. <p><a href="foo(and(bar)">link</a></p>
  5824. ````````````````````````````````
  5825. Parentheses and other symbols can also be escaped, as usual
  5826. in Markdown:
  5827. ```````````````````````````````` example
  5828. [link](foo\)\:)
  5829. .
  5830. <p><a href="foo):">link</a></p>
  5831. ````````````````````````````````
  5832. A link can contain fragment identifiers and queries:
  5833. ```````````````````````````````` example
  5834. [link](#fragment)
  5835. [link](http://example.com#fragment)
  5836. [link](http://example.com?foo=3#frag)
  5837. .
  5838. <p><a href="#fragment">link</a></p>
  5839. <p><a href="http://example.com#fragment">link</a></p>
  5840. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5841. ````````````````````````````````
  5842. Note that a backslash before a non-escapable character is
  5843. just a backslash:
  5844. ```````````````````````````````` example
  5845. [link](foo\bar)
  5846. .
  5847. <p><a href="foo%5Cbar">link</a></p>
  5848. ````````````````````````````````
  5849. URL-escaping should be left alone inside the destination, as all
  5850. URL-escaped characters are also valid URL characters. Entity and
  5851. numerical character references in the destination will be parsed
  5852. into the corresponding Unicode code points, as usual. These may
  5853. be optionally URL-escaped when written as HTML, but this spec
  5854. does not enforce any particular policy for rendering URLs in
  5855. HTML or other formats. Renderers may make different decisions
  5856. about how to escape or normalize URLs in the output.
  5857. ```````````````````````````````` example
  5858. [link](foo%20b&auml;)
  5859. .
  5860. <p><a href="foo%20b%C3%A4">link</a></p>
  5861. ````````````````````````````````
  5862. Note that, because titles can often be parsed as destinations,
  5863. if you try to omit the destination and keep the title, you'll
  5864. get unexpected results:
  5865. ```````````````````````````````` example
  5866. [link]("title")
  5867. .
  5868. <p><a href="%22title%22">link</a></p>
  5869. ````````````````````````````````
  5870. Titles may be in single quotes, double quotes, or parentheses:
  5871. ```````````````````````````````` example
  5872. [link](/url "title")
  5873. [link](/url 'title')
  5874. [link](/url (title))
  5875. .
  5876. <p><a href="/url" title="title">link</a>
  5877. <a href="/url" title="title">link</a>
  5878. <a href="/url" title="title">link</a></p>
  5879. ````````````````````````````````
  5880. Backslash escapes and entity and numeric character references
  5881. may be used in titles:
  5882. ```````````````````````````````` example
  5883. [link](/url "title \"&quot;")
  5884. .
  5885. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5886. ````````````````````````````````
  5887. Titles must be separated from the link using a [whitespace].
  5888. Other [Unicode whitespace] like non-breaking space doesn't work.
  5889. ```````````````````````````````` example
  5890. [link](/url "title")
  5891. .
  5892. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5893. ````````````````````````````````
  5894. Nested balanced quotes are not allowed without escaping:
  5895. ```````````````````````````````` example
  5896. [link](/url "title "and" title")
  5897. .
  5898. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5899. ````````````````````````````````
  5900. But it is easy to work around this by using a different quote type:
  5901. ```````````````````````````````` example
  5902. [link](/url 'title "and" title')
  5903. .
  5904. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5905. ````````````````````````````````
  5906. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5907. title, and its test suite included a test demonstrating this.
  5908. But it is hard to see a good rationale for the extra complexity this
  5909. brings, since there are already many ways---backslash escaping,
  5910. entity and numeric character references, or using a different
  5911. quote type for the enclosing title---to write titles containing
  5912. double quotes. `Markdown.pl`'s handling of titles has a number
  5913. of other strange features. For example, it allows single-quoted
  5914. titles in inline links, but not reference links. And, in
  5915. reference links but not inline links, it allows a title to begin
  5916. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5917. titles with no closing quotation mark, though 1.0.2b8 does not.
  5918. It seems preferable to adopt a simple, rational rule that works
  5919. the same way in inline links and link reference definitions.)
  5920. [Whitespace] is allowed around the destination and title:
  5921. ```````````````````````````````` example
  5922. [link]( /uri
  5923. "title" )
  5924. .
  5925. <p><a href="/uri" title="title">link</a></p>
  5926. ````````````````````````````````
  5927. But it is not allowed between the link text and the
  5928. following parenthesis:
  5929. ```````````````````````````````` example
  5930. [link] (/uri)
  5931. .
  5932. <p>[link] (/uri)</p>
  5933. ````````````````````````````````
  5934. The link text may contain balanced brackets, but not unbalanced ones,
  5935. unless they are escaped:
  5936. ```````````````````````````````` example
  5937. [link [foo [bar]]](/uri)
  5938. .
  5939. <p><a href="/uri">link [foo [bar]]</a></p>
  5940. ````````````````````````````````
  5941. ```````````````````````````````` example
  5942. [link] bar](/uri)
  5943. .
  5944. <p>[link] bar](/uri)</p>
  5945. ````````````````````````````````
  5946. ```````````````````````````````` example
  5947. [link [bar](/uri)
  5948. .
  5949. <p>[link <a href="/uri">bar</a></p>
  5950. ````````````````````````````````
  5951. ```````````````````````````````` example
  5952. [link \[bar](/uri)
  5953. .
  5954. <p><a href="/uri">link [bar</a></p>
  5955. ````````````````````````````````
  5956. The link text may contain inline content:
  5957. ```````````````````````````````` example
  5958. [link *foo **bar** `#`*](/uri)
  5959. .
  5960. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5961. ````````````````````````````````
  5962. ```````````````````````````````` example
  5963. [![moon](moon.jpg)](/uri)
  5964. .
  5965. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5966. ````````````````````````````````
  5967. However, links may not contain other links, at any level of nesting.
  5968. ```````````````````````````````` example
  5969. [foo [bar](/uri)](/uri)
  5970. .
  5971. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5972. ````````````````````````````````
  5973. ```````````````````````````````` example
  5974. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5975. .
  5976. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5977. ````````````````````````````````
  5978. ```````````````````````````````` example
  5979. ![[[foo](uri1)](uri2)](uri3)
  5980. .
  5981. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5982. ````````````````````````````````
  5983. These cases illustrate the precedence of link text grouping over
  5984. emphasis grouping:
  5985. ```````````````````````````````` example
  5986. *[foo*](/uri)
  5987. .
  5988. <p>*<a href="/uri">foo*</a></p>
  5989. ````````````````````````````````
  5990. ```````````````````````````````` example
  5991. [foo *bar](baz*)
  5992. .
  5993. <p><a href="baz*">foo *bar</a></p>
  5994. ````````````````````````````````
  5995. Note that brackets that *aren't* part of links do not take
  5996. precedence:
  5997. ```````````````````````````````` example
  5998. *foo [bar* baz]
  5999. .
  6000. <p><em>foo [bar</em> baz]</p>
  6001. ````````````````````````````````
  6002. These cases illustrate the precedence of HTML tags, code spans,
  6003. and autolinks over link grouping:
  6004. ```````````````````````````````` example
  6005. [foo <bar attr="](baz)">
  6006. .
  6007. <p>[foo <bar attr="](baz)"></p>
  6008. ````````````````````````````````
  6009. ```````````````````````````````` example
  6010. [foo`](/uri)`
  6011. .
  6012. <p>[foo<code>](/uri)</code></p>
  6013. ````````````````````````````````
  6014. ```````````````````````````````` example
  6015. [foo<http://example.com/?search=](uri)>
  6016. .
  6017. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6018. ````````````````````````````````
  6019. There are three kinds of [reference link](@)s:
  6020. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6021. and [shortcut](#shortcut-reference-link).
  6022. A [full reference link](@)
  6023. consists of a [link text] immediately followed by a [link label]
  6024. that [matches] a [link reference definition] elsewhere in the document.
  6025. A [link label](@) begins with a left bracket (`[`) and ends
  6026. with the first right bracket (`]`) that is not backslash-escaped.
  6027. Between these brackets there must be at least one [non-whitespace character].
  6028. Unescaped square bracket characters are not allowed inside the
  6029. opening and closing square brackets of [link labels]. A link
  6030. label can have at most 999 characters inside the square
  6031. brackets.
  6032. One label [matches](@)
  6033. another just in case their normalized forms are equal. To normalize a
  6034. label, strip off the opening and closing brackets,
  6035. perform the *Unicode case fold*, strip leading and trailing
  6036. [whitespace] and collapse consecutive internal
  6037. [whitespace] to a single space. If there are multiple
  6038. matching reference link definitions, the one that comes first in the
  6039. document is used. (It is desirable in such cases to emit a warning.)
  6040. The link's URI and title are provided by the matching [link
  6041. reference definition].
  6042. Here is a simple example:
  6043. ```````````````````````````````` example
  6044. [foo][bar]
  6045. [bar]: /url "title"
  6046. .
  6047. <p><a href="/url" title="title">foo</a></p>
  6048. ````````````````````````````````
  6049. The rules for the [link text] are the same as with
  6050. [inline links]. Thus:
  6051. The link text may contain balanced brackets, but not unbalanced ones,
  6052. unless they are escaped:
  6053. ```````````````````````````````` example
  6054. [link [foo [bar]]][ref]
  6055. [ref]: /uri
  6056. .
  6057. <p><a href="/uri">link [foo [bar]]</a></p>
  6058. ````````````````````````````````
  6059. ```````````````````````````````` example
  6060. [link \[bar][ref]
  6061. [ref]: /uri
  6062. .
  6063. <p><a href="/uri">link [bar</a></p>
  6064. ````````````````````````````````
  6065. The link text may contain inline content:
  6066. ```````````````````````````````` example
  6067. [link *foo **bar** `#`*][ref]
  6068. [ref]: /uri
  6069. .
  6070. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6071. ````````````````````````````````
  6072. ```````````````````````````````` example
  6073. [![moon](moon.jpg)][ref]
  6074. [ref]: /uri
  6075. .
  6076. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6077. ````````````````````````````````
  6078. However, links may not contain other links, at any level of nesting.
  6079. ```````````````````````````````` example
  6080. [foo [bar](/uri)][ref]
  6081. [ref]: /uri
  6082. .
  6083. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6084. ````````````````````````````````
  6085. ```````````````````````````````` example
  6086. [foo *bar [baz][ref]*][ref]
  6087. [ref]: /uri
  6088. .
  6089. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6090. ````````````````````````````````
  6091. (In the examples above, we have two [shortcut reference links]
  6092. instead of one [full reference link].)
  6093. The following cases illustrate the precedence of link text grouping over
  6094. emphasis grouping:
  6095. ```````````````````````````````` example
  6096. *[foo*][ref]
  6097. [ref]: /uri
  6098. .
  6099. <p>*<a href="/uri">foo*</a></p>
  6100. ````````````````````````````````
  6101. ```````````````````````````````` example
  6102. [foo *bar][ref]*
  6103. [ref]: /uri
  6104. .
  6105. <p><a href="/uri">foo *bar</a>*</p>
  6106. ````````````````````````````````
  6107. These cases illustrate the precedence of HTML tags, code spans,
  6108. and autolinks over link grouping:
  6109. ```````````````````````````````` example
  6110. [foo <bar attr="][ref]">
  6111. [ref]: /uri
  6112. .
  6113. <p>[foo <bar attr="][ref]"></p>
  6114. ````````````````````````````````
  6115. ```````````````````````````````` example
  6116. [foo`][ref]`
  6117. [ref]: /uri
  6118. .
  6119. <p>[foo<code>][ref]</code></p>
  6120. ````````````````````````````````
  6121. ```````````````````````````````` example
  6122. [foo<http://example.com/?search=][ref]>
  6123. [ref]: /uri
  6124. .
  6125. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6126. ````````````````````````````````
  6127. Matching is case-insensitive:
  6128. ```````````````````````````````` example
  6129. [foo][BaR]
  6130. [bar]: /url "title"
  6131. .
  6132. <p><a href="/url" title="title">foo</a></p>
  6133. ````````````````````````````````
  6134. Unicode case fold is used:
  6135. ```````````````````````````````` example
  6136. [ẞ]
  6137. [SS]: /url
  6138. .
  6139. <p><a href="/url">ẞ</a></p>
  6140. ````````````````````````````````
  6141. Consecutive internal [whitespace] is treated as one space for
  6142. purposes of determining matching:
  6143. ```````````````````````````````` example
  6144. [Foo
  6145. bar]: /url
  6146. [Baz][Foo bar]
  6147. .
  6148. <p><a href="/url">Baz</a></p>
  6149. ````````````````````````````````
  6150. No [whitespace] is allowed between the [link text] and the
  6151. [link label]:
  6152. ```````````````````````````````` example
  6153. [foo] [bar]
  6154. [bar]: /url "title"
  6155. .
  6156. <p>[foo] <a href="/url" title="title">bar</a></p>
  6157. ````````````````````````````````
  6158. ```````````````````````````````` example
  6159. [foo]
  6160. [bar]
  6161. [bar]: /url "title"
  6162. .
  6163. <p>[foo]
  6164. <a href="/url" title="title">bar</a></p>
  6165. ````````````````````````````````
  6166. This is a departure from John Gruber's original Markdown syntax
  6167. description, which explicitly allows whitespace between the link
  6168. text and the link label. It brings reference links in line with
  6169. [inline links], which (according to both original Markdown and
  6170. this spec) cannot have whitespace after the link text. More
  6171. importantly, it prevents inadvertent capture of consecutive
  6172. [shortcut reference links]. If whitespace is allowed between the
  6173. link text and the link label, then in the following we will have
  6174. a single reference link, not two shortcut reference links, as
  6175. intended:
  6176. ``` markdown
  6177. [foo]
  6178. [bar]
  6179. [foo]: /url1
  6180. [bar]: /url2
  6181. ```
  6182. (Note that [shortcut reference links] were introduced by Gruber
  6183. himself in a beta version of `Markdown.pl`, but never included
  6184. in the official syntax description. Without shortcut reference
  6185. links, it is harmless to allow space between the link text and
  6186. link label; but once shortcut references are introduced, it is
  6187. too dangerous to allow this, as it frequently leads to
  6188. unintended results.)
  6189. When there are multiple matching [link reference definitions],
  6190. the first is used:
  6191. ```````````````````````````````` example
  6192. [foo]: /url1
  6193. [foo]: /url2
  6194. [bar][foo]
  6195. .
  6196. <p><a href="/url1">bar</a></p>
  6197. ````````````````````````````````
  6198. Note that matching is performed on normalized strings, not parsed
  6199. inline content. So the following does not match, even though the
  6200. labels define equivalent inline content:
  6201. ```````````````````````````````` example
  6202. [bar][foo\!]
  6203. [foo!]: /url
  6204. .
  6205. <p>[bar][foo!]</p>
  6206. ````````````````````````````````
  6207. [Link labels] cannot contain brackets, unless they are
  6208. backslash-escaped:
  6209. ```````````````````````````````` example
  6210. [foo][ref[]
  6211. [ref[]: /uri
  6212. .
  6213. <p>[foo][ref[]</p>
  6214. <p>[ref[]: /uri</p>
  6215. ````````````````````````````````
  6216. ```````````````````````````````` example
  6217. [foo][ref[bar]]
  6218. [ref[bar]]: /uri
  6219. .
  6220. <p>[foo][ref[bar]]</p>
  6221. <p>[ref[bar]]: /uri</p>
  6222. ````````````````````````````````
  6223. ```````````````````````````````` example
  6224. [[[foo]]]
  6225. [[[foo]]]: /url
  6226. .
  6227. <p>[[[foo]]]</p>
  6228. <p>[[[foo]]]: /url</p>
  6229. ````````````````````````````````
  6230. ```````````````````````````````` example
  6231. [foo][ref\[]
  6232. [ref\[]: /uri
  6233. .
  6234. <p><a href="/uri">foo</a></p>
  6235. ````````````````````````````````
  6236. Note that in this example `]` is not backslash-escaped:
  6237. ```````````````````````````````` example
  6238. [bar\\]: /uri
  6239. [bar\\]
  6240. .
  6241. <p><a href="/uri">bar\</a></p>
  6242. ````````````````````````````````
  6243. A [link label] must contain at least one [non-whitespace character]:
  6244. ```````````````````````````````` example
  6245. []
  6246. []: /uri
  6247. .
  6248. <p>[]</p>
  6249. <p>[]: /uri</p>
  6250. ````````````````````````````````
  6251. ```````````````````````````````` example
  6252. [
  6253. ]
  6254. [
  6255. ]: /uri
  6256. .
  6257. <p>[
  6258. ]</p>
  6259. <p>[
  6260. ]: /uri</p>
  6261. ````````````````````````````````
  6262. A [collapsed reference link](@)
  6263. consists of a [link label] that [matches] a
  6264. [link reference definition] elsewhere in the
  6265. document, followed by the string `[]`.
  6266. The contents of the first link label are parsed as inlines,
  6267. which are used as the link's text. The link's URI and title are
  6268. provided by the matching reference link definition. Thus,
  6269. `[foo][]` is equivalent to `[foo][foo]`.
  6270. ```````````````````````````````` example
  6271. [foo][]
  6272. [foo]: /url "title"
  6273. .
  6274. <p><a href="/url" title="title">foo</a></p>
  6275. ````````````````````````````````
  6276. ```````````````````````````````` example
  6277. [*foo* bar][]
  6278. [*foo* bar]: /url "title"
  6279. .
  6280. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6281. ````````````````````````````````
  6282. The link labels are case-insensitive:
  6283. ```````````````````````````````` example
  6284. [Foo][]
  6285. [foo]: /url "title"
  6286. .
  6287. <p><a href="/url" title="title">Foo</a></p>
  6288. ````````````````````````````````
  6289. As with full reference links, [whitespace] is not
  6290. allowed between the two sets of brackets:
  6291. ```````````````````````````````` example
  6292. [foo]
  6293. []
  6294. [foo]: /url "title"
  6295. .
  6296. <p><a href="/url" title="title">foo</a>
  6297. []</p>
  6298. ````````````````````````````````
  6299. A [shortcut reference link](@)
  6300. consists of a [link label] that [matches] a
  6301. [link reference definition] elsewhere in the
  6302. document and is not followed by `[]` or a link label.
  6303. The contents of the first link label are parsed as inlines,
  6304. which are used as the link's text. The link's URI and title
  6305. are provided by the matching link reference definition.
  6306. Thus, `[foo]` is equivalent to `[foo][]`.
  6307. ```````````````````````````````` example
  6308. [foo]
  6309. [foo]: /url "title"
  6310. .
  6311. <p><a href="/url" title="title">foo</a></p>
  6312. ````````````````````````````````
  6313. ```````````````````````````````` example
  6314. [*foo* bar]
  6315. [*foo* bar]: /url "title"
  6316. .
  6317. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6318. ````````````````````````````````
  6319. ```````````````````````````````` example
  6320. [[*foo* bar]]
  6321. [*foo* bar]: /url "title"
  6322. .
  6323. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6324. ````````````````````````````````
  6325. ```````````````````````````````` example
  6326. [[bar [foo]
  6327. [foo]: /url
  6328. .
  6329. <p>[[bar <a href="/url">foo</a></p>
  6330. ````````````````````````````````
  6331. The link labels are case-insensitive:
  6332. ```````````````````````````````` example
  6333. [Foo]
  6334. [foo]: /url "title"
  6335. .
  6336. <p><a href="/url" title="title">Foo</a></p>
  6337. ````````````````````````````````
  6338. A space after the link text should be preserved:
  6339. ```````````````````````````````` example
  6340. [foo] bar
  6341. [foo]: /url
  6342. .
  6343. <p><a href="/url">foo</a> bar</p>
  6344. ````````````````````````````````
  6345. If you just want bracketed text, you can backslash-escape the
  6346. opening bracket to avoid links:
  6347. ```````````````````````````````` example
  6348. \[foo]
  6349. [foo]: /url "title"
  6350. .
  6351. <p>[foo]</p>
  6352. ````````````````````````````````
  6353. Note that this is a link, because a link label ends with the first
  6354. following closing bracket:
  6355. ```````````````````````````````` example
  6356. [foo*]: /url
  6357. *[foo*]
  6358. .
  6359. <p>*<a href="/url">foo*</a></p>
  6360. ````````````````````````````````
  6361. Full and compact references take precedence over shortcut
  6362. references:
  6363. ```````````````````````````````` example
  6364. [foo][bar]
  6365. [foo]: /url1
  6366. [bar]: /url2
  6367. .
  6368. <p><a href="/url2">foo</a></p>
  6369. ````````````````````````````````
  6370. ```````````````````````````````` example
  6371. [foo][]
  6372. [foo]: /url1
  6373. .
  6374. <p><a href="/url1">foo</a></p>
  6375. ````````````````````````````````
  6376. Inline links also take precedence:
  6377. ```````````````````````````````` example
  6378. [foo]()
  6379. [foo]: /url1
  6380. .
  6381. <p><a href="">foo</a></p>
  6382. ````````````````````````````````
  6383. ```````````````````````````````` example
  6384. [foo](not a link)
  6385. [foo]: /url1
  6386. .
  6387. <p><a href="/url1">foo</a>(not a link)</p>
  6388. ````````````````````````````````
  6389. In the following case `[bar][baz]` is parsed as a reference,
  6390. `[foo]` as normal text:
  6391. ```````````````````````````````` example
  6392. [foo][bar][baz]
  6393. [baz]: /url
  6394. .
  6395. <p>[foo]<a href="/url">bar</a></p>
  6396. ````````````````````````````````
  6397. Here, though, `[foo][bar]` is parsed as a reference, since
  6398. `[bar]` is defined:
  6399. ```````````````````````````````` example
  6400. [foo][bar][baz]
  6401. [baz]: /url1
  6402. [bar]: /url2
  6403. .
  6404. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6405. ````````````````````````````````
  6406. Here `[foo]` is not parsed as a shortcut reference, because it
  6407. is followed by a link label (even though `[bar]` is not defined):
  6408. ```````````````````````````````` example
  6409. [foo][bar][baz]
  6410. [baz]: /url1
  6411. [foo]: /url2
  6412. .
  6413. <p>[foo]<a href="/url1">bar</a></p>
  6414. ````````````````````````````````
  6415. ## Images
  6416. Syntax for images is like the syntax for links, with one
  6417. difference. Instead of [link text], we have an
  6418. [image description](@). The rules for this are the
  6419. same as for [link text], except that (a) an
  6420. image description starts with `![` rather than `[`, and
  6421. (b) an image description may contain links.
  6422. An image description has inline elements
  6423. as its contents. When an image is rendered to HTML,
  6424. this is standardly used as the image's `alt` attribute.
  6425. ```````````````````````````````` example
  6426. ![foo](/url "title")
  6427. .
  6428. <p><img src="/url" alt="foo" title="title" /></p>
  6429. ````````````````````````````````
  6430. ```````````````````````````````` example
  6431. ![foo *bar*]
  6432. [foo *bar*]: train.jpg "train & tracks"
  6433. .
  6434. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6435. ````````````````````````````````
  6436. ```````````````````````````````` example
  6437. ![foo ![bar](/url)](/url2)
  6438. .
  6439. <p><img src="/url2" alt="foo bar" /></p>
  6440. ````````````````````````````````
  6441. ```````````````````````````````` example
  6442. ![foo [bar](/url)](/url2)
  6443. .
  6444. <p><img src="/url2" alt="foo bar" /></p>
  6445. ````````````````````````````````
  6446. Though this spec is concerned with parsing, not rendering, it is
  6447. recommended that in rendering to HTML, only the plain string content
  6448. of the [image description] be used. Note that in
  6449. the above example, the alt attribute's value is `foo bar`, not `foo
  6450. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6451. content is rendered, without formatting.
  6452. ```````````````````````````````` example
  6453. ![foo *bar*][]
  6454. [foo *bar*]: train.jpg "train & tracks"
  6455. .
  6456. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6457. ````````````````````````````````
  6458. ```````````````````````````````` example
  6459. ![foo *bar*][foobar]
  6460. [FOOBAR]: train.jpg "train & tracks"
  6461. .
  6462. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6463. ````````````````````````````````
  6464. ```````````````````````````````` example
  6465. ![foo](train.jpg)
  6466. .
  6467. <p><img src="train.jpg" alt="foo" /></p>
  6468. ````````````````````````````````
  6469. ```````````````````````````````` example
  6470. My ![foo bar](/path/to/train.jpg "title" )
  6471. .
  6472. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6473. ````````````````````````````````
  6474. ```````````````````````````````` example
  6475. ![foo](<url>)
  6476. .
  6477. <p><img src="url" alt="foo" /></p>
  6478. ````````````````````````````````
  6479. ```````````````````````````````` example
  6480. ![](/url)
  6481. .
  6482. <p><img src="/url" alt="" /></p>
  6483. ````````````````````````````````
  6484. Reference-style:
  6485. ```````````````````````````````` example
  6486. ![foo][bar]
  6487. [bar]: /url
  6488. .
  6489. <p><img src="/url" alt="foo" /></p>
  6490. ````````````````````````````````
  6491. ```````````````````````````````` example
  6492. ![foo][bar]
  6493. [BAR]: /url
  6494. .
  6495. <p><img src="/url" alt="foo" /></p>
  6496. ````````````````````````````````
  6497. Collapsed:
  6498. ```````````````````````````````` example
  6499. ![foo][]
  6500. [foo]: /url "title"
  6501. .
  6502. <p><img src="/url" alt="foo" title="title" /></p>
  6503. ````````````````````````````````
  6504. ```````````````````````````````` example
  6505. ![*foo* bar][]
  6506. [*foo* bar]: /url "title"
  6507. .
  6508. <p><img src="/url" alt="foo bar" title="title" /></p>
  6509. ````````````````````````````````
  6510. The labels are case-insensitive:
  6511. ```````````````````````````````` example
  6512. ![Foo][]
  6513. [foo]: /url "title"
  6514. .
  6515. <p><img src="/url" alt="Foo" title="title" /></p>
  6516. ````````````````````````````````
  6517. As with reference links, [whitespace] is not allowed
  6518. between the two sets of brackets:
  6519. ```````````````````````````````` example
  6520. ![foo]
  6521. []
  6522. [foo]: /url "title"
  6523. .
  6524. <p><img src="/url" alt="foo" title="title" />
  6525. []</p>
  6526. ````````````````````````````````
  6527. Shortcut:
  6528. ```````````````````````````````` example
  6529. ![foo]
  6530. [foo]: /url "title"
  6531. .
  6532. <p><img src="/url" alt="foo" title="title" /></p>
  6533. ````````````````````````````````
  6534. ```````````````````````````````` example
  6535. ![*foo* bar]
  6536. [*foo* bar]: /url "title"
  6537. .
  6538. <p><img src="/url" alt="foo bar" title="title" /></p>
  6539. ````````````````````````````````
  6540. Note that link labels cannot contain unescaped brackets:
  6541. ```````````````````````````````` example
  6542. ![[foo]]
  6543. [[foo]]: /url "title"
  6544. .
  6545. <p>![[foo]]</p>
  6546. <p>[[foo]]: /url &quot;title&quot;</p>
  6547. ````````````````````````````````
  6548. The link labels are case-insensitive:
  6549. ```````````````````````````````` example
  6550. ![Foo]
  6551. [foo]: /url "title"
  6552. .
  6553. <p><img src="/url" alt="Foo" title="title" /></p>
  6554. ````````````````````````````````
  6555. If you just want a literal `!` followed by bracketed text, you can
  6556. backslash-escape the opening `[`:
  6557. ```````````````````````````````` example
  6558. !\[foo]
  6559. [foo]: /url "title"
  6560. .
  6561. <p>![foo]</p>
  6562. ````````````````````````````````
  6563. If you want a link after a literal `!`, backslash-escape the
  6564. `!`:
  6565. ```````````````````````````````` example
  6566. \![foo]
  6567. [foo]: /url "title"
  6568. .
  6569. <p>!<a href="/url" title="title">foo</a></p>
  6570. ````````````````````````````````
  6571. ## Autolinks
  6572. [Autolink](@)s are absolute URIs and email addresses inside
  6573. `<` and `>`. They are parsed as links, with the URL or email address
  6574. as the link label.
  6575. A [URI autolink](@) consists of `<`, followed by an
  6576. [absolute URI] followed by `>`. It is parsed as
  6577. a link to the URI, with the URI as the link's label.
  6578. An [absolute URI](@),
  6579. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6580. followed by zero or more characters other than ASCII
  6581. [whitespace] and control characters, `<`, and `>`. If
  6582. the URI includes these characters, they must be percent-encoded
  6583. (e.g. `%20` for a space).
  6584. For purposes of this spec, a [scheme](@) is any sequence
  6585. of 2--32 characters beginning with an ASCII letter and followed
  6586. by any combination of ASCII letters, digits, or the symbols plus
  6587. ("+"), period ("."), or hyphen ("-").
  6588. Here are some valid autolinks:
  6589. ```````````````````````````````` example
  6590. <http://foo.bar.baz>
  6591. .
  6592. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6593. ````````````````````````````````
  6594. ```````````````````````````````` example
  6595. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6596. .
  6597. <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>
  6598. ````````````````````````````````
  6599. ```````````````````````````````` example
  6600. <irc://foo.bar:2233/baz>
  6601. .
  6602. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6603. ````````````````````````````````
  6604. Uppercase is also fine:
  6605. ```````````````````````````````` example
  6606. <MAILTO:FOO@BAR.BAZ>
  6607. .
  6608. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6609. ````````````````````````````````
  6610. Note that many strings that count as [absolute URIs] for
  6611. purposes of this spec are not valid URIs, because their
  6612. schemes are not registered or because of other problems
  6613. with their syntax:
  6614. ```````````````````````````````` example
  6615. <a+b+c:d>
  6616. .
  6617. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6618. ````````````````````````````````
  6619. ```````````````````````````````` example
  6620. <made-up-scheme://foo,bar>
  6621. .
  6622. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6623. ````````````````````````````````
  6624. ```````````````````````````````` example
  6625. <http://../>
  6626. .
  6627. <p><a href="http://../">http://../</a></p>
  6628. ````````````````````````````````
  6629. ```````````````````````````````` example
  6630. <localhost:5001/foo>
  6631. .
  6632. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6633. ````````````````````````````````
  6634. Spaces are not allowed in autolinks:
  6635. ```````````````````````````````` example
  6636. <http://foo.bar/baz bim>
  6637. .
  6638. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6639. ````````````````````````````````
  6640. Backslash-escapes do not work inside autolinks:
  6641. ```````````````````````````````` example
  6642. <http://example.com/\[\>
  6643. .
  6644. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6645. ````````````````````````````````
  6646. An [email autolink](@)
  6647. consists of `<`, followed by an [email address],
  6648. followed by `>`. The link's label is the email address,
  6649. and the URL is `mailto:` followed by the email address.
  6650. An [email address](@),
  6651. for these purposes, is anything that matches
  6652. the [non-normative regex from the HTML5
  6653. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6654. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6655. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6656. Examples of email autolinks:
  6657. ```````````````````````````````` example
  6658. <foo@bar.example.com>
  6659. .
  6660. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6661. ````````````````````````````````
  6662. ```````````````````````````````` example
  6663. <foo+special@Bar.baz-bar0.com>
  6664. .
  6665. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6666. ````````````````````````````````
  6667. Backslash-escapes do not work inside email autolinks:
  6668. ```````````````````````````````` example
  6669. <foo\+@bar.example.com>
  6670. .
  6671. <p>&lt;foo+@bar.example.com&gt;</p>
  6672. ````````````````````````````````
  6673. These are not autolinks:
  6674. ```````````````````````````````` example
  6675. <>
  6676. .
  6677. <p>&lt;&gt;</p>
  6678. ````````````````````````````````
  6679. ```````````````````````````````` example
  6680. < http://foo.bar >
  6681. .
  6682. <p>&lt; http://foo.bar &gt;</p>
  6683. ````````````````````````````````
  6684. ```````````````````````````````` example
  6685. <m:abc>
  6686. .
  6687. <p>&lt;m:abc&gt;</p>
  6688. ````````````````````````````````
  6689. ```````````````````````````````` example
  6690. <foo.bar.baz>
  6691. .
  6692. <p>&lt;foo.bar.baz&gt;</p>
  6693. ````````````````````````````````
  6694. ```````````````````````````````` example
  6695. http://example.com
  6696. .
  6697. <p>http://example.com</p>
  6698. ````````````````````````````````
  6699. ```````````````````````````````` example
  6700. foo@bar.example.com
  6701. .
  6702. <p>foo@bar.example.com</p>
  6703. ````````````````````````````````
  6704. ## Raw HTML
  6705. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6706. raw HTML tag and will be rendered in HTML without escaping.
  6707. Tag and attribute names are not limited to current HTML tags,
  6708. so custom tags (and even, say, DocBook tags) may be used.
  6709. Here is the grammar for tags:
  6710. A [tag name](@) consists of an ASCII letter
  6711. followed by zero or more ASCII letters, digits, or
  6712. hyphens (`-`).
  6713. An [attribute](@) consists of [whitespace],
  6714. an [attribute name], and an optional
  6715. [attribute value specification].
  6716. An [attribute name](@)
  6717. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6718. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6719. specification restricted to ASCII. HTML5 is laxer.)
  6720. An [attribute value specification](@)
  6721. consists of optional [whitespace],
  6722. a `=` character, optional [whitespace], and an [attribute
  6723. value].
  6724. An [attribute value](@)
  6725. consists of an [unquoted attribute value],
  6726. a [single-quoted attribute value], or a [double-quoted attribute value].
  6727. An [unquoted attribute value](@)
  6728. is a nonempty string of characters not
  6729. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6730. A [single-quoted attribute value](@)
  6731. consists of `'`, zero or more
  6732. characters not including `'`, and a final `'`.
  6733. A [double-quoted attribute value](@)
  6734. consists of `"`, zero or more
  6735. characters not including `"`, and a final `"`.
  6736. An [open tag](@) consists of a `<` character, a [tag name],
  6737. zero or more [attributes], optional [whitespace], an optional `/`
  6738. character, and a `>` character.
  6739. A [closing tag](@) consists of the string `</`, a
  6740. [tag name], optional [whitespace], and the character `>`.
  6741. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6742. where *text* does not start with `>` or `->`, does not end with `-`,
  6743. and does not contain `--`. (See the
  6744. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6745. A [processing instruction](@)
  6746. consists of the string `<?`, a string
  6747. of characters not including the string `?>`, and the string
  6748. `?>`.
  6749. A [declaration](@) consists of the
  6750. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6751. [whitespace], a string of characters not including the
  6752. character `>`, and the character `>`.
  6753. A [CDATA section](@) consists of
  6754. the string `<![CDATA[`, a string of characters not including the string
  6755. `]]>`, and the string `]]>`.
  6756. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6757. an [HTML comment], a [processing instruction], a [declaration],
  6758. or a [CDATA section].
  6759. Here are some simple open tags:
  6760. ```````````````````````````````` example
  6761. <a><bab><c2c>
  6762. .
  6763. <p><a><bab><c2c></p>
  6764. ````````````````````````````````
  6765. Empty elements:
  6766. ```````````````````````````````` example
  6767. <a/><b2/>
  6768. .
  6769. <p><a/><b2/></p>
  6770. ````````````````````````````````
  6771. [Whitespace] is allowed:
  6772. ```````````````````````````````` example
  6773. <a /><b2
  6774. data="foo" >
  6775. .
  6776. <p><a /><b2
  6777. data="foo" ></p>
  6778. ````````````````````````````````
  6779. With attributes:
  6780. ```````````````````````````````` example
  6781. <a foo="bar" bam = 'baz <em>"</em>'
  6782. _boolean zoop:33=zoop:33 />
  6783. .
  6784. <p><a foo="bar" bam = 'baz <em>"</em>'
  6785. _boolean zoop:33=zoop:33 /></p>
  6786. ````````````````````````````````
  6787. Custom tag names can be used:
  6788. ```````````````````````````````` example
  6789. Foo <responsive-image src="foo.jpg" />
  6790. .
  6791. <p>Foo <responsive-image src="foo.jpg" /></p>
  6792. ````````````````````````````````
  6793. Illegal tag names, not parsed as HTML:
  6794. ```````````````````````````````` example
  6795. <33> <__>
  6796. .
  6797. <p>&lt;33&gt; &lt;__&gt;</p>
  6798. ````````````````````````````````
  6799. Illegal attribute names:
  6800. ```````````````````````````````` example
  6801. <a h*#ref="hi">
  6802. .
  6803. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6804. ````````````````````````````````
  6805. Illegal attribute values:
  6806. ```````````````````````````````` example
  6807. <a href="hi'> <a href=hi'>
  6808. .
  6809. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6810. ````````````````````````````````
  6811. Illegal [whitespace]:
  6812. ```````````````````````````````` example
  6813. < a><
  6814. foo><bar/ >
  6815. <foo bar=baz
  6816. bim!bop />
  6817. .
  6818. <p>&lt; a&gt;&lt;
  6819. foo&gt;&lt;bar/ &gt;
  6820. &lt;foo bar=baz
  6821. bim!bop /&gt;</p>
  6822. ````````````````````````````````
  6823. Missing [whitespace]:
  6824. ```````````````````````````````` example
  6825. <a href='bar'title=title>
  6826. .
  6827. <p>&lt;a href='bar'title=title&gt;</p>
  6828. ````````````````````````````````
  6829. Closing tags:
  6830. ```````````````````````````````` example
  6831. </a></foo >
  6832. .
  6833. <p></a></foo ></p>
  6834. ````````````````````````````````
  6835. Illegal attributes in closing tag:
  6836. ```````````````````````````````` example
  6837. </a href="foo">
  6838. .
  6839. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6840. ````````````````````````````````
  6841. Comments:
  6842. ```````````````````````````````` example
  6843. foo <!-- this is a
  6844. comment - with hyphen -->
  6845. .
  6846. <p>foo <!-- this is a
  6847. comment - with hyphen --></p>
  6848. ````````````````````````````````
  6849. ```````````````````````````````` example
  6850. foo <!-- not a comment -- two hyphens -->
  6851. .
  6852. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6853. ````````````````````````````````
  6854. Not comments:
  6855. ```````````````````````````````` example
  6856. foo <!--> foo -->
  6857. foo <!-- foo--->
  6858. .
  6859. <p>foo &lt;!--&gt; foo --&gt;</p>
  6860. <p>foo &lt;!-- foo---&gt;</p>
  6861. ````````````````````````````````
  6862. Processing instructions:
  6863. ```````````````````````````````` example
  6864. foo <?php echo $a; ?>
  6865. .
  6866. <p>foo <?php echo $a; ?></p>
  6867. ````````````````````````````````
  6868. Declarations:
  6869. ```````````````````````````````` example
  6870. foo <!ELEMENT br EMPTY>
  6871. .
  6872. <p>foo <!ELEMENT br EMPTY></p>
  6873. ````````````````````````````````
  6874. CDATA sections:
  6875. ```````````````````````````````` example
  6876. foo <![CDATA[>&<]]>
  6877. .
  6878. <p>foo <![CDATA[>&<]]></p>
  6879. ````````````````````````````````
  6880. Entity and numeric character references are preserved in HTML
  6881. attributes:
  6882. ```````````````````````````````` example
  6883. foo <a href="&ouml;">
  6884. .
  6885. <p>foo <a href="&ouml;"></p>
  6886. ````````````````````````````````
  6887. Backslash escapes do not work in HTML attributes:
  6888. ```````````````````````````````` example
  6889. foo <a href="\*">
  6890. .
  6891. <p>foo <a href="\*"></p>
  6892. ````````````````````````````````
  6893. ```````````````````````````````` example
  6894. <a href="\"">
  6895. .
  6896. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6897. ````````````````````````````````
  6898. ## Hard line breaks
  6899. A line break (not in a code span or HTML tag) that is preceded
  6900. by two or more spaces and does not occur at the end of a block
  6901. is parsed as a [hard line break](@) (rendered
  6902. in HTML as a `<br />` tag):
  6903. ```````````````````````````````` example
  6904. foo
  6905. baz
  6906. .
  6907. <p>foo<br />
  6908. baz</p>
  6909. ````````````````````````````````
  6910. For a more visible alternative, a backslash before the
  6911. [line ending] may be used instead of two spaces:
  6912. ```````````````````````````````` example
  6913. foo\
  6914. baz
  6915. .
  6916. <p>foo<br />
  6917. baz</p>
  6918. ````````````````````````````````
  6919. More than two spaces can be used:
  6920. ```````````````````````````````` example
  6921. foo
  6922. baz
  6923. .
  6924. <p>foo<br />
  6925. baz</p>
  6926. ````````````````````````````````
  6927. Leading spaces at the beginning of the next line are ignored:
  6928. ```````````````````````````````` example
  6929. foo
  6930. bar
  6931. .
  6932. <p>foo<br />
  6933. bar</p>
  6934. ````````````````````````````````
  6935. ```````````````````````````````` example
  6936. foo\
  6937. bar
  6938. .
  6939. <p>foo<br />
  6940. bar</p>
  6941. ````````````````````````````````
  6942. Line breaks can occur inside emphasis, links, and other constructs
  6943. that allow inline content:
  6944. ```````````````````````````````` example
  6945. *foo
  6946. bar*
  6947. .
  6948. <p><em>foo<br />
  6949. bar</em></p>
  6950. ````````````````````````````````
  6951. ```````````````````````````````` example
  6952. *foo\
  6953. bar*
  6954. .
  6955. <p><em>foo<br />
  6956. bar</em></p>
  6957. ````````````````````````````````
  6958. Line breaks do not occur inside code spans
  6959. ```````````````````````````````` example
  6960. `code
  6961. span`
  6962. .
  6963. <p><code>code span</code></p>
  6964. ````````````````````````````````
  6965. ```````````````````````````````` example
  6966. `code\
  6967. span`
  6968. .
  6969. <p><code>code\ span</code></p>
  6970. ````````````````````````````````
  6971. or HTML tags:
  6972. ```````````````````````````````` example
  6973. <a href="foo
  6974. bar">
  6975. .
  6976. <p><a href="foo
  6977. bar"></p>
  6978. ````````````````````````````````
  6979. ```````````````````````````````` example
  6980. <a href="foo\
  6981. bar">
  6982. .
  6983. <p><a href="foo\
  6984. bar"></p>
  6985. ````````````````````````````````
  6986. Hard line breaks are for separating inline content within a block.
  6987. Neither syntax for hard line breaks works at the end of a paragraph or
  6988. other block element:
  6989. ```````````````````````````````` example
  6990. foo\
  6991. .
  6992. <p>foo\</p>
  6993. ````````````````````````````````
  6994. ```````````````````````````````` example
  6995. foo
  6996. .
  6997. <p>foo</p>
  6998. ````````````````````````````````
  6999. ```````````````````````````````` example
  7000. ### foo\
  7001. .
  7002. <h3>foo\</h3>
  7003. ````````````````````````````````
  7004. ```````````````````````````````` example
  7005. ### foo
  7006. .
  7007. <h3>foo</h3>
  7008. ````````````````````````````````
  7009. ## Soft line breaks
  7010. A regular line break (not in a code span or HTML tag) that is not
  7011. preceded by two or more spaces or a backslash is parsed as a
  7012. [softbreak](@). (A softbreak may be rendered in HTML either as a
  7013. [line ending] or as a space. The result will be the same in
  7014. browsers. In the examples here, a [line ending] will be used.)
  7015. ```````````````````````````````` example
  7016. foo
  7017. baz
  7018. .
  7019. <p>foo
  7020. baz</p>
  7021. ````````````````````````````````
  7022. Spaces at the end of the line and beginning of the next line are
  7023. removed:
  7024. ```````````````````````````````` example
  7025. foo
  7026. baz
  7027. .
  7028. <p>foo
  7029. baz</p>
  7030. ````````````````````````````````
  7031. A conforming parser may render a soft line break in HTML either as a
  7032. line break or as a space.
  7033. A renderer may also provide an option to render soft line breaks
  7034. as hard line breaks.
  7035. ## Textual content
  7036. Any characters not given an interpretation by the above rules will
  7037. be parsed as plain textual content.
  7038. ```````````````````````````````` example
  7039. hello $.;'there
  7040. .
  7041. <p>hello $.;'there</p>
  7042. ````````````````````````````````
  7043. ```````````````````````````````` example
  7044. Foo χρῆν
  7045. .
  7046. <p>Foo χρῆν</p>
  7047. ````````````````````````````````
  7048. Internal spaces are preserved verbatim:
  7049. ```````````````````````````````` example
  7050. Multiple spaces
  7051. .
  7052. <p>Multiple spaces</p>
  7053. ````````````````````````````````
  7054. <!-- END TESTS -->
  7055. # Appendix: A parsing strategy
  7056. In this appendix we describe some features of the parsing strategy
  7057. used in the CommonMark reference implementations.
  7058. ## Overview
  7059. Parsing has two phases:
  7060. 1. In the first phase, lines of input are consumed and the block
  7061. structure of the document---its division into paragraphs, block quotes,
  7062. list items, and so on---is constructed. Text is assigned to these
  7063. blocks but not parsed. Link reference definitions are parsed and a
  7064. map of links is constructed.
  7065. 2. In the second phase, the raw text contents of paragraphs and headings
  7066. are parsed into sequences of Markdown inline elements (strings,
  7067. code spans, links, emphasis, and so on), using the map of link
  7068. references constructed in phase 1.
  7069. At each point in processing, the document is represented as a tree of
  7070. **blocks**. The root of the tree is a `document` block. The `document`
  7071. may have any number of other blocks as **children**. These children
  7072. may, in turn, have other blocks as children. The last child of a block
  7073. is normally considered **open**, meaning that subsequent lines of input
  7074. can alter its contents. (Blocks that are not open are **closed**.)
  7075. Here, for example, is a possible document tree, with the open blocks
  7076. marked by arrows:
  7077. ``` tree
  7078. -> document
  7079. -> block_quote
  7080. paragraph
  7081. "Lorem ipsum dolor\nsit amet."
  7082. -> list (type=bullet tight=true bullet_char=-)
  7083. list_item
  7084. paragraph
  7085. "Qui *quodsi iracundia*"
  7086. -> list_item
  7087. -> paragraph
  7088. "aliquando id"
  7089. ```
  7090. ## Phase 1: block structure
  7091. Each line that is processed has an effect on this tree. The line is
  7092. analyzed and, depending on its contents, the document may be altered
  7093. in one or more of the following ways:
  7094. 1. One or more open blocks may be closed.
  7095. 2. One or more new blocks may be created as children of the
  7096. last open block.
  7097. 3. Text may be added to the last (deepest) open block remaining
  7098. on the tree.
  7099. Once a line has been incorporated into the tree in this way,
  7100. it can be discarded, so input can be read in a stream.
  7101. For each line, we follow this procedure:
  7102. 1. First we iterate through the open blocks, starting with the
  7103. root document, and descending through last children down to the last
  7104. open block. Each block imposes a condition that the line must satisfy
  7105. if the block is to remain open. For example, a block quote requires a
  7106. `>` character. A paragraph requires a non-blank line.
  7107. In this phase we may match all or just some of the open
  7108. blocks. But we cannot close unmatched blocks yet, because we may have a
  7109. [lazy continuation line].
  7110. 2. Next, after consuming the continuation markers for existing
  7111. blocks, we look for new block starts (e.g. `>` for a block quote).
  7112. If we encounter a new block start, we close any blocks unmatched
  7113. in step 1 before creating the new block as a child of the last
  7114. matched container block.
  7115. 3. Finally, we look at the remainder of the line (after block
  7116. markers like `>`, list markers, and indentation have been consumed).
  7117. This is text that can be incorporated into the last open
  7118. block (a paragraph, code block, heading, or raw HTML).
  7119. Setext headings are formed when we see a line of a paragraph
  7120. that is a [setext heading underline].
  7121. Reference link definitions are detected when a paragraph is closed;
  7122. the accumulated text lines are parsed to see if they begin with
  7123. one or more reference link definitions. Any remainder becomes a
  7124. normal paragraph.
  7125. We can see how this works by considering how the tree above is
  7126. generated by four lines of Markdown:
  7127. ``` markdown
  7128. > Lorem ipsum dolor
  7129. sit amet.
  7130. > - Qui *quodsi iracundia*
  7131. > - aliquando id
  7132. ```
  7133. At the outset, our document model is just
  7134. ``` tree
  7135. -> document
  7136. ```
  7137. The first line of our text,
  7138. ``` markdown
  7139. > Lorem ipsum dolor
  7140. ```
  7141. causes a `block_quote` block to be created as a child of our
  7142. open `document` block, and a `paragraph` block as a child of
  7143. the `block_quote`. Then the text is added to the last open
  7144. block, the `paragraph`:
  7145. ``` tree
  7146. -> document
  7147. -> block_quote
  7148. -> paragraph
  7149. "Lorem ipsum dolor"
  7150. ```
  7151. The next line,
  7152. ``` markdown
  7153. sit amet.
  7154. ```
  7155. is a "lazy continuation" of the open `paragraph`, so it gets added
  7156. to the paragraph's text:
  7157. ``` tree
  7158. -> document
  7159. -> block_quote
  7160. -> paragraph
  7161. "Lorem ipsum dolor\nsit amet."
  7162. ```
  7163. The third line,
  7164. ``` markdown
  7165. > - Qui *quodsi iracundia*
  7166. ```
  7167. causes the `paragraph` block to be closed, and a new `list` block
  7168. opened as a child of the `block_quote`. A `list_item` is also
  7169. added as a child of the `list`, and a `paragraph` as a child of
  7170. the `list_item`. The text is then added to the new `paragraph`:
  7171. ``` tree
  7172. -> document
  7173. -> block_quote
  7174. paragraph
  7175. "Lorem ipsum dolor\nsit amet."
  7176. -> list (type=bullet tight=true bullet_char=-)
  7177. -> list_item
  7178. -> paragraph
  7179. "Qui *quodsi iracundia*"
  7180. ```
  7181. The fourth line,
  7182. ``` markdown
  7183. > - aliquando id
  7184. ```
  7185. causes the `list_item` (and its child the `paragraph`) to be closed,
  7186. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7187. is added as a child of the new `list_item`, to contain the text.
  7188. We thus obtain the final tree:
  7189. ``` tree
  7190. -> document
  7191. -> block_quote
  7192. paragraph
  7193. "Lorem ipsum dolor\nsit amet."
  7194. -> list (type=bullet tight=true bullet_char=-)
  7195. list_item
  7196. paragraph
  7197. "Qui *quodsi iracundia*"
  7198. -> list_item
  7199. -> paragraph
  7200. "aliquando id"
  7201. ```
  7202. ## Phase 2: inline structure
  7203. Once all of the input has been parsed, all open blocks are closed.
  7204. We then "walk the tree," visiting every node, and parse raw
  7205. string contents of paragraphs and headings as inlines. At this
  7206. point we have seen all the link reference definitions, so we can
  7207. resolve reference links as we go.
  7208. ``` tree
  7209. document
  7210. block_quote
  7211. paragraph
  7212. str "Lorem ipsum dolor"
  7213. softbreak
  7214. str "sit amet."
  7215. list (type=bullet tight=true bullet_char=-)
  7216. list_item
  7217. paragraph
  7218. str "Qui "
  7219. emph
  7220. str "quodsi iracundia"
  7221. list_item
  7222. paragraph
  7223. str "aliquando id"
  7224. ```
  7225. Notice how the [line ending] in the first paragraph has
  7226. been parsed as a `softbreak`, and the asterisks in the first list item
  7227. have become an `emph`.
  7228. ### An algorithm for parsing nested emphasis and links
  7229. By far the trickiest part of inline parsing is handling emphasis,
  7230. strong emphasis, links, and images. This is done using the following
  7231. algorithm.
  7232. When we're parsing inlines and we hit either
  7233. - a run of `*` or `_` characters, or
  7234. - a `[` or `![`
  7235. we insert a text node with these symbols as its literal content, and we
  7236. add a pointer to this text node to the [delimiter stack](@).
  7237. The [delimiter stack] is a doubly linked list. Each
  7238. element contains a pointer to a text node, plus information about
  7239. - the type of delimiter (`[`, `![`, `*`, `_`)
  7240. - the number of delimiters,
  7241. - whether the delimiter is "active" (all are active to start), and
  7242. - whether the delimiter is a potential opener, a potential closer,
  7243. or both (which depends on what sort of characters precede
  7244. and follow the delimiters).
  7245. When we hit a `]` character, we call the *look for link or image*
  7246. procedure (see below).
  7247. When we hit the end of the input, we call the *process emphasis*
  7248. procedure (see below), with `stack_bottom` = NULL.
  7249. #### *look for link or image*
  7250. Starting at the top of the delimiter stack, we look backwards
  7251. through the stack for an opening `[` or `![` delimiter.
  7252. - If we don't find one, we return a literal text node `]`.
  7253. - If we do find one, but it's not *active*, we remove the inactive
  7254. delimiter from the stack, and return a literal text node `]`.
  7255. - If we find one and it's active, then we parse ahead to see if
  7256. we have an inline link/image, reference link/image, compact reference
  7257. link/image, or shortcut reference link/image.
  7258. + If we don't, then we remove the opening delimiter from the
  7259. delimiter stack and return a literal text node `]`.
  7260. + If we do, then
  7261. * We return a link or image node whose children are the inlines
  7262. after the text node pointed to by the opening delimiter.
  7263. * We run *process emphasis* on these inlines, with the `[` opener
  7264. as `stack_bottom`.
  7265. * We remove the opening delimiter.
  7266. * If we have a link (and not an image), we also set all
  7267. `[` delimiters before the opening delimiter to *inactive*. (This
  7268. will prevent us from getting links within links.)
  7269. #### *process emphasis*
  7270. Parameter `stack_bottom` sets a lower bound to how far we
  7271. descend in the [delimiter stack]. If it is NULL, we can
  7272. go all the way to the bottom. Otherwise, we stop before
  7273. visiting `stack_bottom`.
  7274. Let `current_position` point to the element on the [delimiter stack]
  7275. just above `stack_bottom` (or the first element if `stack_bottom`
  7276. is NULL).
  7277. We keep track of the `openers_bottom` for each delimiter
  7278. type (`*`, `_`) and each length of the closing delimiter run
  7279. (modulo 3). Initialize this to `stack_bottom`.
  7280. Then we repeat the following until we run out of potential
  7281. closers:
  7282. - Move `current_position` forward in the delimiter stack (if needed)
  7283. until we find the first potential closer with delimiter `*` or `_`.
  7284. (This will be the potential closer closest
  7285. to the beginning of the input -- the first one in parse order.)
  7286. - Now, look back in the stack (staying above `stack_bottom` and
  7287. the `openers_bottom` for this delimiter type) for the
  7288. first matching potential opener ("matching" means same delimiter).
  7289. - If one is found:
  7290. + Figure out whether we have emphasis or strong emphasis:
  7291. if both closer and opener spans have length >= 2, we have
  7292. strong, otherwise regular.
  7293. + Insert an emph or strong emph node accordingly, after
  7294. the text node corresponding to the opener.
  7295. + Remove any delimiters between the opener and closer from
  7296. the delimiter stack.
  7297. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7298. from the opening and closing text nodes. If they become empty
  7299. as a result, remove them and remove the corresponding element
  7300. of the delimiter stack. If the closing node is removed, reset
  7301. `current_position` to the next element in the stack.
  7302. - If none is found:
  7303. + Set `openers_bottom` to the element before `current_position`.
  7304. (We know that there are no openers for this kind of closer up to and
  7305. including this point, so this puts a lower bound on future searches.)
  7306. + If the closer at `current_position` is not a potential opener,
  7307. remove it from the delimiter stack (since we know it can't
  7308. be a closer either).
  7309. + Advance `current_position` to the next element in the stack.
  7310. After we're done, we remove all delimiters above `stack_bottom` from the
  7311. delimiter stack.