aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 31e1714dd822c32008cfdbb2ece174dccbe53fdc (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. .
  4305. <p>*not emphasized*
  4306. &lt;br/&gt; not a tag
  4307. [not a link](/foo)
  4308. `not code`
  4309. 1. not a list
  4310. * not a list
  4311. # not a heading
  4312. [foo]: /url &quot;not a reference&quot;</p>
  4313. ````````````````````````````````
  4314. If a backslash is itself escaped, the following character is not:
  4315. ```````````````````````````````` example
  4316. \\*emphasis*
  4317. .
  4318. <p>\<em>emphasis</em></p>
  4319. ````````````````````````````````
  4320. A backslash at the end of the line is a [hard line break]:
  4321. ```````````````````````````````` example
  4322. foo\
  4323. bar
  4324. .
  4325. <p>foo<br />
  4326. bar</p>
  4327. ````````````````````````````````
  4328. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4329. raw HTML:
  4330. ```````````````````````````````` example
  4331. `` \[\` ``
  4332. .
  4333. <p><code>\[\`</code></p>
  4334. ````````````````````````````````
  4335. ```````````````````````````````` example
  4336. \[\]
  4337. .
  4338. <pre><code>\[\]
  4339. </code></pre>
  4340. ````````````````````````````````
  4341. ```````````````````````````````` example
  4342. ~~~
  4343. \[\]
  4344. ~~~
  4345. .
  4346. <pre><code>\[\]
  4347. </code></pre>
  4348. ````````````````````````````````
  4349. ```````````````````````````````` example
  4350. <http://example.com?find=\*>
  4351. .
  4352. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4353. ````````````````````````````````
  4354. ```````````````````````````````` example
  4355. <a href="/bar\/)">
  4356. .
  4357. <a href="/bar\/)">
  4358. ````````````````````````````````
  4359. But they work in all other contexts, including URLs and link titles,
  4360. link references, and [info strings] in [fenced code blocks]:
  4361. ```````````````````````````````` example
  4362. [foo](/bar\* "ti\*tle")
  4363. .
  4364. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4365. ````````````````````````````````
  4366. ```````````````````````````````` example
  4367. [foo]
  4368. [foo]: /bar\* "ti\*tle"
  4369. .
  4370. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4371. ````````````````````````````````
  4372. ```````````````````````````````` example
  4373. ``` foo\+bar
  4374. foo
  4375. ```
  4376. .
  4377. <pre><code class="language-foo+bar">foo
  4378. </code></pre>
  4379. ````````````````````````````````
  4380. ## Entity and numeric character references
  4381. Valid HTML entity references and numeric character references
  4382. can be used in place of the corresponding Unicode character,
  4383. with the following exceptions:
  4384. - Entity and character references are not recognized in code
  4385. blocks and code spans.
  4386. - Entity and character references cannot stand in place of
  4387. special characters that define structural elements in
  4388. CommonMark. For example, although `&#42;` can be used
  4389. in place of a literal `*` character, `&#42;` cannot replace
  4390. `*` in emphasis delimiters, bullet list markers, or thematic
  4391. breaks.
  4392. Conforming CommonMark parsers need not store information about
  4393. whether a particular character was represented in the source
  4394. using a Unicode character or an entity reference.
  4395. [Entity references](@) consist of `&` + any of the valid
  4396. HTML5 entity names + `;`. The
  4397. document <https://html.spec.whatwg.org/multipage/entities.json>
  4398. is used as an authoritative source for the valid entity
  4399. references and their corresponding code points.
  4400. ```````````````````````````````` example
  4401. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4402. &frac34; &HilbertSpace; &DifferentialD;
  4403. &ClockwiseContourIntegral; &ngE;
  4404. .
  4405. <p>  &amp; © Æ Ď
  4406. ¾ ℋ ⅆ
  4407. ∲ ≧̸</p>
  4408. ````````````````````````````````
  4409. [Decimal numeric character
  4410. references](@)
  4411. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4412. numeric character reference is parsed as the corresponding
  4413. Unicode character. Invalid Unicode code points will be replaced by
  4414. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4415. the code point `U+0000` will also be replaced by `U+FFFD`.
  4416. ```````````````````````````````` example
  4417. &#35; &#1234; &#992; &#0;
  4418. .
  4419. <p># Ӓ Ϡ �</p>
  4420. ````````````````````````````````
  4421. [Hexadecimal numeric character
  4422. references](@) consist of `&#` +
  4423. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4424. They too are parsed as the corresponding Unicode character (this
  4425. time specified with a hexadecimal numeral instead of decimal).
  4426. ```````````````````````````````` example
  4427. &#X22; &#XD06; &#xcab;
  4428. .
  4429. <p>&quot; ആ ಫ</p>
  4430. ````````````````````````````````
  4431. Here are some nonentities:
  4432. ```````````````````````````````` example
  4433. &nbsp &x; &#; &#x;
  4434. &#987654321;
  4435. &#abcdef0;
  4436. &ThisIsNotDefined; &hi?;
  4437. .
  4438. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4439. &amp;#987654321;
  4440. &amp;#abcdef0;
  4441. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4442. ````````````````````````````````
  4443. Although HTML5 does accept some entity references
  4444. without a trailing semicolon (such as `&copy`), these are not
  4445. recognized here, because it makes the grammar too ambiguous:
  4446. ```````````````````````````````` example
  4447. &copy
  4448. .
  4449. <p>&amp;copy</p>
  4450. ````````````````````````````````
  4451. Strings that are not on the list of HTML5 named entities are not
  4452. recognized as entity references either:
  4453. ```````````````````````````````` example
  4454. &MadeUpEntity;
  4455. .
  4456. <p>&amp;MadeUpEntity;</p>
  4457. ````````````````````````````````
  4458. Entity and numeric character references are recognized in any
  4459. context besides code spans or code blocks, including
  4460. URLs, [link titles], and [fenced code block][] [info strings]:
  4461. ```````````````````````````````` example
  4462. <a href="&ouml;&ouml;.html">
  4463. .
  4464. <a href="&ouml;&ouml;.html">
  4465. ````````````````````````````````
  4466. ```````````````````````````````` example
  4467. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4468. .
  4469. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4470. ````````````````````````````````
  4471. ```````````````````````````````` example
  4472. [foo]
  4473. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4474. .
  4475. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4476. ````````````````````````````````
  4477. ```````````````````````````````` example
  4478. ``` f&ouml;&ouml;
  4479. foo
  4480. ```
  4481. .
  4482. <pre><code class="language-föö">foo
  4483. </code></pre>
  4484. ````````````````````````````````
  4485. Entity and numeric character references are treated as literal
  4486. text in code spans and code blocks:
  4487. ```````````````````````````````` example
  4488. `f&ouml;&ouml;`
  4489. .
  4490. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4491. ````````````````````````````````
  4492. ```````````````````````````````` example
  4493. f&ouml;f&ouml;
  4494. .
  4495. <pre><code>f&amp;ouml;f&amp;ouml;
  4496. </code></pre>
  4497. ````````````````````````````````
  4498. Entity and numeric character references cannot be used
  4499. in place of symbols indicating structure in CommonMark
  4500. documents.
  4501. ```````````````````````````````` example
  4502. &#42;foo&#42;
  4503. *foo*
  4504. .
  4505. <p>*foo*
  4506. <em>foo</em></p>
  4507. ````````````````````````````````
  4508. ```````````````````````````````` example
  4509. &#42; foo
  4510. * foo
  4511. .
  4512. <p>* foo</p>
  4513. <ul>
  4514. <li>foo</li>
  4515. </ul>
  4516. ````````````````````````````````
  4517. ```````````````````````````````` example
  4518. foo&#10;&#10;bar
  4519. .
  4520. <p>foo
  4521. bar</p>
  4522. ````````````````````````````````
  4523. ```````````````````````````````` example
  4524. &#9;foo
  4525. .
  4526. <p>→foo</p>
  4527. ````````````````````````````````
  4528. ```````````````````````````````` example
  4529. [a](url &quot;tit&quot;)
  4530. .
  4531. <p>[a](url &quot;tit&quot;)</p>
  4532. ````````````````````````````````
  4533. ## Code spans
  4534. A [backtick string](@)
  4535. is a string of one or more backtick characters (`` ` ``) that is neither
  4536. preceded nor followed by a backtick.
  4537. A [code span](@) begins with a backtick string and ends with
  4538. a backtick string of equal length. The contents of the code span are
  4539. the characters between the two backtick strings, normalized in the
  4540. following ways:
  4541. - First, [line endings] are converted to [spaces].
  4542. - If the resulting string both begins *and* ends with a [space]
  4543. character, but does not consist entirely of [space]
  4544. characters, a single [space] character is removed from the
  4545. front and back. This allows you to include code that begins
  4546. or ends with backtick characters, which must be separated by
  4547. whitespace from the opening or closing backtick strings.
  4548. This is a simple code span:
  4549. ```````````````````````````````` example
  4550. `foo`
  4551. .
  4552. <p><code>foo</code></p>
  4553. ````````````````````````````````
  4554. Here two backticks are used, because the code contains a backtick.
  4555. This example also illustrates stripping of a single leading and
  4556. trailing space:
  4557. ```````````````````````````````` example
  4558. `` foo ` bar ``
  4559. .
  4560. <p><code>foo ` bar</code></p>
  4561. ````````````````````````````````
  4562. This example shows the motivation for stripping leading and trailing
  4563. spaces:
  4564. ```````````````````````````````` example
  4565. ` `` `
  4566. .
  4567. <p><code>``</code></p>
  4568. ````````````````````````````````
  4569. Note that only *one* space is stripped:
  4570. ```````````````````````````````` example
  4571. ` `` `
  4572. .
  4573. <p><code> `` </code></p>
  4574. ````````````````````````````````
  4575. The stripping only happens if the space is on both
  4576. sides of the string:
  4577. ```````````````````````````````` example
  4578. ` a`
  4579. .
  4580. <p><code> a</code></p>
  4581. ````````````````````````````````
  4582. Only [spaces], and not [unicode whitespace] in general, are
  4583. stripped in this way:
  4584. ```````````````````````````````` example
  4585. ` b `
  4586. .
  4587. <p><code> b </code></p>
  4588. ````````````````````````````````
  4589. No stripping occurs if the code span contains only spaces:
  4590. ```````````````````````````````` example
  4591. ` `
  4592. ` `
  4593. .
  4594. <p><code> </code>
  4595. <code> </code></p>
  4596. ````````````````````````````````
  4597. [Line endings] are treated like spaces:
  4598. ```````````````````````````````` example
  4599. ``
  4600. foo
  4601. bar
  4602. baz
  4603. ``
  4604. .
  4605. <p><code>foo bar baz</code></p>
  4606. ````````````````````````````````
  4607. ```````````````````````````````` example
  4608. ``
  4609. foo
  4610. ``
  4611. .
  4612. <p><code>foo </code></p>
  4613. ````````````````````````````````
  4614. Interior spaces are not collapsed:
  4615. ```````````````````````````````` example
  4616. `foo bar
  4617. baz`
  4618. .
  4619. <p><code>foo bar baz</code></p>
  4620. ````````````````````````````````
  4621. Note that browsers will typically collapse consecutive spaces
  4622. when rendering `<code>` elements, so it is recommended that
  4623. the following CSS be used:
  4624. code{white-space: pre-wrap;}
  4625. Note that backslash escapes do not work in code spans. All backslashes
  4626. are treated literally:
  4627. ```````````````````````````````` example
  4628. `foo\`bar`
  4629. .
  4630. <p><code>foo\</code>bar`</p>
  4631. ````````````````````````````````
  4632. Backslash escapes are never needed, because one can always choose a
  4633. string of *n* backtick characters as delimiters, where the code does
  4634. not contain any strings of exactly *n* backtick characters.
  4635. ```````````````````````````````` example
  4636. ``foo`bar``
  4637. .
  4638. <p><code>foo`bar</code></p>
  4639. ````````````````````````````````
  4640. ```````````````````````````````` example
  4641. ` foo `` bar `
  4642. .
  4643. <p><code>foo `` bar</code></p>
  4644. ````````````````````````````````
  4645. Code span backticks have higher precedence than any other inline
  4646. constructs except HTML tags and autolinks. Thus, for example, this is
  4647. not parsed as emphasized text, since the second `*` is part of a code
  4648. span:
  4649. ```````````````````````````````` example
  4650. *foo`*`
  4651. .
  4652. <p>*foo<code>*</code></p>
  4653. ````````````````````````````````
  4654. And this is not parsed as a link:
  4655. ```````````````````````````````` example
  4656. [not a `link](/foo`)
  4657. .
  4658. <p>[not a <code>link](/foo</code>)</p>
  4659. ````````````````````````````````
  4660. Code spans, HTML tags, and autolinks have the same precedence.
  4661. Thus, this is code:
  4662. ```````````````````````````````` example
  4663. `<a href="`">`
  4664. .
  4665. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4666. ````````````````````````````````
  4667. But this is an HTML tag:
  4668. ```````````````````````````````` example
  4669. <a href="`">`
  4670. .
  4671. <p><a href="`">`</p>
  4672. ````````````````````````````````
  4673. And this is code:
  4674. ```````````````````````````````` example
  4675. `<http://foo.bar.`baz>`
  4676. .
  4677. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4678. ````````````````````````````````
  4679. But this is an autolink:
  4680. ```````````````````````````````` example
  4681. <http://foo.bar.`baz>`
  4682. .
  4683. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4684. ````````````````````````````````
  4685. When a backtick string is not closed by a matching backtick string,
  4686. we just have literal backticks:
  4687. ```````````````````````````````` example
  4688. ```foo``
  4689. .
  4690. <p>```foo``</p>
  4691. ````````````````````````````````
  4692. ```````````````````````````````` example
  4693. `foo
  4694. .
  4695. <p>`foo</p>
  4696. ````````````````````````````````
  4697. The following case also illustrates the need for opening and
  4698. closing backtick strings to be equal in length:
  4699. ```````````````````````````````` example
  4700. `foo``bar``
  4701. .
  4702. <p>`foo<code>bar</code></p>
  4703. ````````````````````````````````
  4704. ## Emphasis and strong emphasis
  4705. John Gruber's original [Markdown syntax
  4706. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4707. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4708. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4709. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4710. > tag.
  4711. This is enough for most users, but these rules leave much undecided,
  4712. especially when it comes to nested emphasis. The original
  4713. `Markdown.pl` test suite makes it clear that triple `***` and
  4714. `___` delimiters can be used for strong emphasis, and most
  4715. implementations have also allowed the following patterns:
  4716. ``` markdown
  4717. ***strong emph***
  4718. ***strong** in emph*
  4719. ***emph* in strong**
  4720. **in strong *emph***
  4721. *in emph **strong***
  4722. ```
  4723. The following patterns are less widely supported, but the intent
  4724. is clear and they are useful (especially in contexts like bibliography
  4725. entries):
  4726. ``` markdown
  4727. *emph *with emph* in it*
  4728. **strong **with strong** in it**
  4729. ```
  4730. Many implementations have also restricted intraword emphasis to
  4731. the `*` forms, to avoid unwanted emphasis in words containing
  4732. internal underscores. (It is best practice to put these in code
  4733. spans, but users often do not.)
  4734. ``` markdown
  4735. internal emphasis: foo*bar*baz
  4736. no emphasis: foo_bar_baz
  4737. ```
  4738. The rules given below capture all of these patterns, while allowing
  4739. for efficient parsing strategies that do not backtrack.
  4740. First, some definitions. A [delimiter run](@) is either
  4741. a sequence of one or more `*` characters that is not preceded or
  4742. followed by a non-backslash-escaped `*` character, or a sequence
  4743. of one or more `_` characters that is not preceded or followed by
  4744. a non-backslash-escaped `_` character.
  4745. A [left-flanking delimiter run](@) is
  4746. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4747. and either (2a) not followed by a [punctuation character], or
  4748. (2b) followed by a [punctuation character] and
  4749. preceded by [Unicode whitespace] or a [punctuation character].
  4750. For purposes of this definition, the beginning and the end of
  4751. the line count as Unicode whitespace.
  4752. A [right-flanking delimiter run](@) is
  4753. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4754. and either (2a) not preceded by a [punctuation character], or
  4755. (2b) preceded by a [punctuation character] and
  4756. followed by [Unicode whitespace] or a [punctuation character].
  4757. For purposes of this definition, the beginning and the end of
  4758. the line count as Unicode whitespace.
  4759. Here are some examples of delimiter runs.
  4760. - left-flanking but not right-flanking:
  4761. ```
  4762. ***abc
  4763. _abc
  4764. **"abc"
  4765. _"abc"
  4766. ```
  4767. - right-flanking but not left-flanking:
  4768. ```
  4769. abc***
  4770. abc_
  4771. "abc"**
  4772. "abc"_
  4773. ```
  4774. - Both left and right-flanking:
  4775. ```
  4776. abc***def
  4777. "abc"_"def"
  4778. ```
  4779. - Neither left nor right-flanking:
  4780. ```
  4781. abc *** def
  4782. a _ b
  4783. ```
  4784. (The idea of distinguishing left-flanking and right-flanking
  4785. delimiter runs based on the character before and the character
  4786. after comes from Roopesh Chander's
  4787. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4788. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4789. run," and its rules for distinguishing left- and right-flanking runs
  4790. are a bit more complex than the ones given here.)
  4791. The following rules define emphasis and strong emphasis:
  4792. 1. A single `*` character [can open emphasis](@)
  4793. iff (if and only if) it is part of a [left-flanking delimiter run].
  4794. 2. A single `_` character [can open emphasis] iff
  4795. it is part of a [left-flanking delimiter run]
  4796. and either (a) not part of a [right-flanking delimiter run]
  4797. or (b) part of a [right-flanking delimiter run]
  4798. preceded by punctuation.
  4799. 3. A single `*` character [can close emphasis](@)
  4800. iff it is part of a [right-flanking delimiter run].
  4801. 4. A single `_` character [can close emphasis] iff
  4802. it is part of a [right-flanking delimiter run]
  4803. and either (a) not part of a [left-flanking delimiter run]
  4804. or (b) part of a [left-flanking delimiter run]
  4805. followed by punctuation.
  4806. 5. A double `**` [can open strong emphasis](@)
  4807. iff it is part of a [left-flanking delimiter run].
  4808. 6. A double `__` [can open strong emphasis] iff
  4809. it is part of a [left-flanking delimiter run]
  4810. and either (a) not part of a [right-flanking delimiter run]
  4811. or (b) part of a [right-flanking delimiter run]
  4812. preceded by punctuation.
  4813. 7. A double `**` [can close strong emphasis](@)
  4814. iff it is part of a [right-flanking delimiter run].
  4815. 8. A double `__` [can close strong emphasis] iff
  4816. it is part of a [right-flanking delimiter run]
  4817. and either (a) not part of a [left-flanking delimiter run]
  4818. or (b) part of a [left-flanking delimiter run]
  4819. followed by punctuation.
  4820. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4821. with a delimiter that [can close emphasis], and that uses the same
  4822. character (`_` or `*`) as the opening delimiter. The
  4823. opening and closing delimiters must belong to separate
  4824. [delimiter runs]. If one of the delimiters can both
  4825. open and close emphasis, then the sum of the lengths of the
  4826. delimiter runs containing the opening and closing delimiters
  4827. must not be a multiple of 3 unless both lengths are
  4828. multiples of 3.
  4829. 10. Strong emphasis begins with a delimiter that
  4830. [can open strong emphasis] and ends with a delimiter that
  4831. [can close strong emphasis], and that uses the same character
  4832. (`_` or `*`) as the opening delimiter. The
  4833. opening and closing delimiters must belong to separate
  4834. [delimiter runs]. If one of the delimiters can both open
  4835. and close strong emphasis, then the sum of the lengths of
  4836. the delimiter runs containing the opening and closing
  4837. delimiters must not be a multiple of 3 unless both lengths
  4838. are multiples of 3.
  4839. 11. A literal `*` character cannot occur at the beginning or end of
  4840. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4841. is backslash-escaped.
  4842. 12. A literal `_` character cannot occur at the beginning or end of
  4843. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4844. is backslash-escaped.
  4845. Where rules 1--12 above are compatible with multiple parsings,
  4846. the following principles resolve ambiguity:
  4847. 13. The number of nestings should be minimized. Thus, for example,
  4848. an interpretation `<strong>...</strong>` is always preferred to
  4849. `<em><em>...</em></em>`.
  4850. 14. An interpretation `<em><strong>...</strong></em>` is always
  4851. preferred to `<strong><em>...</em></strong>`.
  4852. 15. When two potential emphasis or strong emphasis spans overlap,
  4853. so that the second begins before the first ends and ends after
  4854. the first ends, the first takes precedence. Thus, for example,
  4855. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4856. than `*foo <em>bar* baz</em>`.
  4857. 16. When there are two potential emphasis or strong emphasis spans
  4858. with the same closing delimiter, the shorter one (the one that
  4859. opens later) takes precedence. Thus, for example,
  4860. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4861. rather than `<strong>foo **bar baz</strong>`.
  4862. 17. Inline code spans, links, images, and HTML tags group more tightly
  4863. than emphasis. So, when there is a choice between an interpretation
  4864. that contains one of these elements and one that does not, the
  4865. former always wins. Thus, for example, `*[foo*](bar)` is
  4866. parsed as `*<a href="bar">foo*</a>` rather than as
  4867. `<em>[foo</em>](bar)`.
  4868. These rules can be illustrated through a series of examples.
  4869. Rule 1:
  4870. ```````````````````````````````` example
  4871. *foo bar*
  4872. .
  4873. <p><em>foo bar</em></p>
  4874. ````````````````````````````````
  4875. This is not emphasis, because the opening `*` is followed by
  4876. whitespace, and hence not part of a [left-flanking delimiter run]:
  4877. ```````````````````````````````` example
  4878. a * foo bar*
  4879. .
  4880. <p>a * foo bar*</p>
  4881. ````````````````````````````````
  4882. This is not emphasis, because the opening `*` is preceded
  4883. by an alphanumeric and followed by punctuation, and hence
  4884. not part of a [left-flanking delimiter run]:
  4885. ```````````````````````````````` example
  4886. a*"foo"*
  4887. .
  4888. <p>a*&quot;foo&quot;*</p>
  4889. ````````````````````````````````
  4890. Unicode nonbreaking spaces count as whitespace, too:
  4891. ```````````````````````````````` example
  4892. * a *
  4893. .
  4894. <p>* a *</p>
  4895. ````````````````````````````````
  4896. Intraword emphasis with `*` is permitted:
  4897. ```````````````````````````````` example
  4898. foo*bar*
  4899. .
  4900. <p>foo<em>bar</em></p>
  4901. ````````````````````````````````
  4902. ```````````````````````````````` example
  4903. 5*6*78
  4904. .
  4905. <p>5<em>6</em>78</p>
  4906. ````````````````````````````````
  4907. Rule 2:
  4908. ```````````````````````````````` example
  4909. _foo bar_
  4910. .
  4911. <p><em>foo bar</em></p>
  4912. ````````````````````````````````
  4913. This is not emphasis, because the opening `_` is followed by
  4914. whitespace:
  4915. ```````````````````````````````` example
  4916. _ foo bar_
  4917. .
  4918. <p>_ foo bar_</p>
  4919. ````````````````````````````````
  4920. This is not emphasis, because the opening `_` is preceded
  4921. by an alphanumeric and followed by punctuation:
  4922. ```````````````````````````````` example
  4923. a_"foo"_
  4924. .
  4925. <p>a_&quot;foo&quot;_</p>
  4926. ````````````````````````````````
  4927. Emphasis with `_` is not allowed inside words:
  4928. ```````````````````````````````` example
  4929. foo_bar_
  4930. .
  4931. <p>foo_bar_</p>
  4932. ````````````````````````````````
  4933. ```````````````````````````````` example
  4934. 5_6_78
  4935. .
  4936. <p>5_6_78</p>
  4937. ````````````````````````````````
  4938. ```````````````````````````````` example
  4939. пристаням_стремятся_
  4940. .
  4941. <p>пристаням_стремятся_</p>
  4942. ````````````````````````````````
  4943. Here `_` does not generate emphasis, because the first delimiter run
  4944. is right-flanking and the second left-flanking:
  4945. ```````````````````````````````` example
  4946. aa_"bb"_cc
  4947. .
  4948. <p>aa_&quot;bb&quot;_cc</p>
  4949. ````````````````````````````````
  4950. This is emphasis, even though the opening delimiter is
  4951. both left- and right-flanking, because it is preceded by
  4952. punctuation:
  4953. ```````````````````````````````` example
  4954. foo-_(bar)_
  4955. .
  4956. <p>foo-<em>(bar)</em></p>
  4957. ````````````````````````````````
  4958. Rule 3:
  4959. This is not emphasis, because the closing delimiter does
  4960. not match the opening delimiter:
  4961. ```````````````````````````````` example
  4962. _foo*
  4963. .
  4964. <p>_foo*</p>
  4965. ````````````````````````````````
  4966. This is not emphasis, because the closing `*` is preceded by
  4967. whitespace:
  4968. ```````````````````````````````` example
  4969. *foo bar *
  4970. .
  4971. <p>*foo bar *</p>
  4972. ````````````````````````````````
  4973. A newline also counts as whitespace:
  4974. ```````````````````````````````` example
  4975. *foo bar
  4976. *
  4977. .
  4978. <p>*foo bar
  4979. *</p>
  4980. ````````````````````````````````
  4981. This is not emphasis, because the second `*` is
  4982. preceded by punctuation and followed by an alphanumeric
  4983. (hence it is not part of a [right-flanking delimiter run]:
  4984. ```````````````````````````````` example
  4985. *(*foo)
  4986. .
  4987. <p>*(*foo)</p>
  4988. ````````````````````````````````
  4989. The point of this restriction is more easily appreciated
  4990. with this example:
  4991. ```````````````````````````````` example
  4992. *(*foo*)*
  4993. .
  4994. <p><em>(<em>foo</em>)</em></p>
  4995. ````````````````````````````````
  4996. Intraword emphasis with `*` is allowed:
  4997. ```````````````````````````````` example
  4998. *foo*bar
  4999. .
  5000. <p><em>foo</em>bar</p>
  5001. ````````````````````````````````
  5002. Rule 4:
  5003. This is not emphasis, because the closing `_` is preceded by
  5004. whitespace:
  5005. ```````````````````````````````` example
  5006. _foo bar _
  5007. .
  5008. <p>_foo bar _</p>
  5009. ````````````````````````````````
  5010. This is not emphasis, because the second `_` is
  5011. preceded by punctuation and followed by an alphanumeric:
  5012. ```````````````````````````````` example
  5013. _(_foo)
  5014. .
  5015. <p>_(_foo)</p>
  5016. ````````````````````````````````
  5017. This is emphasis within emphasis:
  5018. ```````````````````````````````` example
  5019. _(_foo_)_
  5020. .
  5021. <p><em>(<em>foo</em>)</em></p>
  5022. ````````````````````````````````
  5023. Intraword emphasis is disallowed for `_`:
  5024. ```````````````````````````````` example
  5025. _foo_bar
  5026. .
  5027. <p>_foo_bar</p>
  5028. ````````````````````````````````
  5029. ```````````````````````````````` example
  5030. _пристаням_стремятся
  5031. .
  5032. <p>_пристаням_стремятся</p>
  5033. ````````````````````````````````
  5034. ```````````````````````````````` example
  5035. _foo_bar_baz_
  5036. .
  5037. <p><em>foo_bar_baz</em></p>
  5038. ````````````````````````````````
  5039. This is emphasis, even though the closing delimiter is
  5040. both left- and right-flanking, because it is followed by
  5041. punctuation:
  5042. ```````````````````````````````` example
  5043. _(bar)_.
  5044. .
  5045. <p><em>(bar)</em>.</p>
  5046. ````````````````````````````````
  5047. Rule 5:
  5048. ```````````````````````````````` example
  5049. **foo bar**
  5050. .
  5051. <p><strong>foo bar</strong></p>
  5052. ````````````````````````````````
  5053. This is not strong emphasis, because the opening delimiter is
  5054. followed by whitespace:
  5055. ```````````````````````````````` example
  5056. ** foo bar**
  5057. .
  5058. <p>** foo bar**</p>
  5059. ````````````````````````````````
  5060. This is not strong emphasis, because the opening `**` is preceded
  5061. by an alphanumeric and followed by punctuation, and hence
  5062. not part of a [left-flanking delimiter run]:
  5063. ```````````````````````````````` example
  5064. a**"foo"**
  5065. .
  5066. <p>a**&quot;foo&quot;**</p>
  5067. ````````````````````````````````
  5068. Intraword strong emphasis with `**` is permitted:
  5069. ```````````````````````````````` example
  5070. foo**bar**
  5071. .
  5072. <p>foo<strong>bar</strong></p>
  5073. ````````````````````````````````
  5074. Rule 6:
  5075. ```````````````````````````````` example
  5076. __foo bar__
  5077. .
  5078. <p><strong>foo bar</strong></p>
  5079. ````````````````````````````````
  5080. This is not strong emphasis, because the opening delimiter is
  5081. followed by whitespace:
  5082. ```````````````````````````````` example
  5083. __ foo bar__
  5084. .
  5085. <p>__ foo bar__</p>
  5086. ````````````````````````````````
  5087. A newline counts as whitespace:
  5088. ```````````````````````````````` example
  5089. __
  5090. foo bar__
  5091. .
  5092. <p>__
  5093. foo bar__</p>
  5094. ````````````````````````````````
  5095. This is not strong emphasis, because the opening `__` is preceded
  5096. by an alphanumeric and followed by punctuation:
  5097. ```````````````````````````````` example
  5098. a__"foo"__
  5099. .
  5100. <p>a__&quot;foo&quot;__</p>
  5101. ````````````````````````````````
  5102. Intraword strong emphasis is forbidden with `__`:
  5103. ```````````````````````````````` example
  5104. foo__bar__
  5105. .
  5106. <p>foo__bar__</p>
  5107. ````````````````````````````````
  5108. ```````````````````````````````` example
  5109. 5__6__78
  5110. .
  5111. <p>5__6__78</p>
  5112. ````````````````````````````````
  5113. ```````````````````````````````` example
  5114. пристаням__стремятся__
  5115. .
  5116. <p>пристаням__стремятся__</p>
  5117. ````````````````````````````````
  5118. ```````````````````````````````` example
  5119. __foo, __bar__, baz__
  5120. .
  5121. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5122. ````````````````````````````````
  5123. This is strong emphasis, even though the opening delimiter is
  5124. both left- and right-flanking, because it is preceded by
  5125. punctuation:
  5126. ```````````````````````````````` example
  5127. foo-__(bar)__
  5128. .
  5129. <p>foo-<strong>(bar)</strong></p>
  5130. ````````````````````````````````
  5131. Rule 7:
  5132. This is not strong emphasis, because the closing delimiter is preceded
  5133. by whitespace:
  5134. ```````````````````````````````` example
  5135. **foo bar **
  5136. .
  5137. <p>**foo bar **</p>
  5138. ````````````````````````````````
  5139. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5140. Rule 11.)
  5141. This is not strong emphasis, because the second `**` is
  5142. preceded by punctuation and followed by an alphanumeric:
  5143. ```````````````````````````````` example
  5144. **(**foo)
  5145. .
  5146. <p>**(**foo)</p>
  5147. ````````````````````````````````
  5148. The point of this restriction is more easily appreciated
  5149. with these examples:
  5150. ```````````````````````````````` example
  5151. *(**foo**)*
  5152. .
  5153. <p><em>(<strong>foo</strong>)</em></p>
  5154. ````````````````````````````````
  5155. ```````````````````````````````` example
  5156. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5157. *Asclepias physocarpa*)**
  5158. .
  5159. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5160. <em>Asclepias physocarpa</em>)</strong></p>
  5161. ````````````````````````````````
  5162. ```````````````````````````````` example
  5163. **foo "*bar*" foo**
  5164. .
  5165. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5166. ````````````````````````````````
  5167. Intraword emphasis:
  5168. ```````````````````````````````` example
  5169. **foo**bar
  5170. .
  5171. <p><strong>foo</strong>bar</p>
  5172. ````````````````````````````````
  5173. Rule 8:
  5174. This is not strong emphasis, because the closing delimiter is
  5175. preceded by whitespace:
  5176. ```````````````````````````````` example
  5177. __foo bar __
  5178. .
  5179. <p>__foo bar __</p>
  5180. ````````````````````````````````
  5181. This is not strong emphasis, because the second `__` is
  5182. preceded by punctuation and followed by an alphanumeric:
  5183. ```````````````````````````````` example
  5184. __(__foo)
  5185. .
  5186. <p>__(__foo)</p>
  5187. ````````````````````````````````
  5188. The point of this restriction is more easily appreciated
  5189. with this example:
  5190. ```````````````````````````````` example
  5191. _(__foo__)_
  5192. .
  5193. <p><em>(<strong>foo</strong>)</em></p>
  5194. ````````````````````````````````
  5195. Intraword strong emphasis is forbidden with `__`:
  5196. ```````````````````````````````` example
  5197. __foo__bar
  5198. .
  5199. <p>__foo__bar</p>
  5200. ````````````````````````````````
  5201. ```````````````````````````````` example
  5202. __пристаням__стремятся
  5203. .
  5204. <p>__пристаням__стремятся</p>
  5205. ````````````````````````````````
  5206. ```````````````````````````````` example
  5207. __foo__bar__baz__
  5208. .
  5209. <p><strong>foo__bar__baz</strong></p>
  5210. ````````````````````````````````
  5211. This is strong emphasis, even though the closing delimiter is
  5212. both left- and right-flanking, because it is followed by
  5213. punctuation:
  5214. ```````````````````````````````` example
  5215. __(bar)__.
  5216. .
  5217. <p><strong>(bar)</strong>.</p>
  5218. ````````````````````````````````
  5219. Rule 9:
  5220. Any nonempty sequence of inline elements can be the contents of an
  5221. emphasized span.
  5222. ```````````````````````````````` example
  5223. *foo [bar](/url)*
  5224. .
  5225. <p><em>foo <a href="/url">bar</a></em></p>
  5226. ````````````````````````````````
  5227. ```````````````````````````````` example
  5228. *foo
  5229. bar*
  5230. .
  5231. <p><em>foo
  5232. bar</em></p>
  5233. ````````````````````````````````
  5234. In particular, emphasis and strong emphasis can be nested
  5235. inside emphasis:
  5236. ```````````````````````````````` example
  5237. _foo __bar__ baz_
  5238. .
  5239. <p><em>foo <strong>bar</strong> baz</em></p>
  5240. ````````````````````````````````
  5241. ```````````````````````````````` example
  5242. _foo _bar_ baz_
  5243. .
  5244. <p><em>foo <em>bar</em> baz</em></p>
  5245. ````````````````````````````````
  5246. ```````````````````````````````` example
  5247. __foo_ bar_
  5248. .
  5249. <p><em><em>foo</em> bar</em></p>
  5250. ````````````````````````````````
  5251. ```````````````````````````````` example
  5252. *foo *bar**
  5253. .
  5254. <p><em>foo <em>bar</em></em></p>
  5255. ````````````````````````````````
  5256. ```````````````````````````````` example
  5257. *foo **bar** baz*
  5258. .
  5259. <p><em>foo <strong>bar</strong> baz</em></p>
  5260. ````````````````````````````````
  5261. ```````````````````````````````` example
  5262. *foo**bar**baz*
  5263. .
  5264. <p><em>foo<strong>bar</strong>baz</em></p>
  5265. ````````````````````````````````
  5266. Note that in the preceding case, the interpretation
  5267. ``` markdown
  5268. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5269. ```
  5270. is precluded by the condition that a delimiter that
  5271. can both open and close (like the `*` after `foo`)
  5272. cannot form emphasis if the sum of the lengths of
  5273. the delimiter runs containing the opening and
  5274. closing delimiters is a multiple of 3 unless
  5275. both lengths are multiples of 3.
  5276. For the same reason, we don't get two consecutive
  5277. emphasis sections in this example:
  5278. ```````````````````````````````` example
  5279. *foo**bar*
  5280. .
  5281. <p><em>foo**bar</em></p>
  5282. ````````````````````````````````
  5283. The same condition ensures that the following
  5284. cases are all strong emphasis nested inside
  5285. emphasis, even when the interior spaces are
  5286. omitted:
  5287. ```````````````````````````````` example
  5288. ***foo** bar*
  5289. .
  5290. <p><em><strong>foo</strong> bar</em></p>
  5291. ````````````````````````````````
  5292. ```````````````````````````````` example
  5293. *foo **bar***
  5294. .
  5295. <p><em>foo <strong>bar</strong></em></p>
  5296. ````````````````````````````````
  5297. ```````````````````````````````` example
  5298. *foo**bar***
  5299. .
  5300. <p><em>foo<strong>bar</strong></em></p>
  5301. ````````````````````````````````
  5302. When the lengths of the interior closing and opening
  5303. delimiter runs are *both* multiples of 3, though,
  5304. they can match to create emphasis:
  5305. ```````````````````````````````` example
  5306. foo***bar***baz
  5307. .
  5308. <p>foo<em><strong>bar</strong></em>baz</p>
  5309. ````````````````````````````````
  5310. ```````````````````````````````` example
  5311. foo******bar*********baz
  5312. .
  5313. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5314. ````````````````````````````````
  5315. Indefinite levels of nesting are possible:
  5316. ```````````````````````````````` example
  5317. *foo **bar *baz* bim** bop*
  5318. .
  5319. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5320. ````````````````````````````````
  5321. ```````````````````````````````` example
  5322. *foo [*bar*](/url)*
  5323. .
  5324. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5325. ````````````````````````````````
  5326. There can be no empty emphasis or strong emphasis:
  5327. ```````````````````````````````` example
  5328. ** is not an empty emphasis
  5329. .
  5330. <p>** is not an empty emphasis</p>
  5331. ````````````````````````````````
  5332. ```````````````````````````````` example
  5333. **** is not an empty strong emphasis
  5334. .
  5335. <p>**** is not an empty strong emphasis</p>
  5336. ````````````````````````````````
  5337. Rule 10:
  5338. Any nonempty sequence of inline elements can be the contents of an
  5339. strongly emphasized span.
  5340. ```````````````````````````````` example
  5341. **foo [bar](/url)**
  5342. .
  5343. <p><strong>foo <a href="/url">bar</a></strong></p>
  5344. ````````````````````````````````
  5345. ```````````````````````````````` example
  5346. **foo
  5347. bar**
  5348. .
  5349. <p><strong>foo
  5350. bar</strong></p>
  5351. ````````````````````````````````
  5352. In particular, emphasis and strong emphasis can be nested
  5353. inside strong emphasis:
  5354. ```````````````````````````````` example
  5355. __foo _bar_ baz__
  5356. .
  5357. <p><strong>foo <em>bar</em> baz</strong></p>
  5358. ````````````````````````````````
  5359. ```````````````````````````````` example
  5360. __foo __bar__ baz__
  5361. .
  5362. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5363. ````````````````````````````````
  5364. ```````````````````````````````` example
  5365. ____foo__ bar__
  5366. .
  5367. <p><strong><strong>foo</strong> bar</strong></p>
  5368. ````````````````````````````````
  5369. ```````````````````````````````` example
  5370. **foo **bar****
  5371. .
  5372. <p><strong>foo <strong>bar</strong></strong></p>
  5373. ````````````````````````````````
  5374. ```````````````````````````````` example
  5375. **foo *bar* baz**
  5376. .
  5377. <p><strong>foo <em>bar</em> baz</strong></p>
  5378. ````````````````````````````````
  5379. ```````````````````````````````` example
  5380. **foo*bar*baz**
  5381. .
  5382. <p><strong>foo<em>bar</em>baz</strong></p>
  5383. ````````````````````````````````
  5384. ```````````````````````````````` example
  5385. ***foo* bar**
  5386. .
  5387. <p><strong><em>foo</em> bar</strong></p>
  5388. ````````````````````````````````
  5389. ```````````````````````````````` example
  5390. **foo *bar***
  5391. .
  5392. <p><strong>foo <em>bar</em></strong></p>
  5393. ````````````````````````````````
  5394. Indefinite levels of nesting are possible:
  5395. ```````````````````````````````` example
  5396. **foo *bar **baz**
  5397. bim* bop**
  5398. .
  5399. <p><strong>foo <em>bar <strong>baz</strong>
  5400. bim</em> bop</strong></p>
  5401. ````````````````````````````````
  5402. ```````````````````````````````` example
  5403. **foo [*bar*](/url)**
  5404. .
  5405. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5406. ````````````````````````````````
  5407. There can be no empty emphasis or strong emphasis:
  5408. ```````````````````````````````` example
  5409. __ is not an empty emphasis
  5410. .
  5411. <p>__ is not an empty emphasis</p>
  5412. ````````````````````````````````
  5413. ```````````````````````````````` example
  5414. ____ is not an empty strong emphasis
  5415. .
  5416. <p>____ is not an empty strong emphasis</p>
  5417. ````````````````````````````````
  5418. Rule 11:
  5419. ```````````````````````````````` example
  5420. foo ***
  5421. .
  5422. <p>foo ***</p>
  5423. ````````````````````````````````
  5424. ```````````````````````````````` example
  5425. foo *\**
  5426. .
  5427. <p>foo <em>*</em></p>
  5428. ````````````````````````````````
  5429. ```````````````````````````````` example
  5430. foo *_*
  5431. .
  5432. <p>foo <em>_</em></p>
  5433. ````````````````````````````````
  5434. ```````````````````````````````` example
  5435. foo *****
  5436. .
  5437. <p>foo *****</p>
  5438. ````````````````````````````````
  5439. ```````````````````````````````` example
  5440. foo **\***
  5441. .
  5442. <p>foo <strong>*</strong></p>
  5443. ````````````````````````````````
  5444. ```````````````````````````````` example
  5445. foo **_**
  5446. .
  5447. <p>foo <strong>_</strong></p>
  5448. ````````````````````````````````
  5449. Note that when delimiters do not match evenly, Rule 11 determines
  5450. that the excess literal `*` characters will appear outside of the
  5451. emphasis, rather than inside it:
  5452. ```````````````````````````````` example
  5453. **foo*
  5454. .
  5455. <p>*<em>foo</em></p>
  5456. ````````````````````````````````
  5457. ```````````````````````````````` example
  5458. *foo**
  5459. .
  5460. <p><em>foo</em>*</p>
  5461. ````````````````````````````````
  5462. ```````````````````````````````` example
  5463. ***foo**
  5464. .
  5465. <p>*<strong>foo</strong></p>
  5466. ````````````````````````````````
  5467. ```````````````````````````````` example
  5468. ****foo*
  5469. .
  5470. <p>***<em>foo</em></p>
  5471. ````````````````````````````````
  5472. ```````````````````````````````` example
  5473. **foo***
  5474. .
  5475. <p><strong>foo</strong>*</p>
  5476. ````````````````````````````````
  5477. ```````````````````````````````` example
  5478. *foo****
  5479. .
  5480. <p><em>foo</em>***</p>
  5481. ````````````````````````````````
  5482. Rule 12:
  5483. ```````````````````````````````` example
  5484. foo ___
  5485. .
  5486. <p>foo ___</p>
  5487. ````````````````````````````````
  5488. ```````````````````````````````` example
  5489. foo _\__
  5490. .
  5491. <p>foo <em>_</em></p>
  5492. ````````````````````````````````
  5493. ```````````````````````````````` example
  5494. foo _*_
  5495. .
  5496. <p>foo <em>*</em></p>
  5497. ````````````````````````````````
  5498. ```````````````````````````````` example
  5499. foo _____
  5500. .
  5501. <p>foo _____</p>
  5502. ````````````````````````````````
  5503. ```````````````````````````````` example
  5504. foo __\___
  5505. .
  5506. <p>foo <strong>_</strong></p>
  5507. ````````````````````````````````
  5508. ```````````````````````````````` example
  5509. foo __*__
  5510. .
  5511. <p>foo <strong>*</strong></p>
  5512. ````````````````````````````````
  5513. ```````````````````````````````` example
  5514. __foo_
  5515. .
  5516. <p>_<em>foo</em></p>
  5517. ````````````````````````````````
  5518. Note that when delimiters do not match evenly, Rule 12 determines
  5519. that the excess literal `_` characters will appear outside of the
  5520. emphasis, rather than inside it:
  5521. ```````````````````````````````` example
  5522. _foo__
  5523. .
  5524. <p><em>foo</em>_</p>
  5525. ````````````````````````````````
  5526. ```````````````````````````````` example
  5527. ___foo__
  5528. .
  5529. <p>_<strong>foo</strong></p>
  5530. ````````````````````````````````
  5531. ```````````````````````````````` example
  5532. ____foo_
  5533. .
  5534. <p>___<em>foo</em></p>
  5535. ````````````````````````````````
  5536. ```````````````````````````````` example
  5537. __foo___
  5538. .
  5539. <p><strong>foo</strong>_</p>
  5540. ````````````````````````````````
  5541. ```````````````````````````````` example
  5542. _foo____
  5543. .
  5544. <p><em>foo</em>___</p>
  5545. ````````````````````````````````
  5546. Rule 13 implies that if you want emphasis nested directly inside
  5547. emphasis, you must use different delimiters:
  5548. ```````````````````````````````` example
  5549. **foo**
  5550. .
  5551. <p><strong>foo</strong></p>
  5552. ````````````````````````````````
  5553. ```````````````````````````````` example
  5554. *_foo_*
  5555. .
  5556. <p><em><em>foo</em></em></p>
  5557. ````````````````````````````````
  5558. ```````````````````````````````` example
  5559. __foo__
  5560. .
  5561. <p><strong>foo</strong></p>
  5562. ````````````````````````````````
  5563. ```````````````````````````````` example
  5564. _*foo*_
  5565. .
  5566. <p><em><em>foo</em></em></p>
  5567. ````````````````````````````````
  5568. However, strong emphasis within strong emphasis is possible without
  5569. switching delimiters:
  5570. ```````````````````````````````` example
  5571. ****foo****
  5572. .
  5573. <p><strong><strong>foo</strong></strong></p>
  5574. ````````````````````````````````
  5575. ```````````````````````````````` example
  5576. ____foo____
  5577. .
  5578. <p><strong><strong>foo</strong></strong></p>
  5579. ````````````````````````````````
  5580. Rule 13 can be applied to arbitrarily long sequences of
  5581. delimiters:
  5582. ```````````````````````````````` example
  5583. ******foo******
  5584. .
  5585. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5586. ````````````````````````````````
  5587. Rule 14:
  5588. ```````````````````````````````` example
  5589. ***foo***
  5590. .
  5591. <p><em><strong>foo</strong></em></p>
  5592. ````````````````````````````````
  5593. ```````````````````````````````` example
  5594. _____foo_____
  5595. .
  5596. <p><em><strong><strong>foo</strong></strong></em></p>
  5597. ````````````````````````````````
  5598. Rule 15:
  5599. ```````````````````````````````` example
  5600. *foo _bar* baz_
  5601. .
  5602. <p><em>foo _bar</em> baz_</p>
  5603. ````````````````````````````````
  5604. ```````````````````````````````` example
  5605. *foo __bar *baz bim__ bam*
  5606. .
  5607. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5608. ````````````````````````````````
  5609. Rule 16:
  5610. ```````````````````````````````` example
  5611. **foo **bar baz**
  5612. .
  5613. <p>**foo <strong>bar baz</strong></p>
  5614. ````````````````````````````````
  5615. ```````````````````````````````` example
  5616. *foo *bar baz*
  5617. .
  5618. <p>*foo <em>bar baz</em></p>
  5619. ````````````````````````````````
  5620. Rule 17:
  5621. ```````````````````````````````` example
  5622. *[bar*](/url)
  5623. .
  5624. <p>*<a href="/url">bar*</a></p>
  5625. ````````````````````````````````
  5626. ```````````````````````````````` example
  5627. _foo [bar_](/url)
  5628. .
  5629. <p>_foo <a href="/url">bar_</a></p>
  5630. ````````````````````````````````
  5631. ```````````````````````````````` example
  5632. *<img src="foo" title="*"/>
  5633. .
  5634. <p>*<img src="foo" title="*"/></p>
  5635. ````````````````````````````````
  5636. ```````````````````````````````` example
  5637. **<a href="**">
  5638. .
  5639. <p>**<a href="**"></p>
  5640. ````````````````````````````````
  5641. ```````````````````````````````` example
  5642. __<a href="__">
  5643. .
  5644. <p>__<a href="__"></p>
  5645. ````````````````````````````````
  5646. ```````````````````````````````` example
  5647. *a `*`*
  5648. .
  5649. <p><em>a <code>*</code></em></p>
  5650. ````````````````````````````````
  5651. ```````````````````````````````` example
  5652. _a `_`_
  5653. .
  5654. <p><em>a <code>_</code></em></p>
  5655. ````````````````````````````````
  5656. ```````````````````````````````` example
  5657. **a<http://foo.bar/?q=**>
  5658. .
  5659. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5660. ````````````````````````````````
  5661. ```````````````````````````````` example
  5662. __a<http://foo.bar/?q=__>
  5663. .
  5664. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5665. ````````````````````````````````
  5666. ## Links
  5667. A link contains [link text] (the visible text), a [link destination]
  5668. (the URI that is the link destination), and optionally a [link title].
  5669. There are two basic kinds of links in Markdown. In [inline links] the
  5670. destination and title are given immediately after the link text. In
  5671. [reference links] the destination and title are defined elsewhere in
  5672. the document.
  5673. A [link text](@) consists of a sequence of zero or more
  5674. inline elements enclosed by square brackets (`[` and `]`). The
  5675. following rules apply:
  5676. - Links may not contain other links, at any level of nesting. If
  5677. multiple otherwise valid link definitions appear nested inside each
  5678. other, the inner-most definition is used.
  5679. - Brackets are allowed in the [link text] only if (a) they
  5680. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5681. with an open bracket `[`, a sequence of zero or more inlines, and
  5682. a close bracket `]`.
  5683. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5684. than the brackets in link text. Thus, for example,
  5685. `` [foo`]` `` could not be a link text, since the second `]`
  5686. is part of a code span.
  5687. - The brackets in link text bind more tightly than markers for
  5688. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5689. A [link destination](@) consists of either
  5690. - a sequence of zero or more characters between an opening `<` and a
  5691. closing `>` that contains no line breaks or unescaped
  5692. `<` or `>` characters, or
  5693. - a nonempty sequence of characters that does not start with
  5694. `<`, does not include ASCII space or control characters, and
  5695. includes parentheses only if (a) they are backslash-escaped or
  5696. (b) they are part of a balanced pair of unescaped parentheses.
  5697. (Implementations may impose limits on parentheses nesting to
  5698. avoid performance issues, but at least three levels of nesting
  5699. should be supported.)
  5700. A [link title](@) consists of either
  5701. - a sequence of zero or more characters between straight double-quote
  5702. characters (`"`), including a `"` character only if it is
  5703. backslash-escaped, or
  5704. - a sequence of zero or more characters between straight single-quote
  5705. characters (`'`), including a `'` character only if it is
  5706. backslash-escaped, or
  5707. - a sequence of zero or more characters between matching parentheses
  5708. (`(...)`), including a `(` or `)` character only if it is
  5709. backslash-escaped.
  5710. Although [link titles] may span multiple lines, they may not contain
  5711. a [blank line].
  5712. An [inline link](@) consists of a [link text] followed immediately
  5713. by a left parenthesis `(`, optional [whitespace], an optional
  5714. [link destination], an optional [link title] separated from the link
  5715. destination by [whitespace], optional [whitespace], and a right
  5716. parenthesis `)`. The link's text consists of the inlines contained
  5717. in the [link text] (excluding the enclosing square brackets).
  5718. The link's URI consists of the link destination, excluding enclosing
  5719. `<...>` if present, with backslash-escapes in effect as described
  5720. above. The link's title consists of the link title, excluding its
  5721. enclosing delimiters, with backslash-escapes in effect as described
  5722. above.
  5723. Here is a simple inline link:
  5724. ```````````````````````````````` example
  5725. [link](/uri "title")
  5726. .
  5727. <p><a href="/uri" title="title">link</a></p>
  5728. ````````````````````````````````
  5729. The title may be omitted:
  5730. ```````````````````````````````` example
  5731. [link](/uri)
  5732. .
  5733. <p><a href="/uri">link</a></p>
  5734. ````````````````````````````````
  5735. Both the title and the destination may be omitted:
  5736. ```````````````````````````````` example
  5737. [link]()
  5738. .
  5739. <p><a href="">link</a></p>
  5740. ````````````````````````````````
  5741. ```````````````````````````````` example
  5742. [link](<>)
  5743. .
  5744. <p><a href="">link</a></p>
  5745. ````````````````````````````````
  5746. The destination can only contain spaces if it is
  5747. enclosed in pointy brackets:
  5748. ```````````````````````````````` example
  5749. [link](/my uri)
  5750. .
  5751. <p>[link](/my uri)</p>
  5752. ````````````````````````````````
  5753. ```````````````````````````````` example
  5754. [link](</my uri>)
  5755. .
  5756. <p><a href="/my%20uri">link</a></p>
  5757. ````````````````````````````````
  5758. The destination cannot contain line breaks,
  5759. even if enclosed in pointy brackets:
  5760. ```````````````````````````````` example
  5761. [link](foo
  5762. bar)
  5763. .
  5764. <p>[link](foo
  5765. bar)</p>
  5766. ````````````````````````````````
  5767. ```````````````````````````````` example
  5768. [link](<foo
  5769. bar>)
  5770. .
  5771. <p>[link](<foo
  5772. bar>)</p>
  5773. ````````````````````````````````
  5774. The destination can contain `)` if it is enclosed
  5775. in pointy brackets:
  5776. ```````````````````````````````` example
  5777. [a](<b)c>)
  5778. .
  5779. <p><a href="b)c">a</a></p>
  5780. ````````````````````````````````
  5781. Pointy brackets that enclose links must be unescaped:
  5782. ```````````````````````````````` example
  5783. [link](<foo\>)
  5784. .
  5785. <p>[link](&lt;foo&gt;)</p>
  5786. ````````````````````````````````
  5787. These are not links, because the opening pointy bracket
  5788. is not matched properly:
  5789. ```````````````````````````````` example
  5790. [a](<b)c
  5791. [a](<b)c>
  5792. [a](<b>c)
  5793. .
  5794. <p>[a](&lt;b)c
  5795. [a](&lt;b)c&gt;
  5796. [a](<b>c)</p>
  5797. ````````````````````````````````
  5798. Parentheses inside the link destination may be escaped:
  5799. ```````````````````````````````` example
  5800. [link](\(foo\))
  5801. .
  5802. <p><a href="(foo)">link</a></p>
  5803. ````````````````````````````````
  5804. Any number of parentheses are allowed without escaping, as long as they are
  5805. balanced:
  5806. ```````````````````````````````` example
  5807. [link](foo(and(bar)))
  5808. .
  5809. <p><a href="foo(and(bar))">link</a></p>
  5810. ````````````````````````````````
  5811. However, if you have unbalanced parentheses, you need to escape or use the
  5812. `<...>` form:
  5813. ```````````````````````````````` example
  5814. [link](foo\(and\(bar\))
  5815. .
  5816. <p><a href="foo(and(bar)">link</a></p>
  5817. ````````````````````````````````
  5818. ```````````````````````````````` example
  5819. [link](<foo(and(bar)>)
  5820. .
  5821. <p><a href="foo(and(bar)">link</a></p>
  5822. ````````````````````````````````
  5823. Parentheses and other symbols can also be escaped, as usual
  5824. in Markdown:
  5825. ```````````````````````````````` example
  5826. [link](foo\)\:)
  5827. .
  5828. <p><a href="foo):">link</a></p>
  5829. ````````````````````````````````
  5830. A link can contain fragment identifiers and queries:
  5831. ```````````````````````````````` example
  5832. [link](#fragment)
  5833. [link](http://example.com#fragment)
  5834. [link](http://example.com?foo=3#frag)
  5835. .
  5836. <p><a href="#fragment">link</a></p>
  5837. <p><a href="http://example.com#fragment">link</a></p>
  5838. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5839. ````````````````````````````````
  5840. Note that a backslash before a non-escapable character is
  5841. just a backslash:
  5842. ```````````````````````````````` example
  5843. [link](foo\bar)
  5844. .
  5845. <p><a href="foo%5Cbar">link</a></p>
  5846. ````````````````````````````````
  5847. URL-escaping should be left alone inside the destination, as all
  5848. URL-escaped characters are also valid URL characters. Entity and
  5849. numerical character references in the destination will be parsed
  5850. into the corresponding Unicode code points, as usual. These may
  5851. be optionally URL-escaped when written as HTML, but this spec
  5852. does not enforce any particular policy for rendering URLs in
  5853. HTML or other formats. Renderers may make different decisions
  5854. about how to escape or normalize URLs in the output.
  5855. ```````````````````````````````` example
  5856. [link](foo%20b&auml;)
  5857. .
  5858. <p><a href="foo%20b%C3%A4">link</a></p>
  5859. ````````````````````````````````
  5860. Note that, because titles can often be parsed as destinations,
  5861. if you try to omit the destination and keep the title, you'll
  5862. get unexpected results:
  5863. ```````````````````````````````` example
  5864. [link]("title")
  5865. .
  5866. <p><a href="%22title%22">link</a></p>
  5867. ````````````````````````````````
  5868. Titles may be in single quotes, double quotes, or parentheses:
  5869. ```````````````````````````````` example
  5870. [link](/url "title")
  5871. [link](/url 'title')
  5872. [link](/url (title))
  5873. .
  5874. <p><a href="/url" title="title">link</a>
  5875. <a href="/url" title="title">link</a>
  5876. <a href="/url" title="title">link</a></p>
  5877. ````````````````````````````````
  5878. Backslash escapes and entity and numeric character references
  5879. may be used in titles:
  5880. ```````````````````````````````` example
  5881. [link](/url "title \"&quot;")
  5882. .
  5883. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5884. ````````````````````````````````
  5885. Titles must be separated from the link using a [whitespace].
  5886. Other [Unicode whitespace] like non-breaking space doesn't work.
  5887. ```````````````````````````````` example
  5888. [link](/url "title")
  5889. .
  5890. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5891. ````````````````````````````````
  5892. Nested balanced quotes are not allowed without escaping:
  5893. ```````````````````````````````` example
  5894. [link](/url "title "and" title")
  5895. .
  5896. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5897. ````````````````````````````````
  5898. But it is easy to work around this by using a different quote type:
  5899. ```````````````````````````````` example
  5900. [link](/url 'title "and" title')
  5901. .
  5902. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5903. ````````````````````````````````
  5904. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5905. title, and its test suite included a test demonstrating this.
  5906. But it is hard to see a good rationale for the extra complexity this
  5907. brings, since there are already many ways---backslash escaping,
  5908. entity and numeric character references, or using a different
  5909. quote type for the enclosing title---to write titles containing
  5910. double quotes. `Markdown.pl`'s handling of titles has a number
  5911. of other strange features. For example, it allows single-quoted
  5912. titles in inline links, but not reference links. And, in
  5913. reference links but not inline links, it allows a title to begin
  5914. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5915. titles with no closing quotation mark, though 1.0.2b8 does not.
  5916. It seems preferable to adopt a simple, rational rule that works
  5917. the same way in inline links and link reference definitions.)
  5918. [Whitespace] is allowed around the destination and title:
  5919. ```````````````````````````````` example
  5920. [link]( /uri
  5921. "title" )
  5922. .
  5923. <p><a href="/uri" title="title">link</a></p>
  5924. ````````````````````````````````
  5925. But it is not allowed between the link text and the
  5926. following parenthesis:
  5927. ```````````````````````````````` example
  5928. [link] (/uri)
  5929. .
  5930. <p>[link] (/uri)</p>
  5931. ````````````````````````````````
  5932. The link text may contain balanced brackets, but not unbalanced ones,
  5933. unless they are escaped:
  5934. ```````````````````````````````` example
  5935. [link [foo [bar]]](/uri)
  5936. .
  5937. <p><a href="/uri">link [foo [bar]]</a></p>
  5938. ````````````````````````````````
  5939. ```````````````````````````````` example
  5940. [link] bar](/uri)
  5941. .
  5942. <p>[link] bar](/uri)</p>
  5943. ````````````````````````````````
  5944. ```````````````````````````````` example
  5945. [link [bar](/uri)
  5946. .
  5947. <p>[link <a href="/uri">bar</a></p>
  5948. ````````````````````````````````
  5949. ```````````````````````````````` example
  5950. [link \[bar](/uri)
  5951. .
  5952. <p><a href="/uri">link [bar</a></p>
  5953. ````````````````````````````````
  5954. The link text may contain inline content:
  5955. ```````````````````````````````` example
  5956. [link *foo **bar** `#`*](/uri)
  5957. .
  5958. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5959. ````````````````````````````````
  5960. ```````````````````````````````` example
  5961. [![moon](moon.jpg)](/uri)
  5962. .
  5963. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5964. ````````````````````````````````
  5965. However, links may not contain other links, at any level of nesting.
  5966. ```````````````````````````````` example
  5967. [foo [bar](/uri)](/uri)
  5968. .
  5969. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5970. ````````````````````````````````
  5971. ```````````````````````````````` example
  5972. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5973. .
  5974. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5975. ````````````````````````````````
  5976. ```````````````````````````````` example
  5977. ![[[foo](uri1)](uri2)](uri3)
  5978. .
  5979. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5980. ````````````````````````````````
  5981. These cases illustrate the precedence of link text grouping over
  5982. emphasis grouping:
  5983. ```````````````````````````````` example
  5984. *[foo*](/uri)
  5985. .
  5986. <p>*<a href="/uri">foo*</a></p>
  5987. ````````````````````````````````
  5988. ```````````````````````````````` example
  5989. [foo *bar](baz*)
  5990. .
  5991. <p><a href="baz*">foo *bar</a></p>
  5992. ````````````````````````````````
  5993. Note that brackets that *aren't* part of links do not take
  5994. precedence:
  5995. ```````````````````````````````` example
  5996. *foo [bar* baz]
  5997. .
  5998. <p><em>foo [bar</em> baz]</p>
  5999. ````````````````````````````````
  6000. These cases illustrate the precedence of HTML tags, code spans,
  6001. and autolinks over link grouping:
  6002. ```````````````````````````````` example
  6003. [foo <bar attr="](baz)">
  6004. .
  6005. <p>[foo <bar attr="](baz)"></p>
  6006. ````````````````````````````````
  6007. ```````````````````````````````` example
  6008. [foo`](/uri)`
  6009. .
  6010. <p>[foo<code>](/uri)</code></p>
  6011. ````````````````````````````````
  6012. ```````````````````````````````` example
  6013. [foo<http://example.com/?search=](uri)>
  6014. .
  6015. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6016. ````````````````````````````````
  6017. There are three kinds of [reference link](@)s:
  6018. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6019. and [shortcut](#shortcut-reference-link).
  6020. A [full reference link](@)
  6021. consists of a [link text] immediately followed by a [link label]
  6022. that [matches] a [link reference definition] elsewhere in the document.
  6023. A [link label](@) begins with a left bracket (`[`) and ends
  6024. with the first right bracket (`]`) that is not backslash-escaped.
  6025. Between these brackets there must be at least one [non-whitespace character].
  6026. Unescaped square bracket characters are not allowed inside the
  6027. opening and closing square brackets of [link labels]. A link
  6028. label can have at most 999 characters inside the square
  6029. brackets.
  6030. One label [matches](@)
  6031. another just in case their normalized forms are equal. To normalize a
  6032. label, strip off the opening and closing brackets,
  6033. perform the *Unicode case fold*, strip leading and trailing
  6034. [whitespace] and collapse consecutive internal
  6035. [whitespace] to a single space. If there are multiple
  6036. matching reference link definitions, the one that comes first in the
  6037. document is used. (It is desirable in such cases to emit a warning.)
  6038. The contents of the first link label are parsed as inlines, which are
  6039. used as the link's text. The link's URI and title are provided by the
  6040. matching [link reference definition].
  6041. Here is a simple example:
  6042. ```````````````````````````````` example
  6043. [foo][bar]
  6044. [bar]: /url "title"
  6045. .
  6046. <p><a href="/url" title="title">foo</a></p>
  6047. ````````````````````````````````
  6048. The rules for the [link text] are the same as with
  6049. [inline links]. Thus:
  6050. The link text may contain balanced brackets, but not unbalanced ones,
  6051. unless they are escaped:
  6052. ```````````````````````````````` example
  6053. [link [foo [bar]]][ref]
  6054. [ref]: /uri
  6055. .
  6056. <p><a href="/uri">link [foo [bar]]</a></p>
  6057. ````````````````````````````````
  6058. ```````````````````````````````` example
  6059. [link \[bar][ref]
  6060. [ref]: /uri
  6061. .
  6062. <p><a href="/uri">link [bar</a></p>
  6063. ````````````````````````````````
  6064. The link text may contain inline content:
  6065. ```````````````````````````````` example
  6066. [link *foo **bar** `#`*][ref]
  6067. [ref]: /uri
  6068. .
  6069. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6070. ````````````````````````````````
  6071. ```````````````````````````````` example
  6072. [![moon](moon.jpg)][ref]
  6073. [ref]: /uri
  6074. .
  6075. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6076. ````````````````````````````````
  6077. However, links may not contain other links, at any level of nesting.
  6078. ```````````````````````````````` example
  6079. [foo [bar](/uri)][ref]
  6080. [ref]: /uri
  6081. .
  6082. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6083. ````````````````````````````````
  6084. ```````````````````````````````` example
  6085. [foo *bar [baz][ref]*][ref]
  6086. [ref]: /uri
  6087. .
  6088. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6089. ````````````````````````````````
  6090. (In the examples above, we have two [shortcut reference links]
  6091. instead of one [full reference link].)
  6092. The following cases illustrate the precedence of link text grouping over
  6093. emphasis grouping:
  6094. ```````````````````````````````` example
  6095. *[foo*][ref]
  6096. [ref]: /uri
  6097. .
  6098. <p>*<a href="/uri">foo*</a></p>
  6099. ````````````````````````````````
  6100. ```````````````````````````````` example
  6101. [foo *bar][ref]
  6102. [ref]: /uri
  6103. .
  6104. <p><a href="/uri">foo *bar</a></p>
  6105. ````````````````````````````````
  6106. These cases illustrate the precedence of HTML tags, code spans,
  6107. and autolinks over link grouping:
  6108. ```````````````````````````````` example
  6109. [foo <bar attr="][ref]">
  6110. [ref]: /uri
  6111. .
  6112. <p>[foo <bar attr="][ref]"></p>
  6113. ````````````````````````````````
  6114. ```````````````````````````````` example
  6115. [foo`][ref]`
  6116. [ref]: /uri
  6117. .
  6118. <p>[foo<code>][ref]</code></p>
  6119. ````````````````````````````````
  6120. ```````````````````````````````` example
  6121. [foo<http://example.com/?search=][ref]>
  6122. [ref]: /uri
  6123. .
  6124. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6125. ````````````````````````````````
  6126. Matching is case-insensitive:
  6127. ```````````````````````````````` example
  6128. [foo][BaR]
  6129. [bar]: /url "title"
  6130. .
  6131. <p><a href="/url" title="title">foo</a></p>
  6132. ````````````````````````````````
  6133. Unicode case fold is used:
  6134. ```````````````````````````````` example
  6135. [Толпой][Толпой] is a Russian word.
  6136. [ТОЛПОЙ]: /url
  6137. .
  6138. <p><a href="/url">Толпой</a> is a Russian word.</p>
  6139. ````````````````````````````````
  6140. Consecutive internal [whitespace] is treated as one space for
  6141. purposes of determining matching:
  6142. ```````````````````````````````` example
  6143. [Foo
  6144. bar]: /url
  6145. [Baz][Foo bar]
  6146. .
  6147. <p><a href="/url">Baz</a></p>
  6148. ````````````````````````````````
  6149. No [whitespace] is allowed between the [link text] and the
  6150. [link label]:
  6151. ```````````````````````````````` example
  6152. [foo] [bar]
  6153. [bar]: /url "title"
  6154. .
  6155. <p>[foo] <a href="/url" title="title">bar</a></p>
  6156. ````````````````````````````````
  6157. ```````````````````````````````` example
  6158. [foo]
  6159. [bar]
  6160. [bar]: /url "title"
  6161. .
  6162. <p>[foo]
  6163. <a href="/url" title="title">bar</a></p>
  6164. ````````````````````````````````
  6165. This is a departure from John Gruber's original Markdown syntax
  6166. description, which explicitly allows whitespace between the link
  6167. text and the link label. It brings reference links in line with
  6168. [inline links], which (according to both original Markdown and
  6169. this spec) cannot have whitespace after the link text. More
  6170. importantly, it prevents inadvertent capture of consecutive
  6171. [shortcut reference links]. If whitespace is allowed between the
  6172. link text and the link label, then in the following we will have
  6173. a single reference link, not two shortcut reference links, as
  6174. intended:
  6175. ``` markdown
  6176. [foo]
  6177. [bar]
  6178. [foo]: /url1
  6179. [bar]: /url2
  6180. ```
  6181. (Note that [shortcut reference links] were introduced by Gruber
  6182. himself in a beta version of `Markdown.pl`, but never included
  6183. in the official syntax description. Without shortcut reference
  6184. links, it is harmless to allow space between the link text and
  6185. link label; but once shortcut references are introduced, it is
  6186. too dangerous to allow this, as it frequently leads to
  6187. unintended results.)
  6188. When there are multiple matching [link reference definitions],
  6189. the first is used:
  6190. ```````````````````````````````` example
  6191. [foo]: /url1
  6192. [foo]: /url2
  6193. [bar][foo]
  6194. .
  6195. <p><a href="/url1">bar</a></p>
  6196. ````````````````````````````````
  6197. Note that matching is performed on normalized strings, not parsed
  6198. inline content. So the following does not match, even though the
  6199. labels define equivalent inline content:
  6200. ```````````````````````````````` example
  6201. [bar][foo\!]
  6202. [foo!]: /url
  6203. .
  6204. <p>[bar][foo!]</p>
  6205. ````````````````````````````````
  6206. [Link labels] cannot contain brackets, unless they are
  6207. backslash-escaped:
  6208. ```````````````````````````````` example
  6209. [foo][ref[]
  6210. [ref[]: /uri
  6211. .
  6212. <p>[foo][ref[]</p>
  6213. <p>[ref[]: /uri</p>
  6214. ````````````````````````````````
  6215. ```````````````````````````````` example
  6216. [foo][ref[bar]]
  6217. [ref[bar]]: /uri
  6218. .
  6219. <p>[foo][ref[bar]]</p>
  6220. <p>[ref[bar]]: /uri</p>
  6221. ````````````````````````````````
  6222. ```````````````````````````````` example
  6223. [[[foo]]]
  6224. [[[foo]]]: /url
  6225. .
  6226. <p>[[[foo]]]</p>
  6227. <p>[[[foo]]]: /url</p>
  6228. ````````````````````````````````
  6229. ```````````````````````````````` example
  6230. [foo][ref\[]
  6231. [ref\[]: /uri
  6232. .
  6233. <p><a href="/uri">foo</a></p>
  6234. ````````````````````````````````
  6235. Note that in this example `]` is not backslash-escaped:
  6236. ```````````````````````````````` example
  6237. [bar\\]: /uri
  6238. [bar\\]
  6239. .
  6240. <p><a href="/uri">bar\</a></p>
  6241. ````````````````````````````````
  6242. A [link label] must contain at least one [non-whitespace character]:
  6243. ```````````````````````````````` example
  6244. []
  6245. []: /uri
  6246. .
  6247. <p>[]</p>
  6248. <p>[]: /uri</p>
  6249. ````````````````````````````````
  6250. ```````````````````````````````` example
  6251. [
  6252. ]
  6253. [
  6254. ]: /uri
  6255. .
  6256. <p>[
  6257. ]</p>
  6258. <p>[
  6259. ]: /uri</p>
  6260. ````````````````````````````````
  6261. A [collapsed reference link](@)
  6262. consists of a [link label] that [matches] a
  6263. [link reference definition] elsewhere in the
  6264. document, followed by the string `[]`.
  6265. The contents of the first link label are parsed as inlines,
  6266. which are used as the link's text. The link's URI and title are
  6267. provided by the matching reference link definition. Thus,
  6268. `[foo][]` is equivalent to `[foo][foo]`.
  6269. ```````````````````````````````` example
  6270. [foo][]
  6271. [foo]: /url "title"
  6272. .
  6273. <p><a href="/url" title="title">foo</a></p>
  6274. ````````````````````````````````
  6275. ```````````````````````````````` example
  6276. [*foo* bar][]
  6277. [*foo* bar]: /url "title"
  6278. .
  6279. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6280. ````````````````````````````````
  6281. The link labels are case-insensitive:
  6282. ```````````````````````````````` example
  6283. [Foo][]
  6284. [foo]: /url "title"
  6285. .
  6286. <p><a href="/url" title="title">Foo</a></p>
  6287. ````````````````````````````````
  6288. As with full reference links, [whitespace] is not
  6289. allowed between the two sets of brackets:
  6290. ```````````````````````````````` example
  6291. [foo]
  6292. []
  6293. [foo]: /url "title"
  6294. .
  6295. <p><a href="/url" title="title">foo</a>
  6296. []</p>
  6297. ````````````````````````````````
  6298. A [shortcut reference link](@)
  6299. consists of a [link label] that [matches] a
  6300. [link reference definition] elsewhere in the
  6301. document and is not followed by `[]` or a link label.
  6302. The contents of the first link label are parsed as inlines,
  6303. which are used as the link's text. The link's URI and title
  6304. are provided by the matching link reference definition.
  6305. Thus, `[foo]` is equivalent to `[foo][]`.
  6306. ```````````````````````````````` example
  6307. [foo]
  6308. [foo]: /url "title"
  6309. .
  6310. <p><a href="/url" title="title">foo</a></p>
  6311. ````````````````````````````````
  6312. ```````````````````````````````` example
  6313. [*foo* bar]
  6314. [*foo* bar]: /url "title"
  6315. .
  6316. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6317. ````````````````````````````````
  6318. ```````````````````````````````` example
  6319. [[*foo* bar]]
  6320. [*foo* bar]: /url "title"
  6321. .
  6322. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6323. ````````````````````````````````
  6324. ```````````````````````````````` example
  6325. [[bar [foo]
  6326. [foo]: /url
  6327. .
  6328. <p>[[bar <a href="/url">foo</a></p>
  6329. ````````````````````````````````
  6330. The link labels are case-insensitive:
  6331. ```````````````````````````````` example
  6332. [Foo]
  6333. [foo]: /url "title"
  6334. .
  6335. <p><a href="/url" title="title">Foo</a></p>
  6336. ````````````````````````````````
  6337. A space after the link text should be preserved:
  6338. ```````````````````````````````` example
  6339. [foo] bar
  6340. [foo]: /url
  6341. .
  6342. <p><a href="/url">foo</a> bar</p>
  6343. ````````````````````````````````
  6344. If you just want bracketed text, you can backslash-escape the
  6345. opening bracket to avoid links:
  6346. ```````````````````````````````` example
  6347. \[foo]
  6348. [foo]: /url "title"
  6349. .
  6350. <p>[foo]</p>
  6351. ````````````````````````````````
  6352. Note that this is a link, because a link label ends with the first
  6353. following closing bracket:
  6354. ```````````````````````````````` example
  6355. [foo*]: /url
  6356. *[foo*]
  6357. .
  6358. <p>*<a href="/url">foo*</a></p>
  6359. ````````````````````````````````
  6360. Full and compact references take precedence over shortcut
  6361. references:
  6362. ```````````````````````````````` example
  6363. [foo][bar]
  6364. [foo]: /url1
  6365. [bar]: /url2
  6366. .
  6367. <p><a href="/url2">foo</a></p>
  6368. ````````````````````````````````
  6369. ```````````````````````````````` example
  6370. [foo][]
  6371. [foo]: /url1
  6372. .
  6373. <p><a href="/url1">foo</a></p>
  6374. ````````````````````````````````
  6375. Inline links also take precedence:
  6376. ```````````````````````````````` example
  6377. [foo]()
  6378. [foo]: /url1
  6379. .
  6380. <p><a href="">foo</a></p>
  6381. ````````````````````````````````
  6382. ```````````````````````````````` example
  6383. [foo](not a link)
  6384. [foo]: /url1
  6385. .
  6386. <p><a href="/url1">foo</a>(not a link)</p>
  6387. ````````````````````````````````
  6388. In the following case `[bar][baz]` is parsed as a reference,
  6389. `[foo]` as normal text:
  6390. ```````````````````````````````` example
  6391. [foo][bar][baz]
  6392. [baz]: /url
  6393. .
  6394. <p>[foo]<a href="/url">bar</a></p>
  6395. ````````````````````````````````
  6396. Here, though, `[foo][bar]` is parsed as a reference, since
  6397. `[bar]` is defined:
  6398. ```````````````````````````````` example
  6399. [foo][bar][baz]
  6400. [baz]: /url1
  6401. [bar]: /url2
  6402. .
  6403. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6404. ````````````````````````````````
  6405. Here `[foo]` is not parsed as a shortcut reference, because it
  6406. is followed by a link label (even though `[bar]` is not defined):
  6407. ```````````````````````````````` example
  6408. [foo][bar][baz]
  6409. [baz]: /url1
  6410. [foo]: /url2
  6411. .
  6412. <p>[foo]<a href="/url1">bar</a></p>
  6413. ````````````````````````````````
  6414. ## Images
  6415. Syntax for images is like the syntax for links, with one
  6416. difference. Instead of [link text], we have an
  6417. [image description](@). The rules for this are the
  6418. same as for [link text], except that (a) an
  6419. image description starts with `![` rather than `[`, and
  6420. (b) an image description may contain links.
  6421. An image description has inline elements
  6422. as its contents. When an image is rendered to HTML,
  6423. this is standardly used as the image's `alt` attribute.
  6424. ```````````````````````````````` example
  6425. ![foo](/url "title")
  6426. .
  6427. <p><img src="/url" alt="foo" title="title" /></p>
  6428. ````````````````````````````````
  6429. ```````````````````````````````` example
  6430. ![foo *bar*]
  6431. [foo *bar*]: train.jpg "train & tracks"
  6432. .
  6433. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6434. ````````````````````````````````
  6435. ```````````````````````````````` example
  6436. ![foo ![bar](/url)](/url2)
  6437. .
  6438. <p><img src="/url2" alt="foo bar" /></p>
  6439. ````````````````````````````````
  6440. ```````````````````````````````` example
  6441. ![foo [bar](/url)](/url2)
  6442. .
  6443. <p><img src="/url2" alt="foo bar" /></p>
  6444. ````````````````````````````````
  6445. Though this spec is concerned with parsing, not rendering, it is
  6446. recommended that in rendering to HTML, only the plain string content
  6447. of the [image description] be used. Note that in
  6448. the above example, the alt attribute's value is `foo bar`, not `foo
  6449. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6450. content is rendered, without formatting.
  6451. ```````````````````````````````` example
  6452. ![foo *bar*][]
  6453. [foo *bar*]: train.jpg "train & tracks"
  6454. .
  6455. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6456. ````````````````````````````````
  6457. ```````````````````````````````` example
  6458. ![foo *bar*][foobar]
  6459. [FOOBAR]: train.jpg "train & tracks"
  6460. .
  6461. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6462. ````````````````````````````````
  6463. ```````````````````````````````` example
  6464. ![foo](train.jpg)
  6465. .
  6466. <p><img src="train.jpg" alt="foo" /></p>
  6467. ````````````````````````````````
  6468. ```````````````````````````````` example
  6469. My ![foo bar](/path/to/train.jpg "title" )
  6470. .
  6471. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6472. ````````````````````````````````
  6473. ```````````````````````````````` example
  6474. ![foo](<url>)
  6475. .
  6476. <p><img src="url" alt="foo" /></p>
  6477. ````````````````````````````````
  6478. ```````````````````````````````` example
  6479. ![](/url)
  6480. .
  6481. <p><img src="/url" alt="" /></p>
  6482. ````````````````````````````````
  6483. Reference-style:
  6484. ```````````````````````````````` example
  6485. ![foo][bar]
  6486. [bar]: /url
  6487. .
  6488. <p><img src="/url" alt="foo" /></p>
  6489. ````````````````````````````````
  6490. ```````````````````````````````` example
  6491. ![foo][bar]
  6492. [BAR]: /url
  6493. .
  6494. <p><img src="/url" alt="foo" /></p>
  6495. ````````````````````````````````
  6496. Collapsed:
  6497. ```````````````````````````````` example
  6498. ![foo][]
  6499. [foo]: /url "title"
  6500. .
  6501. <p><img src="/url" alt="foo" title="title" /></p>
  6502. ````````````````````````````````
  6503. ```````````````````````````````` example
  6504. ![*foo* bar][]
  6505. [*foo* bar]: /url "title"
  6506. .
  6507. <p><img src="/url" alt="foo bar" title="title" /></p>
  6508. ````````````````````````````````
  6509. The labels are case-insensitive:
  6510. ```````````````````````````````` example
  6511. ![Foo][]
  6512. [foo]: /url "title"
  6513. .
  6514. <p><img src="/url" alt="Foo" title="title" /></p>
  6515. ````````````````````````````````
  6516. As with reference links, [whitespace] is not allowed
  6517. between the two sets of brackets:
  6518. ```````````````````````````````` example
  6519. ![foo]
  6520. []
  6521. [foo]: /url "title"
  6522. .
  6523. <p><img src="/url" alt="foo" title="title" />
  6524. []</p>
  6525. ````````````````````````````````
  6526. Shortcut:
  6527. ```````````````````````````````` example
  6528. ![foo]
  6529. [foo]: /url "title"
  6530. .
  6531. <p><img src="/url" alt="foo" title="title" /></p>
  6532. ````````````````````````````````
  6533. ```````````````````````````````` example
  6534. ![*foo* bar]
  6535. [*foo* bar]: /url "title"
  6536. .
  6537. <p><img src="/url" alt="foo bar" title="title" /></p>
  6538. ````````````````````````````````
  6539. Note that link labels cannot contain unescaped brackets:
  6540. ```````````````````````````````` example
  6541. ![[foo]]
  6542. [[foo]]: /url "title"
  6543. .
  6544. <p>![[foo]]</p>
  6545. <p>[[foo]]: /url &quot;title&quot;</p>
  6546. ````````````````````````````````
  6547. The link labels are case-insensitive:
  6548. ```````````````````````````````` example
  6549. ![Foo]
  6550. [foo]: /url "title"
  6551. .
  6552. <p><img src="/url" alt="Foo" title="title" /></p>
  6553. ````````````````````````````````
  6554. If you just want a literal `!` followed by bracketed text, you can
  6555. backslash-escape the opening `[`:
  6556. ```````````````````````````````` example
  6557. !\[foo]
  6558. [foo]: /url "title"
  6559. .
  6560. <p>![foo]</p>
  6561. ````````````````````````````````
  6562. If you want a link after a literal `!`, backslash-escape the
  6563. `!`:
  6564. ```````````````````````````````` example
  6565. \![foo]
  6566. [foo]: /url "title"
  6567. .
  6568. <p>!<a href="/url" title="title">foo</a></p>
  6569. ````````````````````````````````
  6570. ## Autolinks
  6571. [Autolink](@)s are absolute URIs and email addresses inside
  6572. `<` and `>`. They are parsed as links, with the URL or email address
  6573. as the link label.
  6574. A [URI autolink](@) consists of `<`, followed by an
  6575. [absolute URI] followed by `>`. It is parsed as
  6576. a link to the URI, with the URI as the link's label.
  6577. An [absolute URI](@),
  6578. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6579. followed by zero or more characters other than ASCII
  6580. [whitespace] and control characters, `<`, and `>`. If
  6581. the URI includes these characters, they must be percent-encoded
  6582. (e.g. `%20` for a space).
  6583. For purposes of this spec, a [scheme](@) is any sequence
  6584. of 2--32 characters beginning with an ASCII letter and followed
  6585. by any combination of ASCII letters, digits, or the symbols plus
  6586. ("+"), period ("."), or hyphen ("-").
  6587. Here are some valid autolinks:
  6588. ```````````````````````````````` example
  6589. <http://foo.bar.baz>
  6590. .
  6591. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6592. ````````````````````````````````
  6593. ```````````````````````````````` example
  6594. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6595. .
  6596. <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>
  6597. ````````````````````````````````
  6598. ```````````````````````````````` example
  6599. <irc://foo.bar:2233/baz>
  6600. .
  6601. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6602. ````````````````````````````````
  6603. Uppercase is also fine:
  6604. ```````````````````````````````` example
  6605. <MAILTO:FOO@BAR.BAZ>
  6606. .
  6607. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6608. ````````````````````````````````
  6609. Note that many strings that count as [absolute URIs] for
  6610. purposes of this spec are not valid URIs, because their
  6611. schemes are not registered or because of other problems
  6612. with their syntax:
  6613. ```````````````````````````````` example
  6614. <a+b+c:d>
  6615. .
  6616. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6617. ````````````````````````````````
  6618. ```````````````````````````````` example
  6619. <made-up-scheme://foo,bar>
  6620. .
  6621. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6622. ````````````````````````````````
  6623. ```````````````````````````````` example
  6624. <http://../>
  6625. .
  6626. <p><a href="http://../">http://../</a></p>
  6627. ````````````````````````````````
  6628. ```````````````````````````````` example
  6629. <localhost:5001/foo>
  6630. .
  6631. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6632. ````````````````````````````````
  6633. Spaces are not allowed in autolinks:
  6634. ```````````````````````````````` example
  6635. <http://foo.bar/baz bim>
  6636. .
  6637. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6638. ````````````````````````````````
  6639. Backslash-escapes do not work inside autolinks:
  6640. ```````````````````````````````` example
  6641. <http://example.com/\[\>
  6642. .
  6643. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6644. ````````````````````````````````
  6645. An [email autolink](@)
  6646. consists of `<`, followed by an [email address],
  6647. followed by `>`. The link's label is the email address,
  6648. and the URL is `mailto:` followed by the email address.
  6649. An [email address](@),
  6650. for these purposes, is anything that matches
  6651. the [non-normative regex from the HTML5
  6652. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6653. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6654. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6655. Examples of email autolinks:
  6656. ```````````````````````````````` example
  6657. <foo@bar.example.com>
  6658. .
  6659. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6660. ````````````````````````````````
  6661. ```````````````````````````````` example
  6662. <foo+special@Bar.baz-bar0.com>
  6663. .
  6664. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6665. ````````````````````````````````
  6666. Backslash-escapes do not work inside email autolinks:
  6667. ```````````````````````````````` example
  6668. <foo\+@bar.example.com>
  6669. .
  6670. <p>&lt;foo+@bar.example.com&gt;</p>
  6671. ````````````````````````````````
  6672. These are not autolinks:
  6673. ```````````````````````````````` example
  6674. <>
  6675. .
  6676. <p>&lt;&gt;</p>
  6677. ````````````````````````````````
  6678. ```````````````````````````````` example
  6679. < http://foo.bar >
  6680. .
  6681. <p>&lt; http://foo.bar &gt;</p>
  6682. ````````````````````````````````
  6683. ```````````````````````````````` example
  6684. <m:abc>
  6685. .
  6686. <p>&lt;m:abc&gt;</p>
  6687. ````````````````````````````````
  6688. ```````````````````````````````` example
  6689. <foo.bar.baz>
  6690. .
  6691. <p>&lt;foo.bar.baz&gt;</p>
  6692. ````````````````````````````````
  6693. ```````````````````````````````` example
  6694. http://example.com
  6695. .
  6696. <p>http://example.com</p>
  6697. ````````````````````````````````
  6698. ```````````````````````````````` example
  6699. foo@bar.example.com
  6700. .
  6701. <p>foo@bar.example.com</p>
  6702. ````````````````````````````````
  6703. ## Raw HTML
  6704. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6705. raw HTML tag and will be rendered in HTML without escaping.
  6706. Tag and attribute names are not limited to current HTML tags,
  6707. so custom tags (and even, say, DocBook tags) may be used.
  6708. Here is the grammar for tags:
  6709. A [tag name](@) consists of an ASCII letter
  6710. followed by zero or more ASCII letters, digits, or
  6711. hyphens (`-`).
  6712. An [attribute](@) consists of [whitespace],
  6713. an [attribute name], and an optional
  6714. [attribute value specification].
  6715. An [attribute name](@)
  6716. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6717. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6718. specification restricted to ASCII. HTML5 is laxer.)
  6719. An [attribute value specification](@)
  6720. consists of optional [whitespace],
  6721. a `=` character, optional [whitespace], and an [attribute
  6722. value].
  6723. An [attribute value](@)
  6724. consists of an [unquoted attribute value],
  6725. a [single-quoted attribute value], or a [double-quoted attribute value].
  6726. An [unquoted attribute value](@)
  6727. is a nonempty string of characters not
  6728. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6729. A [single-quoted attribute value](@)
  6730. consists of `'`, zero or more
  6731. characters not including `'`, and a final `'`.
  6732. A [double-quoted attribute value](@)
  6733. consists of `"`, zero or more
  6734. characters not including `"`, and a final `"`.
  6735. An [open tag](@) consists of a `<` character, a [tag name],
  6736. zero or more [attributes], optional [whitespace], an optional `/`
  6737. character, and a `>` character.
  6738. A [closing tag](@) consists of the string `</`, a
  6739. [tag name], optional [whitespace], and the character `>`.
  6740. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6741. where *text* does not start with `>` or `->`, does not end with `-`,
  6742. and does not contain `--`. (See the
  6743. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6744. A [processing instruction](@)
  6745. consists of the string `<?`, a string
  6746. of characters not including the string `?>`, and the string
  6747. `?>`.
  6748. A [declaration](@) consists of the
  6749. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6750. [whitespace], a string of characters not including the
  6751. character `>`, and the character `>`.
  6752. A [CDATA section](@) consists of
  6753. the string `<![CDATA[`, a string of characters not including the string
  6754. `]]>`, and the string `]]>`.
  6755. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6756. an [HTML comment], a [processing instruction], a [declaration],
  6757. or a [CDATA section].
  6758. Here are some simple open tags:
  6759. ```````````````````````````````` example
  6760. <a><bab><c2c>
  6761. .
  6762. <p><a><bab><c2c></p>
  6763. ````````````````````````````````
  6764. Empty elements:
  6765. ```````````````````````````````` example
  6766. <a/><b2/>
  6767. .
  6768. <p><a/><b2/></p>
  6769. ````````````````````````````````
  6770. [Whitespace] is allowed:
  6771. ```````````````````````````````` example
  6772. <a /><b2
  6773. data="foo" >
  6774. .
  6775. <p><a /><b2
  6776. data="foo" ></p>
  6777. ````````````````````````````````
  6778. With attributes:
  6779. ```````````````````````````````` example
  6780. <a foo="bar" bam = 'baz <em>"</em>'
  6781. _boolean zoop:33=zoop:33 />
  6782. .
  6783. <p><a foo="bar" bam = 'baz <em>"</em>'
  6784. _boolean zoop:33=zoop:33 /></p>
  6785. ````````````````````````````````
  6786. Custom tag names can be used:
  6787. ```````````````````````````````` example
  6788. Foo <responsive-image src="foo.jpg" />
  6789. .
  6790. <p>Foo <responsive-image src="foo.jpg" /></p>
  6791. ````````````````````````````````
  6792. Illegal tag names, not parsed as HTML:
  6793. ```````````````````````````````` example
  6794. <33> <__>
  6795. .
  6796. <p>&lt;33&gt; &lt;__&gt;</p>
  6797. ````````````````````````````````
  6798. Illegal attribute names:
  6799. ```````````````````````````````` example
  6800. <a h*#ref="hi">
  6801. .
  6802. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6803. ````````````````````````````````
  6804. Illegal attribute values:
  6805. ```````````````````````````````` example
  6806. <a href="hi'> <a href=hi'>
  6807. .
  6808. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6809. ````````````````````````````````
  6810. Illegal [whitespace]:
  6811. ```````````````````````````````` example
  6812. < a><
  6813. foo><bar/ >
  6814. <foo bar=baz
  6815. bim!bop />
  6816. .
  6817. <p>&lt; a&gt;&lt;
  6818. foo&gt;&lt;bar/ &gt;
  6819. &lt;foo bar=baz
  6820. bim!bop /&gt;</p>
  6821. ````````````````````````````````
  6822. Missing [whitespace]:
  6823. ```````````````````````````````` example
  6824. <a href='bar'title=title>
  6825. .
  6826. <p>&lt;a href='bar'title=title&gt;</p>
  6827. ````````````````````````````````
  6828. Closing tags:
  6829. ```````````````````````````````` example
  6830. </a></foo >
  6831. .
  6832. <p></a></foo ></p>
  6833. ````````````````````````````````
  6834. Illegal attributes in closing tag:
  6835. ```````````````````````````````` example
  6836. </a href="foo">
  6837. .
  6838. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6839. ````````````````````````````````
  6840. Comments:
  6841. ```````````````````````````````` example
  6842. foo <!-- this is a
  6843. comment - with hyphen -->
  6844. .
  6845. <p>foo <!-- this is a
  6846. comment - with hyphen --></p>
  6847. ````````````````````````````````
  6848. ```````````````````````````````` example
  6849. foo <!-- not a comment -- two hyphens -->
  6850. .
  6851. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6852. ````````````````````````````````
  6853. Not comments:
  6854. ```````````````````````````````` example
  6855. foo <!--> foo -->
  6856. foo <!-- foo--->
  6857. .
  6858. <p>foo &lt;!--&gt; foo --&gt;</p>
  6859. <p>foo &lt;!-- foo---&gt;</p>
  6860. ````````````````````````````````
  6861. Processing instructions:
  6862. ```````````````````````````````` example
  6863. foo <?php echo $a; ?>
  6864. .
  6865. <p>foo <?php echo $a; ?></p>
  6866. ````````````````````````````````
  6867. Declarations:
  6868. ```````````````````````````````` example
  6869. foo <!ELEMENT br EMPTY>
  6870. .
  6871. <p>foo <!ELEMENT br EMPTY></p>
  6872. ````````````````````````````````
  6873. CDATA sections:
  6874. ```````````````````````````````` example
  6875. foo <![CDATA[>&<]]>
  6876. .
  6877. <p>foo <![CDATA[>&<]]></p>
  6878. ````````````````````````````````
  6879. Entity and numeric character references are preserved in HTML
  6880. attributes:
  6881. ```````````````````````````````` example
  6882. foo <a href="&ouml;">
  6883. .
  6884. <p>foo <a href="&ouml;"></p>
  6885. ````````````````````````````````
  6886. Backslash escapes do not work in HTML attributes:
  6887. ```````````````````````````````` example
  6888. foo <a href="\*">
  6889. .
  6890. <p>foo <a href="\*"></p>
  6891. ````````````````````````````````
  6892. ```````````````````````````````` example
  6893. <a href="\"">
  6894. .
  6895. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6896. ````````````````````````````````
  6897. ## Hard line breaks
  6898. A line break (not in a code span or HTML tag) that is preceded
  6899. by two or more spaces and does not occur at the end of a block
  6900. is parsed as a [hard line break](@) (rendered
  6901. in HTML as a `<br />` tag):
  6902. ```````````````````````````````` example
  6903. foo
  6904. baz
  6905. .
  6906. <p>foo<br />
  6907. baz</p>
  6908. ````````````````````````````````
  6909. For a more visible alternative, a backslash before the
  6910. [line ending] may be used instead of two spaces:
  6911. ```````````````````````````````` example
  6912. foo\
  6913. baz
  6914. .
  6915. <p>foo<br />
  6916. baz</p>
  6917. ````````````````````````````````
  6918. More than two spaces can be used:
  6919. ```````````````````````````````` example
  6920. foo
  6921. baz
  6922. .
  6923. <p>foo<br />
  6924. baz</p>
  6925. ````````````````````````````````
  6926. Leading spaces at the beginning of the next line are ignored:
  6927. ```````````````````````````````` example
  6928. foo
  6929. bar
  6930. .
  6931. <p>foo<br />
  6932. bar</p>
  6933. ````````````````````````````````
  6934. ```````````````````````````````` example
  6935. foo\
  6936. bar
  6937. .
  6938. <p>foo<br />
  6939. bar</p>
  6940. ````````````````````````````````
  6941. Line breaks can occur inside emphasis, links, and other constructs
  6942. that allow inline content:
  6943. ```````````````````````````````` example
  6944. *foo
  6945. bar*
  6946. .
  6947. <p><em>foo<br />
  6948. bar</em></p>
  6949. ````````````````````````````````
  6950. ```````````````````````````````` example
  6951. *foo\
  6952. bar*
  6953. .
  6954. <p><em>foo<br />
  6955. bar</em></p>
  6956. ````````````````````````````````
  6957. Line breaks do not occur inside code spans
  6958. ```````````````````````````````` example
  6959. `code
  6960. span`
  6961. .
  6962. <p><code>code span</code></p>
  6963. ````````````````````````````````
  6964. ```````````````````````````````` example
  6965. `code\
  6966. span`
  6967. .
  6968. <p><code>code\ span</code></p>
  6969. ````````````````````````````````
  6970. or HTML tags:
  6971. ```````````````````````````````` example
  6972. <a href="foo
  6973. bar">
  6974. .
  6975. <p><a href="foo
  6976. bar"></p>
  6977. ````````````````````````````````
  6978. ```````````````````````````````` example
  6979. <a href="foo\
  6980. bar">
  6981. .
  6982. <p><a href="foo\
  6983. bar"></p>
  6984. ````````````````````````````````
  6985. Hard line breaks are for separating inline content within a block.
  6986. Neither syntax for hard line breaks works at the end of a paragraph or
  6987. other block element:
  6988. ```````````````````````````````` example
  6989. foo\
  6990. .
  6991. <p>foo\</p>
  6992. ````````````````````````````````
  6993. ```````````````````````````````` example
  6994. foo
  6995. .
  6996. <p>foo</p>
  6997. ````````````````````````````````
  6998. ```````````````````````````````` example
  6999. ### foo\
  7000. .
  7001. <h3>foo\</h3>
  7002. ````````````````````````````````
  7003. ```````````````````````````````` example
  7004. ### foo
  7005. .
  7006. <h3>foo</h3>
  7007. ````````````````````````````````
  7008. ## Soft line breaks
  7009. A regular line break (not in a code span or HTML tag) that is not
  7010. preceded by two or more spaces or a backslash is parsed as a
  7011. [softbreak](@). (A softbreak may be rendered in HTML either as a
  7012. [line ending] or as a space. The result will be the same in
  7013. browsers. In the examples here, a [line ending] will be used.)
  7014. ```````````````````````````````` example
  7015. foo
  7016. baz
  7017. .
  7018. <p>foo
  7019. baz</p>
  7020. ````````````````````````````````
  7021. Spaces at the end of the line and beginning of the next line are
  7022. removed:
  7023. ```````````````````````````````` example
  7024. foo
  7025. baz
  7026. .
  7027. <p>foo
  7028. baz</p>
  7029. ````````````````````````````````
  7030. A conforming parser may render a soft line break in HTML either as a
  7031. line break or as a space.
  7032. A renderer may also provide an option to render soft line breaks
  7033. as hard line breaks.
  7034. ## Textual content
  7035. Any characters not given an interpretation by the above rules will
  7036. be parsed as plain textual content.
  7037. ```````````````````````````````` example
  7038. hello $.;'there
  7039. .
  7040. <p>hello $.;'there</p>
  7041. ````````````````````````````````
  7042. ```````````````````````````````` example
  7043. Foo χρῆν
  7044. .
  7045. <p>Foo χρῆν</p>
  7046. ````````````````````````````````
  7047. Internal spaces are preserved verbatim:
  7048. ```````````````````````````````` example
  7049. Multiple spaces
  7050. .
  7051. <p>Multiple spaces</p>
  7052. ````````````````````````````````
  7053. <!-- END TESTS -->
  7054. # Appendix: A parsing strategy
  7055. In this appendix we describe some features of the parsing strategy
  7056. used in the CommonMark reference implementations.
  7057. ## Overview
  7058. Parsing has two phases:
  7059. 1. In the first phase, lines of input are consumed and the block
  7060. structure of the document---its division into paragraphs, block quotes,
  7061. list items, and so on---is constructed. Text is assigned to these
  7062. blocks but not parsed. Link reference definitions are parsed and a
  7063. map of links is constructed.
  7064. 2. In the second phase, the raw text contents of paragraphs and headings
  7065. are parsed into sequences of Markdown inline elements (strings,
  7066. code spans, links, emphasis, and so on), using the map of link
  7067. references constructed in phase 1.
  7068. At each point in processing, the document is represented as a tree of
  7069. **blocks**. The root of the tree is a `document` block. The `document`
  7070. may have any number of other blocks as **children**. These children
  7071. may, in turn, have other blocks as children. The last child of a block
  7072. is normally considered **open**, meaning that subsequent lines of input
  7073. can alter its contents. (Blocks that are not open are **closed**.)
  7074. Here, for example, is a possible document tree, with the open blocks
  7075. marked by arrows:
  7076. ``` tree
  7077. -> document
  7078. -> block_quote
  7079. paragraph
  7080. "Lorem ipsum dolor\nsit amet."
  7081. -> list (type=bullet tight=true bullet_char=-)
  7082. list_item
  7083. paragraph
  7084. "Qui *quodsi iracundia*"
  7085. -> list_item
  7086. -> paragraph
  7087. "aliquando id"
  7088. ```
  7089. ## Phase 1: block structure
  7090. Each line that is processed has an effect on this tree. The line is
  7091. analyzed and, depending on its contents, the document may be altered
  7092. in one or more of the following ways:
  7093. 1. One or more open blocks may be closed.
  7094. 2. One or more new blocks may be created as children of the
  7095. last open block.
  7096. 3. Text may be added to the last (deepest) open block remaining
  7097. on the tree.
  7098. Once a line has been incorporated into the tree in this way,
  7099. it can be discarded, so input can be read in a stream.
  7100. For each line, we follow this procedure:
  7101. 1. First we iterate through the open blocks, starting with the
  7102. root document, and descending through last children down to the last
  7103. open block. Each block imposes a condition that the line must satisfy
  7104. if the block is to remain open. For example, a block quote requires a
  7105. `>` character. A paragraph requires a non-blank line.
  7106. In this phase we may match all or just some of the open
  7107. blocks. But we cannot close unmatched blocks yet, because we may have a
  7108. [lazy continuation line].
  7109. 2. Next, after consuming the continuation markers for existing
  7110. blocks, we look for new block starts (e.g. `>` for a block quote).
  7111. If we encounter a new block start, we close any blocks unmatched
  7112. in step 1 before creating the new block as a child of the last
  7113. matched block.
  7114. 3. Finally, we look at the remainder of the line (after block
  7115. markers like `>`, list markers, and indentation have been consumed).
  7116. This is text that can be incorporated into the last open
  7117. block (a paragraph, code block, heading, or raw HTML).
  7118. Setext headings are formed when we see a line of a paragraph
  7119. that is a [setext heading underline].
  7120. Reference link definitions are detected when a paragraph is closed;
  7121. the accumulated text lines are parsed to see if they begin with
  7122. one or more reference link definitions. Any remainder becomes a
  7123. normal paragraph.
  7124. We can see how this works by considering how the tree above is
  7125. generated by four lines of Markdown:
  7126. ``` markdown
  7127. > Lorem ipsum dolor
  7128. sit amet.
  7129. > - Qui *quodsi iracundia*
  7130. > - aliquando id
  7131. ```
  7132. At the outset, our document model is just
  7133. ``` tree
  7134. -> document
  7135. ```
  7136. The first line of our text,
  7137. ``` markdown
  7138. > Lorem ipsum dolor
  7139. ```
  7140. causes a `block_quote` block to be created as a child of our
  7141. open `document` block, and a `paragraph` block as a child of
  7142. the `block_quote`. Then the text is added to the last open
  7143. block, the `paragraph`:
  7144. ``` tree
  7145. -> document
  7146. -> block_quote
  7147. -> paragraph
  7148. "Lorem ipsum dolor"
  7149. ```
  7150. The next line,
  7151. ``` markdown
  7152. sit amet.
  7153. ```
  7154. is a "lazy continuation" of the open `paragraph`, so it gets added
  7155. to the paragraph's text:
  7156. ``` tree
  7157. -> document
  7158. -> block_quote
  7159. -> paragraph
  7160. "Lorem ipsum dolor\nsit amet."
  7161. ```
  7162. The third line,
  7163. ``` markdown
  7164. > - Qui *quodsi iracundia*
  7165. ```
  7166. causes the `paragraph` block to be closed, and a new `list` block
  7167. opened as a child of the `block_quote`. A `list_item` is also
  7168. added as a child of the `list`, and a `paragraph` as a child of
  7169. the `list_item`. The text is then added to the new `paragraph`:
  7170. ``` tree
  7171. -> document
  7172. -> block_quote
  7173. paragraph
  7174. "Lorem ipsum dolor\nsit amet."
  7175. -> list (type=bullet tight=true bullet_char=-)
  7176. -> list_item
  7177. -> paragraph
  7178. "Qui *quodsi iracundia*"
  7179. ```
  7180. The fourth line,
  7181. ``` markdown
  7182. > - aliquando id
  7183. ```
  7184. causes the `list_item` (and its child the `paragraph`) to be closed,
  7185. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7186. is added as a child of the new `list_item`, to contain the text.
  7187. We thus obtain the final tree:
  7188. ``` tree
  7189. -> document
  7190. -> block_quote
  7191. paragraph
  7192. "Lorem ipsum dolor\nsit amet."
  7193. -> list (type=bullet tight=true bullet_char=-)
  7194. list_item
  7195. paragraph
  7196. "Qui *quodsi iracundia*"
  7197. -> list_item
  7198. -> paragraph
  7199. "aliquando id"
  7200. ```
  7201. ## Phase 2: inline structure
  7202. Once all of the input has been parsed, all open blocks are closed.
  7203. We then "walk the tree," visiting every node, and parse raw
  7204. string contents of paragraphs and headings as inlines. At this
  7205. point we have seen all the link reference definitions, so we can
  7206. resolve reference links as we go.
  7207. ``` tree
  7208. document
  7209. block_quote
  7210. paragraph
  7211. str "Lorem ipsum dolor"
  7212. softbreak
  7213. str "sit amet."
  7214. list (type=bullet tight=true bullet_char=-)
  7215. list_item
  7216. paragraph
  7217. str "Qui "
  7218. emph
  7219. str "quodsi iracundia"
  7220. list_item
  7221. paragraph
  7222. str "aliquando id"
  7223. ```
  7224. Notice how the [line ending] in the first paragraph has
  7225. been parsed as a `softbreak`, and the asterisks in the first list item
  7226. have become an `emph`.
  7227. ### An algorithm for parsing nested emphasis and links
  7228. By far the trickiest part of inline parsing is handling emphasis,
  7229. strong emphasis, links, and images. This is done using the following
  7230. algorithm.
  7231. When we're parsing inlines and we hit either
  7232. - a run of `*` or `_` characters, or
  7233. - a `[` or `![`
  7234. we insert a text node with these symbols as its literal content, and we
  7235. add a pointer to this text node to the [delimiter stack](@).
  7236. The [delimiter stack] is a doubly linked list. Each
  7237. element contains a pointer to a text node, plus information about
  7238. - the type of delimiter (`[`, `![`, `*`, `_`)
  7239. - the number of delimiters,
  7240. - whether the delimiter is "active" (all are active to start), and
  7241. - whether the delimiter is a potential opener, a potential closer,
  7242. or both (which depends on what sort of characters precede
  7243. and follow the delimiters).
  7244. When we hit a `]` character, we call the *look for link or image*
  7245. procedure (see below).
  7246. When we hit the end of the input, we call the *process emphasis*
  7247. procedure (see below), with `stack_bottom` = NULL.
  7248. #### *look for link or image*
  7249. Starting at the top of the delimiter stack, we look backwards
  7250. through the stack for an opening `[` or `![` delimiter.
  7251. - If we don't find one, we return a literal text node `]`.
  7252. - If we do find one, but it's not *active*, we remove the inactive
  7253. delimiter from the stack, and return a literal text node `]`.
  7254. - If we find one and it's active, then we parse ahead to see if
  7255. we have an inline link/image, reference link/image, compact reference
  7256. link/image, or shortcut reference link/image.
  7257. + If we don't, then we remove the opening delimiter from the
  7258. delimiter stack and return a literal text node `]`.
  7259. + If we do, then
  7260. * We return a link or image node whose children are the inlines
  7261. after the text node pointed to by the opening delimiter.
  7262. * We run *process emphasis* on these inlines, with the `[` opener
  7263. as `stack_bottom`.
  7264. * We remove the opening delimiter.
  7265. * If we have a link (and not an image), we also set all
  7266. `[` delimiters before the opening delimiter to *inactive*. (This
  7267. will prevent us from getting links within links.)
  7268. #### *process emphasis*
  7269. Parameter `stack_bottom` sets a lower bound to how far we
  7270. descend in the [delimiter stack]. If it is NULL, we can
  7271. go all the way to the bottom. Otherwise, we stop before
  7272. visiting `stack_bottom`.
  7273. Let `current_position` point to the element on the [delimiter stack]
  7274. just above `stack_bottom` (or the first element if `stack_bottom`
  7275. is NULL).
  7276. We keep track of the `openers_bottom` for each delimiter
  7277. type (`*`, `_`) and each length of the closing delimiter run
  7278. (modulo 3). Initialize this to `stack_bottom`.
  7279. Then we repeat the following until we run out of potential
  7280. closers:
  7281. - Move `current_position` forward in the delimiter stack (if needed)
  7282. until we find the first potential closer with delimiter `*` or `_`.
  7283. (This will be the potential closer closest
  7284. to the beginning of the input -- the first one in parse order.)
  7285. - Now, look back in the stack (staying above `stack_bottom` and
  7286. the `openers_bottom` for this delimiter type) for the
  7287. first matching potential opener ("matching" means same delimiter).
  7288. - If one is found:
  7289. + Figure out whether we have emphasis or strong emphasis:
  7290. if both closer and opener spans have length >= 2, we have
  7291. strong, otherwise regular.
  7292. + Insert an emph or strong emph node accordingly, after
  7293. the text node corresponding to the opener.
  7294. + Remove any delimiters between the opener and closer from
  7295. the delimiter stack.
  7296. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7297. from the opening and closing text nodes. If they become empty
  7298. as a result, remove them and remove the corresponding element
  7299. of the delimiter stack. If the closing node is removed, reset
  7300. `current_position` to the next element in the stack.
  7301. - If none is found:
  7302. + Set `openers_bottom` to the element before `current_position`.
  7303. (We know that there are no openers for this kind of closer up to and
  7304. including this point, so this puts a lower bound on future searches.)
  7305. + If the closer at `current_position` is not a potential opener,
  7306. remove it from the delimiter stack (since we know it can't
  7307. be a closer either).
  7308. + Advance `current_position` to the next element in the stack.
  7309. After we're done, we remove all delimiters above `stack_bottom` from the
  7310. delimiter stack.