aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 3e96e6f1d7c045a321362a8ea730ed60f7dcc345 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author:
  4. - John MacFarlane
  5. version: 0.13
  6. date: 2014-12-10
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions used for indicating formatting in email and
  12. usenet posts. It was developed in 2004 by John Gruber, who wrote
  13. the first Markdown-to-HTML converter in perl, and it soon became
  14. widely used in websites. By 2014 there were dozens of
  15. implementations in many languages. Some of them extended basic
  16. Markdown syntax with conventions for footnotes, definition lists,
  17. tables, and other constructs, and some allowed output not just in
  18. HTML but in LaTeX and many other formats.
  19. ## Why is a spec needed?
  20. John Gruber's [canonical description of Markdown's
  21. syntax](http://daringfireball.net/projects/markdown/syntax)
  22. does not specify the syntax unambiguously. Here are some examples of
  23. questions it does not answer:
  24. 1. How much indentation is needed for a sublist? The spec says that
  25. continuation paragraphs need to be indented four spaces, but is
  26. not fully explicit about sublists. It is natural to think that
  27. they, too, must be indented four spaces, but `Markdown.pl` does
  28. not require that. This is hardly a "corner case," and divergences
  29. between implementations on this issue often lead to surprises for
  30. users in real documents. (See [this comment by John
  31. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  32. 2. Is a blank line needed before a block quote or header?
  33. Most implementations do not require the blank line. However,
  34. this can lead to unexpected results in hard-wrapped text, and
  35. also to ambiguities in parsing (note that some implementations
  36. put the header inside the blockquote, while others do not).
  37. (John Gruber has also spoken [in favor of requiring the blank
  38. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  39. 3. Is a blank line needed before an indented code block?
  40. (`Markdown.pl` requires it, but this is not mentioned in the
  41. documentation, and some implementations do not require it.)
  42. ``` markdown
  43. paragraph
  44. code?
  45. ```
  46. 4. What is the exact rule for determining when list items get
  47. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  48. "tight"? What should we do with a list like this?
  49. ``` markdown
  50. 1. one
  51. 2. two
  52. 3. three
  53. ```
  54. Or this?
  55. ``` markdown
  56. 1. one
  57. - a
  58. - b
  59. 2. two
  60. ```
  61. (There are some relevant comments by John Gruber
  62. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  63. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  64. ``` markdown
  65. 8. item 1
  66. 9. item 2
  67. 10. item 2a
  68. ```
  69. 6. Is this one list with a horizontal rule in its second item,
  70. or two lists separated by a horizontal rule?
  71. ``` markdown
  72. * a
  73. * * * * *
  74. * b
  75. ```
  76. 7. When list markers change from numbers to bullets, do we have
  77. two lists or one? (The Markdown syntax description suggests two,
  78. but the perl scripts and many other implementations produce one.)
  79. ``` markdown
  80. 1. fee
  81. 2. fie
  82. - foe
  83. - fum
  84. ```
  85. 8. What are the precedence rules for the markers of inline structure?
  86. For example, is the following a valid link, or does the code span
  87. take precedence ?
  88. ``` markdown
  89. [a backtick (`)](/url) and [another backtick (`)](/url).
  90. ```
  91. 9. What are the precedence rules for markers of emphasis and strong
  92. emphasis? For example, how should the following be parsed?
  93. ``` markdown
  94. *foo *bar* baz*
  95. ```
  96. 10. What are the precedence rules between block-level and inline-level
  97. structure? For example, how should the following be parsed?
  98. ``` markdown
  99. - `a long code span can contain a hyphen like this
  100. - and it can screw things up`
  101. ```
  102. 11. Can list items include section headers? (`Markdown.pl` does not
  103. allow this, but does allow blockquotes to include headers.)
  104. ``` markdown
  105. - # Heading
  106. ```
  107. 12. Can list items be empty?
  108. ``` markdown
  109. * a
  110. *
  111. * b
  112. ```
  113. 13. Can link references be defined inside block quotes or list items?
  114. ``` markdown
  115. > Blockquote [foo].
  116. >
  117. > [foo]: /url
  118. ```
  119. 14. If there are multiple definitions for the same reference, which takes
  120. precedence?
  121. ``` markdown
  122. [foo]: /url1
  123. [foo]: /url2
  124. [foo][]
  125. ```
  126. In the absence of a spec, early implementers consulted `Markdown.pl`
  127. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  128. gave manifestly bad results in many cases, so it was not a
  129. satisfactory replacement for a spec.
  130. Because there is no unambiguous spec, implementations have diverged
  131. considerably. As a result, users are often surprised to find that
  132. a document that renders one way on one system (say, a github wiki)
  133. renders differently on another (say, converting to docbook using
  134. pandoc). To make matters worse, because nothing in Markdown counts
  135. as a "syntax error," the divergence often isn't discovered right away.
  136. ## About this document
  137. This document attempts to specify Markdown syntax unambiguously.
  138. It contains many examples with side-by-side Markdown and
  139. HTML. These are intended to double as conformance tests. An
  140. accompanying script `spec_tests.py` can be used to run the tests
  141. against any Markdown program:
  142. python test/spec_tests.py --spec spec.txt --program PROGRAM
  143. Since this document describes how Markdown is to be parsed into
  144. an abstract syntax tree, it would have made sense to use an abstract
  145. representation of the syntax tree instead of HTML. But HTML is capable
  146. of representing the structural distinctions we need to make, and the
  147. choice of HTML for the tests makes it possible to run the tests against
  148. an implementation without writing an abstract syntax tree renderer.
  149. This document is generated from a text file, `spec.txt`, written
  150. in Markdown with a small extension for the side-by-side tests.
  151. The script `spec2md.pl` can be used to turn `spec.txt` into pandoc
  152. Markdown, which can then be converted into other formats.
  153. In the examples, the `→` character is used to represent tabs.
  154. # Preliminaries
  155. ## Characters and lines
  156. The input is a sequence of zero or more [lines](#line).
  157. A [line](@line)
  158. is a sequence of zero or more [characters](#character) followed by a
  159. [line ending](#line-ending) or by the end of file.
  160. A [character](@character) is a unicode code point.
  161. This spec does not specify an encoding; it thinks of lines as composed
  162. of characters rather than bytes. A conforming parser may be limited
  163. to a certain encoding.
  164. A [line ending](@line-ending) is, depending on the platform, a
  165. newline (`U+000A`), carriage return (`U+000D`), or
  166. carriage return + newline.
  167. For security reasons, a conforming parser must strip or replace the
  168. Unicode character `U+0000`.
  169. A line containing no characters, or a line containing only spaces
  170. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@blank-line).
  171. The following definitions of character classes will be used in this spec:
  172. A [whitespace character](@whitespace-character) is a space
  173. (`U+0020`), tab (`U+0009`), carriage return (`U+000D`), or
  174. newline (`U+000A`).
  175. [Whitespace](@whitespace) is a sequence of one or more [whitespace
  176. characters](#whitespace-character).
  177. A [unicode whitespace character](@unicode-whitespace-character) is
  178. any code point in the unicode `Zs` class, or a tab (`U+0009`),
  179. carriage return (`U+000D`), newline (`U+000A`), or form feed
  180. (`U+000C`).
  181. [Unicode whitespace](@unicode-whitespace) is a sequence of one
  182. or more [unicode whitespace characters](#unicode-whitespace-character).
  183. A [non-space character](@non-space-character) is anything but `U+0020`.
  184. A [punctuation character](@punctuation-character) is anything in
  185. the unicode classes `Pc`, `Pd`, `Pe`,` `Pf`, `Pi`, `Po`, or `Ps`.
  186. An [ASCII punctuation character](@ascii-punctuation-character)
  187. is a [punctuation character](#punctuation-character) in the
  188. ASCII class: that is, `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  189. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  190. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  191. ## Tab expansion
  192. Tabs in lines are expanded to spaces, with a tab stop of 4 characters:
  193. .
  194. →foo→baz→→bim
  195. .
  196. <pre><code>foo baz bim
  197. </code></pre>
  198. .
  199. .
  200. a→a
  201. ὐ→a
  202. .
  203. <pre><code>a a
  204. ὐ a
  205. </code></pre>
  206. .
  207. # Blocks and inlines
  208. We can think of a document as a sequence of
  209. [blocks](@block)---structural
  210. elements like paragraphs, block quotations,
  211. lists, headers, rules, and code blocks. Blocks can contain other
  212. blocks, or they can contain [inline](@inline) content:
  213. words, spaces, links, emphasized text, images, and inline code.
  214. ## Precedence
  215. Indicators of block structure always take precedence over indicators
  216. of inline structure. So, for example, the following is a list with
  217. two items, not a list with one item containing a code span:
  218. .
  219. - `one
  220. - two`
  221. .
  222. <ul>
  223. <li>`one</li>
  224. <li>two`</li>
  225. </ul>
  226. .
  227. This means that parsing can proceed in two steps: first, the block
  228. structure of the document can be discerned; second, text lines inside
  229. paragraphs, headers, and other block constructs can be parsed for inline
  230. structure. The second step requires information about link reference
  231. definitions that will be available only at the end of the first
  232. step. Note that the first step requires processing lines in sequence,
  233. but the second can be parallelized, since the inline parsing of
  234. one block element does not affect the inline parsing of any other.
  235. ## Container blocks and leaf blocks
  236. We can divide blocks into two types:
  237. [container blocks](@container-block),
  238. which can contain other blocks, and [leaf blocks](@leaf-block),
  239. which cannot.
  240. # Leaf blocks
  241. This section describes the different kinds of leaf block that make up a
  242. Markdown document.
  243. ## Horizontal rules
  244. A line consisting of 0-3 spaces of indentation, followed by a sequence
  245. of three or more matching `-`, `_`, or `*` characters, each followed
  246. optionally by any number of spaces, forms a [horizontal
  247. rule](@horizontal-rule).
  248. .
  249. ***
  250. ---
  251. ___
  252. .
  253. <hr />
  254. <hr />
  255. <hr />
  256. .
  257. Wrong characters:
  258. .
  259. +++
  260. .
  261. <p>+++</p>
  262. .
  263. .
  264. ===
  265. .
  266. <p>===</p>
  267. .
  268. Not enough characters:
  269. .
  270. --
  271. **
  272. __
  273. .
  274. <p>--
  275. **
  276. __</p>
  277. .
  278. One to three spaces indent are allowed:
  279. .
  280. ***
  281. ***
  282. ***
  283. .
  284. <hr />
  285. <hr />
  286. <hr />
  287. .
  288. Four spaces is too many:
  289. .
  290. ***
  291. .
  292. <pre><code>***
  293. </code></pre>
  294. .
  295. .
  296. Foo
  297. ***
  298. .
  299. <p>Foo
  300. ***</p>
  301. .
  302. More than three characters may be used:
  303. .
  304. _____________________________________
  305. .
  306. <hr />
  307. .
  308. Spaces are allowed between the characters:
  309. .
  310. - - -
  311. .
  312. <hr />
  313. .
  314. .
  315. ** * ** * ** * **
  316. .
  317. <hr />
  318. .
  319. .
  320. - - - -
  321. .
  322. <hr />
  323. .
  324. Spaces are allowed at the end:
  325. .
  326. - - - -
  327. .
  328. <hr />
  329. .
  330. However, no other characters may occur in the line:
  331. .
  332. _ _ _ _ a
  333. a------
  334. ---a---
  335. .
  336. <p>_ _ _ _ a</p>
  337. <p>a------</p>
  338. <p>---a---</p>
  339. .
  340. It is required that all of the
  341. [non-space characters](#non-space-character) be the same.
  342. So, this is not a horizontal rule:
  343. .
  344. *-*
  345. .
  346. <p><em>-</em></p>
  347. .
  348. Horizontal rules do not need blank lines before or after:
  349. .
  350. - foo
  351. ***
  352. - bar
  353. .
  354. <ul>
  355. <li>foo</li>
  356. </ul>
  357. <hr />
  358. <ul>
  359. <li>bar</li>
  360. </ul>
  361. .
  362. Horizontal rules can interrupt a paragraph:
  363. .
  364. Foo
  365. ***
  366. bar
  367. .
  368. <p>Foo</p>
  369. <hr />
  370. <p>bar</p>
  371. .
  372. If a line of dashes that meets the above conditions for being a
  373. horizontal rule could also be interpreted as the underline of a [setext
  374. header](#setext-header), the interpretation as a
  375. [setext-header](#setext-header) takes precedence. Thus, for example,
  376. this is a setext header, not a paragraph followed by a horizontal rule:
  377. .
  378. Foo
  379. ---
  380. bar
  381. .
  382. <h2>Foo</h2>
  383. <p>bar</p>
  384. .
  385. When both a horizontal rule and a list item are possible
  386. interpretations of a line, the horizontal rule is preferred:
  387. .
  388. * Foo
  389. * * *
  390. * Bar
  391. .
  392. <ul>
  393. <li>Foo</li>
  394. </ul>
  395. <hr />
  396. <ul>
  397. <li>Bar</li>
  398. </ul>
  399. .
  400. If you want a horizontal rule in a list item, use a different bullet:
  401. .
  402. - Foo
  403. - * * *
  404. .
  405. <ul>
  406. <li>Foo</li>
  407. <li>
  408. <hr />
  409. </li>
  410. </ul>
  411. .
  412. ## ATX headers
  413. An [ATX header](@atx-header)
  414. consists of a string of characters, parsed as inline content, between an
  415. opening sequence of 1--6 unescaped `#` characters and an optional
  416. closing sequence of any number of `#` characters. The opening sequence
  417. of `#` characters cannot be followed directly by a nonspace character.
  418. The optional closing sequence of `#`s must be preceded by a space and may be
  419. followed by spaces only. The opening `#` character may be indented 0-3
  420. spaces. The raw contents of the header are stripped of leading and
  421. trailing spaces before being parsed as inline content. The header level
  422. is equal to the number of `#` characters in the opening sequence.
  423. Simple headers:
  424. .
  425. # foo
  426. ## foo
  427. ### foo
  428. #### foo
  429. ##### foo
  430. ###### foo
  431. .
  432. <h1>foo</h1>
  433. <h2>foo</h2>
  434. <h3>foo</h3>
  435. <h4>foo</h4>
  436. <h5>foo</h5>
  437. <h6>foo</h6>
  438. .
  439. More than six `#` characters is not a header:
  440. .
  441. ####### foo
  442. .
  443. <p>####### foo</p>
  444. .
  445. A space is required between the `#` characters and the header's
  446. contents. Note that many implementations currently do not require
  447. the space. However, the space was required by the [original ATX
  448. implementation](http://www.aaronsw.com/2002/atx/atx.py), and it helps
  449. prevent things like the following from being parsed as headers:
  450. .
  451. #5 bolt
  452. .
  453. <p>#5 bolt</p>
  454. .
  455. This is not a header, because the first `#` is escaped:
  456. .
  457. \## foo
  458. .
  459. <p>## foo</p>
  460. .
  461. Contents are parsed as inlines:
  462. .
  463. # foo *bar* \*baz\*
  464. .
  465. <h1>foo <em>bar</em> *baz*</h1>
  466. .
  467. Leading and trailing blanks are ignored in parsing inline content:
  468. .
  469. # foo
  470. .
  471. <h1>foo</h1>
  472. .
  473. One to three spaces indentation are allowed:
  474. .
  475. ### foo
  476. ## foo
  477. # foo
  478. .
  479. <h3>foo</h3>
  480. <h2>foo</h2>
  481. <h1>foo</h1>
  482. .
  483. Four spaces are too much:
  484. .
  485. # foo
  486. .
  487. <pre><code># foo
  488. </code></pre>
  489. .
  490. .
  491. foo
  492. # bar
  493. .
  494. <p>foo
  495. # bar</p>
  496. .
  497. A closing sequence of `#` characters is optional:
  498. .
  499. ## foo ##
  500. ### bar ###
  501. .
  502. <h2>foo</h2>
  503. <h3>bar</h3>
  504. .
  505. It need not be the same length as the opening sequence:
  506. .
  507. # foo ##################################
  508. ##### foo ##
  509. .
  510. <h1>foo</h1>
  511. <h5>foo</h5>
  512. .
  513. Spaces are allowed after the closing sequence:
  514. .
  515. ### foo ###
  516. .
  517. <h3>foo</h3>
  518. .
  519. A sequence of `#` characters with a nonspace character following it
  520. is not a closing sequence, but counts as part of the contents of the
  521. header:
  522. .
  523. ### foo ### b
  524. .
  525. <h3>foo ### b</h3>
  526. .
  527. The closing sequence must be preceded by a space:
  528. .
  529. # foo#
  530. .
  531. <h1>foo#</h1>
  532. .
  533. Backslash-escaped `#` characters do not count as part
  534. of the closing sequence:
  535. .
  536. ### foo \###
  537. ## foo #\##
  538. # foo \#
  539. .
  540. <h3>foo ###</h3>
  541. <h2>foo ###</h2>
  542. <h1>foo #</h1>
  543. .
  544. ATX headers need not be separated from surrounding content by blank
  545. lines, and they can interrupt paragraphs:
  546. .
  547. ****
  548. ## foo
  549. ****
  550. .
  551. <hr />
  552. <h2>foo</h2>
  553. <hr />
  554. .
  555. .
  556. Foo bar
  557. # baz
  558. Bar foo
  559. .
  560. <p>Foo bar</p>
  561. <h1>baz</h1>
  562. <p>Bar foo</p>
  563. .
  564. ATX headers can be empty:
  565. .
  566. ##
  567. #
  568. ### ###
  569. .
  570. <h2></h2>
  571. <h1></h1>
  572. <h3></h3>
  573. .
  574. ## Setext headers
  575. A [setext header](@setext-header)
  576. consists of a line of text, containing at least one nonspace character,
  577. with no more than 3 spaces indentation, followed by a [setext header
  578. underline](#setext-header-underline). The line of text must be
  579. one that, were it not followed by the setext header underline,
  580. would be interpreted as part of a paragraph: it cannot be a code
  581. block, header, blockquote, horizontal rule, or list. A [setext header
  582. underline](@setext-header-underline)
  583. is a sequence of `=` characters or a sequence of `-` characters, with no
  584. more than 3 spaces indentation and any number of trailing
  585. spaces. The header is a level 1 header if `=` characters are used, and
  586. a level 2 header if `-` characters are used. The contents of the header
  587. are the result of parsing the first line as Markdown inline content.
  588. In general, a setext header need not be preceded or followed by a
  589. blank line. However, it cannot interrupt a paragraph, so when a
  590. setext header comes after a paragraph, a blank line is needed between
  591. them.
  592. Simple examples:
  593. .
  594. Foo *bar*
  595. =========
  596. Foo *bar*
  597. ---------
  598. .
  599. <h1>Foo <em>bar</em></h1>
  600. <h2>Foo <em>bar</em></h2>
  601. .
  602. The underlining can be any length:
  603. .
  604. Foo
  605. -------------------------
  606. Foo
  607. =
  608. .
  609. <h2>Foo</h2>
  610. <h1>Foo</h1>
  611. .
  612. The header content can be indented up to three spaces, and need
  613. not line up with the underlining:
  614. .
  615. Foo
  616. ---
  617. Foo
  618. -----
  619. Foo
  620. ===
  621. .
  622. <h2>Foo</h2>
  623. <h2>Foo</h2>
  624. <h1>Foo</h1>
  625. .
  626. Four spaces indent is too much:
  627. .
  628. Foo
  629. ---
  630. Foo
  631. ---
  632. .
  633. <pre><code>Foo
  634. ---
  635. Foo
  636. </code></pre>
  637. <hr />
  638. .
  639. The setext header underline can be indented up to three spaces, and
  640. may have trailing spaces:
  641. .
  642. Foo
  643. ----
  644. .
  645. <h2>Foo</h2>
  646. .
  647. Four spaces is too much:
  648. .
  649. Foo
  650. ---
  651. .
  652. <p>Foo
  653. ---</p>
  654. .
  655. The setext header underline cannot contain internal spaces:
  656. .
  657. Foo
  658. = =
  659. Foo
  660. --- -
  661. .
  662. <p>Foo
  663. = =</p>
  664. <p>Foo</p>
  665. <hr />
  666. .
  667. Trailing spaces in the content line do not cause a line break:
  668. .
  669. Foo
  670. -----
  671. .
  672. <h2>Foo</h2>
  673. .
  674. Nor does a backslash at the end:
  675. .
  676. Foo\
  677. ----
  678. .
  679. <h2>Foo\</h2>
  680. .
  681. Since indicators of block structure take precedence over
  682. indicators of inline structure, the following are setext headers:
  683. .
  684. `Foo
  685. ----
  686. `
  687. <a title="a lot
  688. ---
  689. of dashes"/>
  690. .
  691. <h2>`Foo</h2>
  692. <p>`</p>
  693. <h2>&lt;a title=&quot;a lot</h2>
  694. <p>of dashes&quot;/&gt;</p>
  695. .
  696. The setext header underline cannot be a [lazy continuation
  697. line](#lazy-continuation-line) in a list item or block quote:
  698. .
  699. > Foo
  700. ---
  701. .
  702. <blockquote>
  703. <p>Foo</p>
  704. </blockquote>
  705. <hr />
  706. .
  707. .
  708. - Foo
  709. ---
  710. .
  711. <ul>
  712. <li>Foo</li>
  713. </ul>
  714. <hr />
  715. .
  716. A setext header cannot interrupt a paragraph:
  717. .
  718. Foo
  719. Bar
  720. ---
  721. Foo
  722. Bar
  723. ===
  724. .
  725. <p>Foo
  726. Bar</p>
  727. <hr />
  728. <p>Foo
  729. Bar
  730. ===</p>
  731. .
  732. But in general a blank line is not required before or after:
  733. .
  734. ---
  735. Foo
  736. ---
  737. Bar
  738. ---
  739. Baz
  740. .
  741. <hr />
  742. <h2>Foo</h2>
  743. <h2>Bar</h2>
  744. <p>Baz</p>
  745. .
  746. Setext headers cannot be empty:
  747. .
  748. ====
  749. .
  750. <p>====</p>
  751. .
  752. Setext header text lines must not be interpretable as block
  753. constructs other than paragraphs. So, the line of dashes
  754. in these examples gets interpreted as a horizontal rule:
  755. .
  756. ---
  757. ---
  758. .
  759. <hr />
  760. <hr />
  761. .
  762. .
  763. - foo
  764. -----
  765. .
  766. <ul>
  767. <li>foo</li>
  768. </ul>
  769. <hr />
  770. .
  771. .
  772. foo
  773. ---
  774. .
  775. <pre><code>foo
  776. </code></pre>
  777. <hr />
  778. .
  779. .
  780. > foo
  781. -----
  782. .
  783. <blockquote>
  784. <p>foo</p>
  785. </blockquote>
  786. <hr />
  787. .
  788. If you want a header with `> foo` as its literal text, you can
  789. use backslash escapes:
  790. .
  791. \> foo
  792. ------
  793. .
  794. <h2>&gt; foo</h2>
  795. .
  796. ## Indented code blocks
  797. An [indented code block](@indented-code-block) is composed of one or more
  798. [indented chunks](#indented-chunk) separated by blank lines.
  799. An [indented chunk](@indented-chunk) is a sequence of non-blank lines,
  800. each indented four or more spaces. The contents of the code block are
  801. the literal contents of the lines, including trailing
  802. [line endings](#line-ending), minus four spaces of indentation.
  803. An indented code block has no attributes.
  804. An indented code block cannot interrupt a paragraph, so there must be
  805. a blank line between a paragraph and a following indented code block.
  806. (A blank line is not needed, however, between a code block and a following
  807. paragraph.)
  808. .
  809. a simple
  810. indented code block
  811. .
  812. <pre><code>a simple
  813. indented code block
  814. </code></pre>
  815. .
  816. The contents are literal text, and do not get parsed as Markdown:
  817. .
  818. <a/>
  819. *hi*
  820. - one
  821. .
  822. <pre><code>&lt;a/&gt;
  823. *hi*
  824. - one
  825. </code></pre>
  826. .
  827. Here we have three chunks separated by blank lines:
  828. .
  829. chunk1
  830. chunk2
  831. chunk3
  832. .
  833. <pre><code>chunk1
  834. chunk2
  835. chunk3
  836. </code></pre>
  837. .
  838. Any initial spaces beyond four will be included in the content, even
  839. in interior blank lines:
  840. .
  841. chunk1
  842. chunk2
  843. .
  844. <pre><code>chunk1
  845. chunk2
  846. </code></pre>
  847. .
  848. An indented code block cannot interrupt a paragraph. (This
  849. allows hanging indents and the like.)
  850. .
  851. Foo
  852. bar
  853. .
  854. <p>Foo
  855. bar</p>
  856. .
  857. However, any non-blank line with fewer than four leading spaces ends
  858. the code block immediately. So a paragraph may occur immediately
  859. after indented code:
  860. .
  861. foo
  862. bar
  863. .
  864. <pre><code>foo
  865. </code></pre>
  866. <p>bar</p>
  867. .
  868. And indented code can occur immediately before and after other kinds of
  869. blocks:
  870. .
  871. # Header
  872. foo
  873. Header
  874. ------
  875. foo
  876. ----
  877. .
  878. <h1>Header</h1>
  879. <pre><code>foo
  880. </code></pre>
  881. <h2>Header</h2>
  882. <pre><code>foo
  883. </code></pre>
  884. <hr />
  885. .
  886. The first line can be indented more than four spaces:
  887. .
  888. foo
  889. bar
  890. .
  891. <pre><code> foo
  892. bar
  893. </code></pre>
  894. .
  895. Blank lines preceding or following an indented code block
  896. are not included in it:
  897. .
  898. foo
  899. .
  900. <pre><code>foo
  901. </code></pre>
  902. .
  903. Trailing spaces are included in the code block's content:
  904. .
  905. foo
  906. .
  907. <pre><code>foo
  908. </code></pre>
  909. .
  910. ## Fenced code blocks
  911. A [code fence](@code-fence) is a sequence
  912. of at least three consecutive backtick characters (`` ` ``) or
  913. tildes (`~`). (Tildes and backticks cannot be mixed.)
  914. A [fenced code block](@fenced-code-block)
  915. begins with a code fence, indented no more than three spaces.
  916. The line with the opening code fence may optionally contain some text
  917. following the code fence; this is trimmed of leading and trailing
  918. spaces and called the [info string](@info-string).
  919. The info string may not contain any backtick
  920. characters. (The reason for this restriction is that otherwise
  921. some inline code would be incorrectly interpreted as the
  922. beginning of a fenced code block.)
  923. The content of the code block consists of all subsequent lines, until
  924. a closing [code fence](#code-fence) of the same type as the code block
  925. began with (backticks or tildes), and with at least as many backticks
  926. or tildes as the opening code fence. If the leading code fence is
  927. indented N spaces, then up to N spaces of indentation are removed from
  928. each line of the content (if present). (If a content line is not
  929. indented, it is preserved unchanged. If it is indented less than N
  930. spaces, all of the indentation is removed.)
  931. The closing code fence may be indented up to three spaces, and may be
  932. followed only by spaces, which are ignored. If the end of the
  933. containing block (or document) is reached and no closing code fence
  934. has been found, the code block contains all of the lines after the
  935. opening code fence until the end of the containing block (or
  936. document). (An alternative spec would require backtracking in the
  937. event that a closing code fence is not found. But this makes parsing
  938. much less efficient, and there seems to be no real down side to the
  939. behavior described here.)
  940. A fenced code block may interrupt a paragraph, and does not require
  941. a blank line either before or after.
  942. The content of a code fence is treated as literal text, not parsed
  943. as inlines. The first word of the info string is typically used to
  944. specify the language of the code sample, and rendered in the `class`
  945. attribute of the `code` tag. However, this spec does not mandate any
  946. particular treatment of the info string.
  947. Here is a simple example with backticks:
  948. .
  949. ```
  950. <
  951. >
  952. ```
  953. .
  954. <pre><code>&lt;
  955. &gt;
  956. </code></pre>
  957. .
  958. With tildes:
  959. .
  960. ~~~
  961. <
  962. >
  963. ~~~
  964. .
  965. <pre><code>&lt;
  966. &gt;
  967. </code></pre>
  968. .
  969. The closing code fence must use the same character as the opening
  970. fence:
  971. .
  972. ```
  973. aaa
  974. ~~~
  975. ```
  976. .
  977. <pre><code>aaa
  978. ~~~
  979. </code></pre>
  980. .
  981. .
  982. ~~~
  983. aaa
  984. ```
  985. ~~~
  986. .
  987. <pre><code>aaa
  988. ```
  989. </code></pre>
  990. .
  991. The closing code fence must be at least as long as the opening fence:
  992. .
  993. ````
  994. aaa
  995. ```
  996. ``````
  997. .
  998. <pre><code>aaa
  999. ```
  1000. </code></pre>
  1001. .
  1002. .
  1003. ~~~~
  1004. aaa
  1005. ~~~
  1006. ~~~~
  1007. .
  1008. <pre><code>aaa
  1009. ~~~
  1010. </code></pre>
  1011. .
  1012. Unclosed code blocks are closed by the end of the document:
  1013. .
  1014. ```
  1015. .
  1016. <pre><code></code></pre>
  1017. .
  1018. .
  1019. `````
  1020. ```
  1021. aaa
  1022. .
  1023. <pre><code>
  1024. ```
  1025. aaa
  1026. </code></pre>
  1027. .
  1028. A code block can have all empty lines as its content:
  1029. .
  1030. ```
  1031. ```
  1032. .
  1033. <pre><code>
  1034. </code></pre>
  1035. .
  1036. A code block can be empty:
  1037. .
  1038. ```
  1039. ```
  1040. .
  1041. <pre><code></code></pre>
  1042. .
  1043. Fences can be indented. If the opening fence is indented,
  1044. content lines will have equivalent opening indentation removed,
  1045. if present:
  1046. .
  1047. ```
  1048. aaa
  1049. aaa
  1050. ```
  1051. .
  1052. <pre><code>aaa
  1053. aaa
  1054. </code></pre>
  1055. .
  1056. .
  1057. ```
  1058. aaa
  1059. aaa
  1060. aaa
  1061. ```
  1062. .
  1063. <pre><code>aaa
  1064. aaa
  1065. aaa
  1066. </code></pre>
  1067. .
  1068. .
  1069. ```
  1070. aaa
  1071. aaa
  1072. aaa
  1073. ```
  1074. .
  1075. <pre><code>aaa
  1076. aaa
  1077. aaa
  1078. </code></pre>
  1079. .
  1080. Four spaces indentation produces an indented code block:
  1081. .
  1082. ```
  1083. aaa
  1084. ```
  1085. .
  1086. <pre><code>```
  1087. aaa
  1088. ```
  1089. </code></pre>
  1090. .
  1091. Closing fences may be indented by 0-3 spaces, and their indentation
  1092. need not match that of the opening fence:
  1093. .
  1094. ```
  1095. aaa
  1096. ```
  1097. .
  1098. <pre><code>aaa
  1099. </code></pre>
  1100. .
  1101. .
  1102. ```
  1103. aaa
  1104. ```
  1105. .
  1106. <pre><code>aaa
  1107. </code></pre>
  1108. .
  1109. This is not a closing fence, because it is indented 4 spaces:
  1110. .
  1111. ```
  1112. aaa
  1113. ```
  1114. .
  1115. <pre><code>aaa
  1116. ```
  1117. </code></pre>
  1118. .
  1119. Code fences (opening and closing) cannot contain internal spaces:
  1120. .
  1121. ``` ```
  1122. aaa
  1123. .
  1124. <p><code></code>
  1125. aaa</p>
  1126. .
  1127. .
  1128. ~~~~~~
  1129. aaa
  1130. ~~~ ~~
  1131. .
  1132. <pre><code>aaa
  1133. ~~~ ~~
  1134. </code></pre>
  1135. .
  1136. Fenced code blocks can interrupt paragraphs, and can be followed
  1137. directly by paragraphs, without a blank line between:
  1138. .
  1139. foo
  1140. ```
  1141. bar
  1142. ```
  1143. baz
  1144. .
  1145. <p>foo</p>
  1146. <pre><code>bar
  1147. </code></pre>
  1148. <p>baz</p>
  1149. .
  1150. Other blocks can also occur before and after fenced code blocks
  1151. without an intervening blank line:
  1152. .
  1153. foo
  1154. ---
  1155. ~~~
  1156. bar
  1157. ~~~
  1158. # baz
  1159. .
  1160. <h2>foo</h2>
  1161. <pre><code>bar
  1162. </code></pre>
  1163. <h1>baz</h1>
  1164. .
  1165. An [info string](#info-string) can be provided after the opening code fence.
  1166. Opening and closing spaces will be stripped, and the first word, prefixed
  1167. with `language-`, is used as the value for the `class` attribute of the
  1168. `code` element within the enclosing `pre` element.
  1169. .
  1170. ```ruby
  1171. def foo(x)
  1172. return 3
  1173. end
  1174. ```
  1175. .
  1176. <pre><code class="language-ruby">def foo(x)
  1177. return 3
  1178. end
  1179. </code></pre>
  1180. .
  1181. .
  1182. ~~~~ ruby startline=3 $%@#$
  1183. def foo(x)
  1184. return 3
  1185. end
  1186. ~~~~~~~
  1187. .
  1188. <pre><code class="language-ruby">def foo(x)
  1189. return 3
  1190. end
  1191. </code></pre>
  1192. .
  1193. .
  1194. ````;
  1195. ````
  1196. .
  1197. <pre><code class="language-;"></code></pre>
  1198. .
  1199. Info strings for backtick code blocks cannot contain backticks:
  1200. .
  1201. ``` aa ```
  1202. foo
  1203. .
  1204. <p><code>aa</code>
  1205. foo</p>
  1206. .
  1207. Closing code fences cannot have info strings:
  1208. .
  1209. ```
  1210. ``` aaa
  1211. ```
  1212. .
  1213. <pre><code>``` aaa
  1214. </code></pre>
  1215. .
  1216. ## HTML blocks
  1217. An [HTML block tag](@html-block-tag) is
  1218. an [open tag](#open-tag) or [closing tag](#closing-tag) whose tag
  1219. name is one of the following (case-insensitive):
  1220. `article`, `header`, `aside`, `hgroup`, `blockquote`, `hr`, `iframe`,
  1221. `body`, `li`, `map`, `button`, `object`, `canvas`, `ol`, `caption`,
  1222. `output`, `col`, `p`, `colgroup`, `pre`, `dd`, `progress`, `div`,
  1223. `section`, `dl`, `table`, `td`, `dt`, `tbody`, `embed`, `textarea`,
  1224. `fieldset`, `tfoot`, `figcaption`, `th`, `figure`, `thead`, `footer`,
  1225. `tr`, `form`, `ul`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `video`,
  1226. `script`, `style`.
  1227. An [HTML block](@html-block) begins with an
  1228. [HTML block tag](#html-block-tag), [HTML comment](#html-comment),
  1229. [processing instruction](#processing-instruction),
  1230. [declaration](#declaration), or [CDATA section](#cdata-section).
  1231. It ends when a [blank line](#blank-line) or the end of the
  1232. input is encountered. The initial line may be indented up to three
  1233. spaces, and subsequent lines may have any indentation. The contents
  1234. of the HTML block are interpreted as raw HTML, and will not be escaped
  1235. in HTML output.
  1236. Some simple examples:
  1237. .
  1238. <table>
  1239. <tr>
  1240. <td>
  1241. hi
  1242. </td>
  1243. </tr>
  1244. </table>
  1245. okay.
  1246. .
  1247. <table>
  1248. <tr>
  1249. <td>
  1250. hi
  1251. </td>
  1252. </tr>
  1253. </table>
  1254. <p>okay.</p>
  1255. .
  1256. .
  1257. <div>
  1258. *hello*
  1259. <foo><a>
  1260. .
  1261. <div>
  1262. *hello*
  1263. <foo><a>
  1264. .
  1265. Here we have two HTML blocks with a Markdown paragraph between them:
  1266. .
  1267. <DIV CLASS="foo">
  1268. *Markdown*
  1269. </DIV>
  1270. .
  1271. <DIV CLASS="foo">
  1272. <p><em>Markdown</em></p>
  1273. </DIV>
  1274. .
  1275. In the following example, what looks like a Markdown code block
  1276. is actually part of the HTML block, which continues until a blank
  1277. line or the end of the document is reached:
  1278. .
  1279. <div></div>
  1280. ``` c
  1281. int x = 33;
  1282. ```
  1283. .
  1284. <div></div>
  1285. ``` c
  1286. int x = 33;
  1287. ```
  1288. .
  1289. A comment:
  1290. .
  1291. <!-- Foo
  1292. bar
  1293. baz -->
  1294. .
  1295. <!-- Foo
  1296. bar
  1297. baz -->
  1298. .
  1299. A processing instruction:
  1300. .
  1301. <?php
  1302. echo '>';
  1303. ?>
  1304. .
  1305. <?php
  1306. echo '>';
  1307. ?>
  1308. .
  1309. CDATA:
  1310. .
  1311. <![CDATA[
  1312. function matchwo(a,b)
  1313. {
  1314. if (a < b && a < 0) then
  1315. {
  1316. return 1;
  1317. }
  1318. else
  1319. {
  1320. return 0;
  1321. }
  1322. }
  1323. ]]>
  1324. .
  1325. <![CDATA[
  1326. function matchwo(a,b)
  1327. {
  1328. if (a < b && a < 0) then
  1329. {
  1330. return 1;
  1331. }
  1332. else
  1333. {
  1334. return 0;
  1335. }
  1336. }
  1337. ]]>
  1338. .
  1339. The opening tag can be indented 1-3 spaces, but not 4:
  1340. .
  1341. <!-- foo -->
  1342. <!-- foo -->
  1343. .
  1344. <!-- foo -->
  1345. <pre><code>&lt;!-- foo --&gt;
  1346. </code></pre>
  1347. .
  1348. An HTML block can interrupt a paragraph, and need not be preceded
  1349. by a blank line.
  1350. .
  1351. Foo
  1352. <div>
  1353. bar
  1354. </div>
  1355. .
  1356. <p>Foo</p>
  1357. <div>
  1358. bar
  1359. </div>
  1360. .
  1361. However, a following blank line is always needed, except at the end of
  1362. a document:
  1363. .
  1364. <div>
  1365. bar
  1366. </div>
  1367. *foo*
  1368. .
  1369. <div>
  1370. bar
  1371. </div>
  1372. *foo*
  1373. .
  1374. An incomplete HTML block tag may also start an HTML block:
  1375. .
  1376. <div class
  1377. foo
  1378. .
  1379. <div class
  1380. foo
  1381. .
  1382. This rule differs from John Gruber's original Markdown syntax
  1383. specification, which says:
  1384. > The only restrictions are that block-level HTML elements —
  1385. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1386. > surrounding content by blank lines, and the start and end tags of the
  1387. > block should not be indented with tabs or spaces.
  1388. In some ways Gruber's rule is more restrictive than the one given
  1389. here:
  1390. - It requires that an HTML block be preceded by a blank line.
  1391. - It does not allow the start tag to be indented.
  1392. - It requires a matching end tag, which it also does not allow to
  1393. be indented.
  1394. Indeed, most Markdown implementations, including some of Gruber's
  1395. own perl implementations, do not impose these restrictions.
  1396. There is one respect, however, in which Gruber's rule is more liberal
  1397. than the one given here, since it allows blank lines to occur inside
  1398. an HTML block. There are two reasons for disallowing them here.
  1399. First, it removes the need to parse balanced tags, which is
  1400. expensive and can require backtracking from the end of the document
  1401. if no matching end tag is found. Second, it provides a very simple
  1402. and flexible way of including Markdown content inside HTML tags:
  1403. simply separate the Markdown from the HTML using blank lines:
  1404. .
  1405. <div>
  1406. *Emphasized* text.
  1407. </div>
  1408. .
  1409. <div>
  1410. <p><em>Emphasized</em> text.</p>
  1411. </div>
  1412. .
  1413. Compare:
  1414. .
  1415. <div>
  1416. *Emphasized* text.
  1417. </div>
  1418. .
  1419. <div>
  1420. *Emphasized* text.
  1421. </div>
  1422. .
  1423. Some Markdown implementations have adopted a convention of
  1424. interpreting content inside tags as text if the open tag has
  1425. the attribute `markdown=1`. The rule given above seems a simpler and
  1426. more elegant way of achieving the same expressive power, which is also
  1427. much simpler to parse.
  1428. The main potential drawback is that one can no longer paste HTML
  1429. blocks into Markdown documents with 100% reliability. However,
  1430. *in most cases* this will work fine, because the blank lines in
  1431. HTML are usually followed by HTML block tags. For example:
  1432. .
  1433. <table>
  1434. <tr>
  1435. <td>
  1436. Hi
  1437. </td>
  1438. </tr>
  1439. </table>
  1440. .
  1441. <table>
  1442. <tr>
  1443. <td>
  1444. Hi
  1445. </td>
  1446. </tr>
  1447. </table>
  1448. .
  1449. Moreover, blank lines are usually not necessary and can be
  1450. deleted. The exception is inside `<pre>` tags; here, one can
  1451. replace the blank lines with `&#10;` entities.
  1452. So there is no important loss of expressive power with the new rule.
  1453. ## Link reference definitions
  1454. A [link reference definition](@link-reference-definition)
  1455. consists of a [link label](#link-label), indented up to three spaces, followed
  1456. by a colon (`:`), optional [whitespace](#whitespace) (including up to one
  1457. [line ending](#line-ending)), a [link destination](#link-destination),
  1458. optional [whitespace](#whitespace) (including up to one
  1459. [line ending](#line-ending)), and an optional [link
  1460. title](#link-title), which if it is present must be separated
  1461. from the [link destination](#link-destination) by [whitespace](#whitespace).
  1462. No further [non-space characters](#non-space-character) may occur on the line.
  1463. A [link reference-definition](#link-reference-definition)
  1464. does not correspond to a structural element of a document. Instead, it
  1465. defines a label which can be used in [reference links](#reference-link)
  1466. and reference-style [images](#images) elsewhere in the document. [Link
  1467. reference definitions] can come either before or after the links that use
  1468. them.
  1469. .
  1470. [foo]: /url "title"
  1471. [foo]
  1472. .
  1473. <p><a href="/url" title="title">foo</a></p>
  1474. .
  1475. .
  1476. [foo]:
  1477. /url
  1478. 'the title'
  1479. [foo]
  1480. .
  1481. <p><a href="/url" title="the title">foo</a></p>
  1482. .
  1483. .
  1484. [Foo*bar\]]:my_(url) 'title (with parens)'
  1485. [Foo*bar\]]
  1486. .
  1487. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  1488. .
  1489. .
  1490. [Foo bar]:
  1491. <my url>
  1492. 'title'
  1493. [Foo bar]
  1494. .
  1495. <p><a href="my%20url" title="title">Foo bar</a></p>
  1496. .
  1497. The title may be omitted:
  1498. .
  1499. [foo]:
  1500. /url
  1501. [foo]
  1502. .
  1503. <p><a href="/url">foo</a></p>
  1504. .
  1505. The link destination may not be omitted:
  1506. .
  1507. [foo]:
  1508. [foo]
  1509. .
  1510. <p>[foo]:</p>
  1511. <p>[foo]</p>
  1512. .
  1513. A link can come before its corresponding definition:
  1514. .
  1515. [foo]
  1516. [foo]: url
  1517. .
  1518. <p><a href="url">foo</a></p>
  1519. .
  1520. If there are several matching definitions, the first one takes
  1521. precedence:
  1522. .
  1523. [foo]
  1524. [foo]: first
  1525. [foo]: second
  1526. .
  1527. <p><a href="first">foo</a></p>
  1528. .
  1529. As noted in the section on [Links], matching of labels is
  1530. case-insensitive (see [matches](#matches)).
  1531. .
  1532. [FOO]: /url
  1533. [Foo]
  1534. .
  1535. <p><a href="/url">Foo</a></p>
  1536. .
  1537. .
  1538. [ΑΓΩ]: /φου
  1539. [αγω]
  1540. .
  1541. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  1542. .
  1543. Here is a link reference definition with no corresponding link.
  1544. It contributes nothing to the document.
  1545. .
  1546. [foo]: /url
  1547. .
  1548. .
  1549. This is not a link reference definition, because there are
  1550. [non-space characters](#non-space-character) after the title:
  1551. .
  1552. [foo]: /url "title" ok
  1553. .
  1554. <p>[foo]: /url &quot;title&quot; ok</p>
  1555. .
  1556. This is not a link reference definition, because it is indented
  1557. four spaces:
  1558. .
  1559. [foo]: /url "title"
  1560. [foo]
  1561. .
  1562. <pre><code>[foo]: /url &quot;title&quot;
  1563. </code></pre>
  1564. <p>[foo]</p>
  1565. .
  1566. This is not a link reference definition, because it occurs inside
  1567. a code block:
  1568. .
  1569. ```
  1570. [foo]: /url
  1571. ```
  1572. [foo]
  1573. .
  1574. <pre><code>[foo]: /url
  1575. </code></pre>
  1576. <p>[foo]</p>
  1577. .
  1578. A [link reference definition](#link-reference-definition) cannot
  1579. interrupt a paragraph.
  1580. .
  1581. Foo
  1582. [bar]: /baz
  1583. [bar]
  1584. .
  1585. <p>Foo
  1586. [bar]: /baz</p>
  1587. <p>[bar]</p>
  1588. .
  1589. However, it can directly follow other block elements, such as headers
  1590. and horizontal rules, and it need not be followed by a blank line.
  1591. .
  1592. # [Foo]
  1593. [foo]: /url
  1594. > bar
  1595. .
  1596. <h1><a href="/url">Foo</a></h1>
  1597. <blockquote>
  1598. <p>bar</p>
  1599. </blockquote>
  1600. .
  1601. Several [link references definitions](#link-reference-definition)
  1602. can occur one after another, without intervening blank lines.
  1603. .
  1604. [foo]: /foo-url "foo"
  1605. [bar]: /bar-url
  1606. "bar"
  1607. [baz]: /baz-url
  1608. [foo],
  1609. [bar],
  1610. [baz]
  1611. .
  1612. <p><a href="/foo-url" title="foo">foo</a>,
  1613. <a href="/bar-url" title="bar">bar</a>,
  1614. <a href="/baz-url">baz</a></p>
  1615. .
  1616. [Link reference definitions](#link-reference-definition) can occur
  1617. inside block containers, like lists and block quotations. They
  1618. affect the entire document, not just the container in which they
  1619. are defined:
  1620. .
  1621. [foo]
  1622. > [foo]: /url
  1623. .
  1624. <p><a href="/url">foo</a></p>
  1625. <blockquote>
  1626. </blockquote>
  1627. .
  1628. ## Paragraphs
  1629. A sequence of non-blank lines that cannot be interpreted as other
  1630. kinds of blocks forms a [paragraph](@paragraph).
  1631. The contents of the paragraph are the result of parsing the
  1632. paragraph's raw content as inlines. The paragraph's raw content
  1633. is formed by concatenating the lines and removing initial and final
  1634. spaces.
  1635. A simple example with two paragraphs:
  1636. .
  1637. aaa
  1638. bbb
  1639. .
  1640. <p>aaa</p>
  1641. <p>bbb</p>
  1642. .
  1643. Paragraphs can contain multiple lines, but no blank lines:
  1644. .
  1645. aaa
  1646. bbb
  1647. ccc
  1648. ddd
  1649. .
  1650. <p>aaa
  1651. bbb</p>
  1652. <p>ccc
  1653. ddd</p>
  1654. .
  1655. Multiple blank lines between paragraph have no effect:
  1656. .
  1657. aaa
  1658. bbb
  1659. .
  1660. <p>aaa</p>
  1661. <p>bbb</p>
  1662. .
  1663. Leading spaces are skipped:
  1664. .
  1665. aaa
  1666. bbb
  1667. .
  1668. <p>aaa
  1669. bbb</p>
  1670. .
  1671. Lines after the first may be indented any amount, since indented
  1672. code blocks cannot interrupt paragraphs.
  1673. .
  1674. aaa
  1675. bbb
  1676. ccc
  1677. .
  1678. <p>aaa
  1679. bbb
  1680. ccc</p>
  1681. .
  1682. However, the first line may be indented at most three spaces,
  1683. or an indented code block will be triggered:
  1684. .
  1685. aaa
  1686. bbb
  1687. .
  1688. <p>aaa
  1689. bbb</p>
  1690. .
  1691. .
  1692. aaa
  1693. bbb
  1694. .
  1695. <pre><code>aaa
  1696. </code></pre>
  1697. <p>bbb</p>
  1698. .
  1699. Final spaces are stripped before inline parsing, so a paragraph
  1700. that ends with two or more spaces will not end with a [hard line
  1701. break](#hard-line-break):
  1702. .
  1703. aaa
  1704. bbb
  1705. .
  1706. <p>aaa<br />
  1707. bbb</p>
  1708. .
  1709. ## Blank lines
  1710. [Blank lines](#blank-line) between block-level elements are ignored,
  1711. except for the role they play in determining whether a [list](#list)
  1712. is [tight](#tight) or [loose](#loose).
  1713. Blank lines at the beginning and end of the document are also ignored.
  1714. .
  1715. aaa
  1716. # aaa
  1717. .
  1718. <p>aaa</p>
  1719. <h1>aaa</h1>
  1720. .
  1721. # Container blocks
  1722. A [container block](#container-block) is a block that has other
  1723. blocks as its contents. There are two basic kinds of container blocks:
  1724. [block quotes](#block-quote) and [list items](#list-item).
  1725. [Lists](#list) are meta-containers for [list items](#list-item).
  1726. We define the syntax for container blocks recursively. The general
  1727. form of the definition is:
  1728. > If X is a sequence of blocks, then the result of
  1729. > transforming X in such-and-such a way is a container of type Y
  1730. > with these blocks as its content.
  1731. So, we explain what counts as a block quote or list item by explaining
  1732. how these can be *generated* from their contents. This should suffice
  1733. to define the syntax, although it does not give a recipe for *parsing*
  1734. these constructions. (A recipe is provided below in the section entitled
  1735. [A parsing strategy](#appendix-a-a-parsing-strategy).)
  1736. ## Block quotes
  1737. A [block quote marker](@block-quote-marker)
  1738. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  1739. with a following space, or (b) a single character `>` not followed by a space.
  1740. The following rules define [block quotes](@block-quote):
  1741. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  1742. of blocks *Bs*, then the result of prepending a [block quote
  1743. marker](#block-quote-marker) to the beginning of each line in *Ls*
  1744. is a [block quote](#block-quote) containing *Bs*.
  1745. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  1746. quote](#block-quote) with contents *Bs*, then the result of deleting
  1747. the initial [block quote marker](#block-quote-marker) from one or
  1748. more lines in which the next
  1749. [non-space character](#non-space-character) after the [block
  1750. quote marker](#block-quote-marker) is [paragraph continuation
  1751. text](#paragraph-continuation-text) is a block quote with *Bs* as
  1752. its content.
  1753. [Paragraph continuation text](@paragraph-continuation-text) is text
  1754. that will be parsed as part of the content of a paragraph, but does
  1755. not occur at the beginning of the paragraph.
  1756. 3. **Consecutiveness.** A document cannot contain two [block
  1757. quotes](#block-quote) in a row unless there is a [blank
  1758. line](#blank-line) between them.
  1759. Nothing else counts as a [block quote](#block-quote).
  1760. Here is a simple example:
  1761. .
  1762. > # Foo
  1763. > bar
  1764. > baz
  1765. .
  1766. <blockquote>
  1767. <h1>Foo</h1>
  1768. <p>bar
  1769. baz</p>
  1770. </blockquote>
  1771. .
  1772. The spaces after the `>` characters can be omitted:
  1773. .
  1774. ># Foo
  1775. >bar
  1776. > baz
  1777. .
  1778. <blockquote>
  1779. <h1>Foo</h1>
  1780. <p>bar
  1781. baz</p>
  1782. </blockquote>
  1783. .
  1784. The `>` characters can be indented 1-3 spaces:
  1785. .
  1786. > # Foo
  1787. > bar
  1788. > baz
  1789. .
  1790. <blockquote>
  1791. <h1>Foo</h1>
  1792. <p>bar
  1793. baz</p>
  1794. </blockquote>
  1795. .
  1796. Four spaces gives us a code block:
  1797. .
  1798. > # Foo
  1799. > bar
  1800. > baz
  1801. .
  1802. <pre><code>&gt; # Foo
  1803. &gt; bar
  1804. &gt; baz
  1805. </code></pre>
  1806. .
  1807. The Laziness clause allows us to omit the `>` before a
  1808. paragraph continuation line:
  1809. .
  1810. > # Foo
  1811. > bar
  1812. baz
  1813. .
  1814. <blockquote>
  1815. <h1>Foo</h1>
  1816. <p>bar
  1817. baz</p>
  1818. </blockquote>
  1819. .
  1820. A block quote can contain some lazy and some non-lazy
  1821. continuation lines:
  1822. .
  1823. > bar
  1824. baz
  1825. > foo
  1826. .
  1827. <blockquote>
  1828. <p>bar
  1829. baz
  1830. foo</p>
  1831. </blockquote>
  1832. .
  1833. Laziness only applies to lines that are continuations of
  1834. paragraphs. Lines containing characters or indentation that indicate
  1835. block structure cannot be lazy.
  1836. .
  1837. > foo
  1838. ---
  1839. .
  1840. <blockquote>
  1841. <p>foo</p>
  1842. </blockquote>
  1843. <hr />
  1844. .
  1845. .
  1846. > - foo
  1847. - bar
  1848. .
  1849. <blockquote>
  1850. <ul>
  1851. <li>foo</li>
  1852. </ul>
  1853. </blockquote>
  1854. <ul>
  1855. <li>bar</li>
  1856. </ul>
  1857. .
  1858. .
  1859. > foo
  1860. bar
  1861. .
  1862. <blockquote>
  1863. <pre><code>foo
  1864. </code></pre>
  1865. </blockquote>
  1866. <pre><code>bar
  1867. </code></pre>
  1868. .
  1869. .
  1870. > ```
  1871. foo
  1872. ```
  1873. .
  1874. <blockquote>
  1875. <pre><code></code></pre>
  1876. </blockquote>
  1877. <p>foo</p>
  1878. <pre><code></code></pre>
  1879. .
  1880. A block quote can be empty:
  1881. .
  1882. >
  1883. .
  1884. <blockquote>
  1885. </blockquote>
  1886. .
  1887. .
  1888. >
  1889. >
  1890. >
  1891. .
  1892. <blockquote>
  1893. </blockquote>
  1894. .
  1895. A block quote can have initial or final blank lines:
  1896. .
  1897. >
  1898. > foo
  1899. >
  1900. .
  1901. <blockquote>
  1902. <p>foo</p>
  1903. </blockquote>
  1904. .
  1905. A blank line always separates block quotes:
  1906. .
  1907. > foo
  1908. > bar
  1909. .
  1910. <blockquote>
  1911. <p>foo</p>
  1912. </blockquote>
  1913. <blockquote>
  1914. <p>bar</p>
  1915. </blockquote>
  1916. .
  1917. (Most current Markdown implementations, including John Gruber's
  1918. original `Markdown.pl`, will parse this example as a single block quote
  1919. with two paragraphs. But it seems better to allow the author to decide
  1920. whether two block quotes or one are wanted.)
  1921. Consecutiveness means that if we put these block quotes together,
  1922. we get a single block quote:
  1923. .
  1924. > foo
  1925. > bar
  1926. .
  1927. <blockquote>
  1928. <p>foo
  1929. bar</p>
  1930. </blockquote>
  1931. .
  1932. To get a block quote with two paragraphs, use:
  1933. .
  1934. > foo
  1935. >
  1936. > bar
  1937. .
  1938. <blockquote>
  1939. <p>foo</p>
  1940. <p>bar</p>
  1941. </blockquote>
  1942. .
  1943. Block quotes can interrupt paragraphs:
  1944. .
  1945. foo
  1946. > bar
  1947. .
  1948. <p>foo</p>
  1949. <blockquote>
  1950. <p>bar</p>
  1951. </blockquote>
  1952. .
  1953. In general, blank lines are not needed before or after block
  1954. quotes:
  1955. .
  1956. > aaa
  1957. ***
  1958. > bbb
  1959. .
  1960. <blockquote>
  1961. <p>aaa</p>
  1962. </blockquote>
  1963. <hr />
  1964. <blockquote>
  1965. <p>bbb</p>
  1966. </blockquote>
  1967. .
  1968. However, because of laziness, a blank line is needed between
  1969. a block quote and a following paragraph:
  1970. .
  1971. > bar
  1972. baz
  1973. .
  1974. <blockquote>
  1975. <p>bar
  1976. baz</p>
  1977. </blockquote>
  1978. .
  1979. .
  1980. > bar
  1981. baz
  1982. .
  1983. <blockquote>
  1984. <p>bar</p>
  1985. </blockquote>
  1986. <p>baz</p>
  1987. .
  1988. .
  1989. > bar
  1990. >
  1991. baz
  1992. .
  1993. <blockquote>
  1994. <p>bar</p>
  1995. </blockquote>
  1996. <p>baz</p>
  1997. .
  1998. It is a consequence of the Laziness rule that any number
  1999. of initial `>`s may be omitted on a continuation line of a
  2000. nested block quote:
  2001. .
  2002. > > > foo
  2003. bar
  2004. .
  2005. <blockquote>
  2006. <blockquote>
  2007. <blockquote>
  2008. <p>foo
  2009. bar</p>
  2010. </blockquote>
  2011. </blockquote>
  2012. </blockquote>
  2013. .
  2014. .
  2015. >>> foo
  2016. > bar
  2017. >>baz
  2018. .
  2019. <blockquote>
  2020. <blockquote>
  2021. <blockquote>
  2022. <p>foo
  2023. bar
  2024. baz</p>
  2025. </blockquote>
  2026. </blockquote>
  2027. </blockquote>
  2028. .
  2029. When including an indented code block in a block quote,
  2030. remember that the [block quote marker](#block-quote-marker) includes
  2031. both the `>` and a following space. So *five spaces* are needed after
  2032. the `>`:
  2033. .
  2034. > code
  2035. > not code
  2036. .
  2037. <blockquote>
  2038. <pre><code>code
  2039. </code></pre>
  2040. </blockquote>
  2041. <blockquote>
  2042. <p>not code</p>
  2043. </blockquote>
  2044. .
  2045. ## List items
  2046. A [list marker](@list-marker) is a
  2047. [bullet list marker](#bullet-list-marker) or an [ordered list
  2048. marker](#ordered-list-marker).
  2049. A [bullet list marker](@bullet-list-marker)
  2050. is a `-`, `+`, or `*` character.
  2051. An [ordered list marker](@ordered-list-marker)
  2052. is a sequence of one of more digits (`0-9`), followed by either a
  2053. `.` character or a `)` character.
  2054. The following rules define [list items](@list-item):
  2055. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2056. blocks *Bs* starting with a [non-space character](#non-space-character)
  2057. and not separated
  2058. from each other by more than one blank line, and *M* is a list
  2059. marker *M* of width *W* followed by 0 < *N* < 5 spaces, then the result
  2060. of prepending *M* and the following spaces to the first line of
  2061. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2062. list item with *Bs* as its contents. The type of the list item
  2063. (bullet or ordered) is determined by the type of its list marker.
  2064. If the list item is ordered, then it is also assigned a start
  2065. number, based on the ordered list marker.
  2066. For example, let *Ls* be the lines
  2067. .
  2068. A paragraph
  2069. with two lines.
  2070. indented code
  2071. > A block quote.
  2072. .
  2073. <p>A paragraph
  2074. with two lines.</p>
  2075. <pre><code>indented code
  2076. </code></pre>
  2077. <blockquote>
  2078. <p>A block quote.</p>
  2079. </blockquote>
  2080. .
  2081. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2082. that the following is an ordered list item with start number 1,
  2083. and the same contents as *Ls*:
  2084. .
  2085. 1. A paragraph
  2086. with two lines.
  2087. indented code
  2088. > A block quote.
  2089. .
  2090. <ol>
  2091. <li>
  2092. <p>A paragraph
  2093. with two lines.</p>
  2094. <pre><code>indented code
  2095. </code></pre>
  2096. <blockquote>
  2097. <p>A block quote.</p>
  2098. </blockquote>
  2099. </li>
  2100. </ol>
  2101. .
  2102. The most important thing to notice is that the position of
  2103. the text after the list marker determines how much indentation
  2104. is needed in subsequent blocks in the list item. If the list
  2105. marker takes up two spaces, and there are three spaces between
  2106. the list marker and the next nonspace character, then blocks
  2107. must be indented five spaces in order to fall under the list
  2108. item.
  2109. Here are some examples showing how far content must be indented to be
  2110. put under the list item:
  2111. .
  2112. - one
  2113. two
  2114. .
  2115. <ul>
  2116. <li>one</li>
  2117. </ul>
  2118. <p>two</p>
  2119. .
  2120. .
  2121. - one
  2122. two
  2123. .
  2124. <ul>
  2125. <li>
  2126. <p>one</p>
  2127. <p>two</p>
  2128. </li>
  2129. </ul>
  2130. .
  2131. .
  2132. - one
  2133. two
  2134. .
  2135. <ul>
  2136. <li>one</li>
  2137. </ul>
  2138. <pre><code> two
  2139. </code></pre>
  2140. .
  2141. .
  2142. - one
  2143. two
  2144. .
  2145. <ul>
  2146. <li>
  2147. <p>one</p>
  2148. <p>two</p>
  2149. </li>
  2150. </ul>
  2151. .
  2152. It is tempting to think of this in terms of columns: the continuation
  2153. blocks must be indented at least to the column of the first nonspace
  2154. character after the list marker. However, that is not quite right.
  2155. The spaces after the list marker determine how much relative indentation
  2156. is needed. Which column this indentation reaches will depend on
  2157. how the list item is embedded in other constructions, as shown by
  2158. this example:
  2159. .
  2160. > > 1. one
  2161. >>
  2162. >> two
  2163. .
  2164. <blockquote>
  2165. <blockquote>
  2166. <ol>
  2167. <li>
  2168. <p>one</p>
  2169. <p>two</p>
  2170. </li>
  2171. </ol>
  2172. </blockquote>
  2173. </blockquote>
  2174. .
  2175. Here `two` occurs in the same column as the list marker `1.`,
  2176. but is actually contained in the list item, because there is
  2177. sufficent indentation after the last containing blockquote marker.
  2178. The converse is also possible. In the following example, the word `two`
  2179. occurs far to the right of the initial text of the list item, `one`, but
  2180. it is not considered part of the list item, because it is not indented
  2181. far enough past the blockquote marker:
  2182. .
  2183. >>- one
  2184. >>
  2185. > > two
  2186. .
  2187. <blockquote>
  2188. <blockquote>
  2189. <ul>
  2190. <li>one</li>
  2191. </ul>
  2192. <p>two</p>
  2193. </blockquote>
  2194. </blockquote>
  2195. .
  2196. A list item may not contain blocks that are separated by more than
  2197. one blank line. Thus, two blank lines will end a list, unless the
  2198. two blanks are contained in a [fenced code block](#fenced-code-block).
  2199. .
  2200. - foo
  2201. bar
  2202. - foo
  2203. bar
  2204. - ```
  2205. foo
  2206. bar
  2207. ```
  2208. .
  2209. <ul>
  2210. <li>
  2211. <p>foo</p>
  2212. <p>bar</p>
  2213. </li>
  2214. <li>
  2215. <p>foo</p>
  2216. </li>
  2217. </ul>
  2218. <p>bar</p>
  2219. <ul>
  2220. <li>
  2221. <pre><code>foo
  2222. bar
  2223. </code></pre>
  2224. </li>
  2225. </ul>
  2226. .
  2227. A list item may contain any kind of block:
  2228. .
  2229. 1. foo
  2230. ```
  2231. bar
  2232. ```
  2233. baz
  2234. > bam
  2235. .
  2236. <ol>
  2237. <li>
  2238. <p>foo</p>
  2239. <pre><code>bar
  2240. </code></pre>
  2241. <p>baz</p>
  2242. <blockquote>
  2243. <p>bam</p>
  2244. </blockquote>
  2245. </li>
  2246. </ol>
  2247. .
  2248. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2249. constitute a sequence of blocks *Bs* starting with an indented code
  2250. block and not separated from each other by more than one blank line,
  2251. and *M* is a list marker *M* of width *W* followed by
  2252. one space, then the result of prepending *M* and the following
  2253. space to the first line of *Ls*, and indenting subsequent lines of
  2254. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2255. If a line is empty, then it need not be indented. The type of the
  2256. list item (bullet or ordered) is determined by the type of its list
  2257. marker. If the list item is ordered, then it is also assigned a
  2258. start number, based on the ordered list marker.
  2259. An indented code block will have to be indented four spaces beyond
  2260. the edge of the region where text will be included in the list item.
  2261. In the following case that is 6 spaces:
  2262. .
  2263. - foo
  2264. bar
  2265. .
  2266. <ul>
  2267. <li>
  2268. <p>foo</p>
  2269. <pre><code>bar
  2270. </code></pre>
  2271. </li>
  2272. </ul>
  2273. .
  2274. And in this case it is 11 spaces:
  2275. .
  2276. 10. foo
  2277. bar
  2278. .
  2279. <ol start="10">
  2280. <li>
  2281. <p>foo</p>
  2282. <pre><code>bar
  2283. </code></pre>
  2284. </li>
  2285. </ol>
  2286. .
  2287. If the *first* block in the list item is an indented code block,
  2288. then by rule #2, the contents must be indented *one* space after the
  2289. list marker:
  2290. .
  2291. indented code
  2292. paragraph
  2293. more code
  2294. .
  2295. <pre><code>indented code
  2296. </code></pre>
  2297. <p>paragraph</p>
  2298. <pre><code>more code
  2299. </code></pre>
  2300. .
  2301. .
  2302. 1. indented code
  2303. paragraph
  2304. more code
  2305. .
  2306. <ol>
  2307. <li>
  2308. <pre><code>indented code
  2309. </code></pre>
  2310. <p>paragraph</p>
  2311. <pre><code>more code
  2312. </code></pre>
  2313. </li>
  2314. </ol>
  2315. .
  2316. Note that an additional space indent is interpreted as space
  2317. inside the code block:
  2318. .
  2319. 1. indented code
  2320. paragraph
  2321. more code
  2322. .
  2323. <ol>
  2324. <li>
  2325. <pre><code> indented code
  2326. </code></pre>
  2327. <p>paragraph</p>
  2328. <pre><code>more code
  2329. </code></pre>
  2330. </li>
  2331. </ol>
  2332. .
  2333. Note that rules #1 and #2 only apply to two cases: (a) cases
  2334. in which the lines to be included in a list item begin with a nonspace
  2335. character, and (b) cases in which they begin with an indented code
  2336. block. In a case like the following, where the first block begins with
  2337. a three-space indent, the rules do not allow us to form a list item by
  2338. indenting the whole thing and prepending a list marker:
  2339. .
  2340. foo
  2341. bar
  2342. .
  2343. <p>foo</p>
  2344. <p>bar</p>
  2345. .
  2346. .
  2347. - foo
  2348. bar
  2349. .
  2350. <ul>
  2351. <li>foo</li>
  2352. </ul>
  2353. <p>bar</p>
  2354. .
  2355. This is not a significant restriction, because when a block begins
  2356. with 1-3 spaces indent, the indentation can always be removed without
  2357. a change in interpretation, allowing rule #1 to be applied. So, in
  2358. the above case:
  2359. .
  2360. - foo
  2361. bar
  2362. .
  2363. <ul>
  2364. <li>
  2365. <p>foo</p>
  2366. <p>bar</p>
  2367. </li>
  2368. </ul>
  2369. .
  2370. 3. **Empty list item.** A [list marker](#list-marker) followed by a
  2371. line containing only [whitespace](#whitespace) is a list item with
  2372. no contents.
  2373. Here is an empty bullet list item:
  2374. .
  2375. - foo
  2376. -
  2377. - bar
  2378. .
  2379. <ul>
  2380. <li>foo</li>
  2381. <li></li>
  2382. <li>bar</li>
  2383. </ul>
  2384. .
  2385. It does not matter whether there are spaces following the
  2386. [list marker](#list-marker):
  2387. .
  2388. - foo
  2389. -
  2390. - bar
  2391. .
  2392. <ul>
  2393. <li>foo</li>
  2394. <li></li>
  2395. <li>bar</li>
  2396. </ul>
  2397. .
  2398. Here is an empty ordered list item:
  2399. .
  2400. 1. foo
  2401. 2.
  2402. 3. bar
  2403. .
  2404. <ol>
  2405. <li>foo</li>
  2406. <li></li>
  2407. <li>bar</li>
  2408. </ol>
  2409. .
  2410. A list may start or end with an empty list item:
  2411. .
  2412. *
  2413. .
  2414. <ul>
  2415. <li></li>
  2416. </ul>
  2417. .
  2418. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2419. according to rule #1, #2, or #3, then the result of indenting each line
  2420. of *L* by 1-3 spaces (the same for each line) also constitutes a
  2421. list item with the same contents and attributes. If a line is
  2422. empty, then it need not be indented.
  2423. Indented one space:
  2424. .
  2425. 1. A paragraph
  2426. with two lines.
  2427. indented code
  2428. > A block quote.
  2429. .
  2430. <ol>
  2431. <li>
  2432. <p>A paragraph
  2433. with two lines.</p>
  2434. <pre><code>indented code
  2435. </code></pre>
  2436. <blockquote>
  2437. <p>A block quote.</p>
  2438. </blockquote>
  2439. </li>
  2440. </ol>
  2441. .
  2442. Indented two spaces:
  2443. .
  2444. 1. A paragraph
  2445. with two lines.
  2446. indented code
  2447. > A block quote.
  2448. .
  2449. <ol>
  2450. <li>
  2451. <p>A paragraph
  2452. with two lines.</p>
  2453. <pre><code>indented code
  2454. </code></pre>
  2455. <blockquote>
  2456. <p>A block quote.</p>
  2457. </blockquote>
  2458. </li>
  2459. </ol>
  2460. .
  2461. Indented three spaces:
  2462. .
  2463. 1. A paragraph
  2464. with two lines.
  2465. indented code
  2466. > A block quote.
  2467. .
  2468. <ol>
  2469. <li>
  2470. <p>A paragraph
  2471. with two lines.</p>
  2472. <pre><code>indented code
  2473. </code></pre>
  2474. <blockquote>
  2475. <p>A block quote.</p>
  2476. </blockquote>
  2477. </li>
  2478. </ol>
  2479. .
  2480. Four spaces indent gives a code block:
  2481. .
  2482. 1. A paragraph
  2483. with two lines.
  2484. indented code
  2485. > A block quote.
  2486. .
  2487. <pre><code>1. A paragraph
  2488. with two lines.
  2489. indented code
  2490. &gt; A block quote.
  2491. </code></pre>
  2492. .
  2493. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  2494. item](#list-item) with contents *Bs*, then the result of deleting
  2495. some or all of the indentation from one or more lines in which the
  2496. next [non-space character](#non-space-character) after the indentation is
  2497. [paragraph continuation text](#paragraph-continuation-text) is a
  2498. list item with the same contents and attributes. The unindented
  2499. lines are called
  2500. [lazy continuation lines](@lazy-continuation-line).
  2501. Here is an example with [lazy continuation
  2502. lines](#lazy-continuation-line):
  2503. .
  2504. 1. A paragraph
  2505. with two lines.
  2506. indented code
  2507. > A block quote.
  2508. .
  2509. <ol>
  2510. <li>
  2511. <p>A paragraph
  2512. with two lines.</p>
  2513. <pre><code>indented code
  2514. </code></pre>
  2515. <blockquote>
  2516. <p>A block quote.</p>
  2517. </blockquote>
  2518. </li>
  2519. </ol>
  2520. .
  2521. Indentation can be partially deleted:
  2522. .
  2523. 1. A paragraph
  2524. with two lines.
  2525. .
  2526. <ol>
  2527. <li>A paragraph
  2528. with two lines.</li>
  2529. </ol>
  2530. .
  2531. These examples show how laziness can work in nested structures:
  2532. .
  2533. > 1. > Blockquote
  2534. continued here.
  2535. .
  2536. <blockquote>
  2537. <ol>
  2538. <li>
  2539. <blockquote>
  2540. <p>Blockquote
  2541. continued here.</p>
  2542. </blockquote>
  2543. </li>
  2544. </ol>
  2545. </blockquote>
  2546. .
  2547. .
  2548. > 1. > Blockquote
  2549. > continued here.
  2550. .
  2551. <blockquote>
  2552. <ol>
  2553. <li>
  2554. <blockquote>
  2555. <p>Blockquote
  2556. continued here.</p>
  2557. </blockquote>
  2558. </li>
  2559. </ol>
  2560. </blockquote>
  2561. .
  2562. 6. **That's all.** Nothing that is not counted as a list item by rules
  2563. #1--5 counts as a [list item](#list-item).
  2564. The rules for sublists follow from the general rules above. A sublist
  2565. must be indented the same number of spaces a paragraph would need to be
  2566. in order to be included in the list item.
  2567. So, in this case we need two spaces indent:
  2568. .
  2569. - foo
  2570. - bar
  2571. - baz
  2572. .
  2573. <ul>
  2574. <li>foo
  2575. <ul>
  2576. <li>bar
  2577. <ul>
  2578. <li>baz</li>
  2579. </ul>
  2580. </li>
  2581. </ul>
  2582. </li>
  2583. </ul>
  2584. .
  2585. One is not enough:
  2586. .
  2587. - foo
  2588. - bar
  2589. - baz
  2590. .
  2591. <ul>
  2592. <li>foo</li>
  2593. <li>bar</li>
  2594. <li>baz</li>
  2595. </ul>
  2596. .
  2597. Here we need four, because the list marker is wider:
  2598. .
  2599. 10) foo
  2600. - bar
  2601. .
  2602. <ol start="10">
  2603. <li>foo
  2604. <ul>
  2605. <li>bar</li>
  2606. </ul>
  2607. </li>
  2608. </ol>
  2609. .
  2610. Three is not enough:
  2611. .
  2612. 10) foo
  2613. - bar
  2614. .
  2615. <ol start="10">
  2616. <li>foo</li>
  2617. </ol>
  2618. <ul>
  2619. <li>bar</li>
  2620. </ul>
  2621. .
  2622. A list may be the first block in a list item:
  2623. .
  2624. - - foo
  2625. .
  2626. <ul>
  2627. <li>
  2628. <ul>
  2629. <li>foo</li>
  2630. </ul>
  2631. </li>
  2632. </ul>
  2633. .
  2634. .
  2635. 1. - 2. foo
  2636. .
  2637. <ol>
  2638. <li>
  2639. <ul>
  2640. <li>
  2641. <ol start="2">
  2642. <li>foo</li>
  2643. </ol>
  2644. </li>
  2645. </ul>
  2646. </li>
  2647. </ol>
  2648. .
  2649. A list item can contain a header:
  2650. .
  2651. - # Foo
  2652. - Bar
  2653. ---
  2654. baz
  2655. .
  2656. <ul>
  2657. <li>
  2658. <h1>Foo</h1>
  2659. </li>
  2660. <li>
  2661. <h2>Bar</h2>
  2662. baz</li>
  2663. </ul>
  2664. .
  2665. ### Motivation
  2666. John Gruber's Markdown spec says the following about list items:
  2667. 1. "List markers typically start at the left margin, but may be indented
  2668. by up to three spaces. List markers must be followed by one or more
  2669. spaces or a tab."
  2670. 2. "To make lists look nice, you can wrap items with hanging indents....
  2671. But if you don't want to, you don't have to."
  2672. 3. "List items may consist of multiple paragraphs. Each subsequent
  2673. paragraph in a list item must be indented by either 4 spaces or one
  2674. tab."
  2675. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2676. but here again, Markdown will allow you to be lazy."
  2677. 5. "To put a blockquote within a list item, the blockquote's `>`
  2678. delimiters need to be indented."
  2679. 6. "To put a code block within a list item, the code block needs to be
  2680. indented twice — 8 spaces or two tabs."
  2681. These rules specify that a paragraph under a list item must be indented
  2682. four spaces (presumably, from the left margin, rather than the start of
  2683. the list marker, but this is not said), and that code under a list item
  2684. must be indented eight spaces instead of the usual four. They also say
  2685. that a block quote must be indented, but not by how much; however, the
  2686. example given has four spaces indentation. Although nothing is said
  2687. about other kinds of block-level content, it is certainly reasonable to
  2688. infer that *all* block elements under a list item, including other
  2689. lists, must be indented four spaces. This principle has been called the
  2690. *four-space rule*.
  2691. The four-space rule is clear and principled, and if the reference
  2692. implementation `Markdown.pl` had followed it, it probably would have
  2693. become the standard. However, `Markdown.pl` allowed paragraphs and
  2694. sublists to start with only two spaces indentation, at least on the
  2695. outer level. Worse, its behavior was inconsistent: a sublist of an
  2696. outer-level list needed two spaces indentation, but a sublist of this
  2697. sublist needed three spaces. It is not surprising, then, that different
  2698. implementations of Markdown have developed very different rules for
  2699. determining what comes under a list item. (Pandoc and python-Markdown,
  2700. for example, stuck with Gruber's syntax description and the four-space
  2701. rule, while discount, redcarpet, marked, PHP Markdown, and others
  2702. followed `Markdown.pl`'s behavior more closely.)
  2703. Unfortunately, given the divergences between implementations, there
  2704. is no way to give a spec for list items that will be guaranteed not
  2705. to break any existing documents. However, the spec given here should
  2706. correctly handle lists formatted with either the four-space rule or
  2707. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2708. in a way that is natural for a human to read.
  2709. The strategy here is to let the width and indentation of the list marker
  2710. determine the indentation necessary for blocks to fall under the list
  2711. item, rather than having a fixed and arbitrary number. The writer can
  2712. think of the body of the list item as a unit which gets indented to the
  2713. right enough to fit the list marker (and any indentation on the list
  2714. marker). (The laziness rule, #5, then allows continuation lines to be
  2715. unindented if needed.)
  2716. This rule is superior, we claim, to any rule requiring a fixed level of
  2717. indentation from the margin. The four-space rule is clear but
  2718. unnatural. It is quite unintuitive that
  2719. ``` markdown
  2720. - foo
  2721. bar
  2722. - baz
  2723. ```
  2724. should be parsed as two lists with an intervening paragraph,
  2725. ``` html
  2726. <ul>
  2727. <li>foo</li>
  2728. </ul>
  2729. <p>bar</p>
  2730. <ul>
  2731. <li>baz</li>
  2732. </ul>
  2733. ```
  2734. as the four-space rule demands, rather than a single list,
  2735. ``` html
  2736. <ul>
  2737. <li>
  2738. <p>foo</p>
  2739. <p>bar</p>
  2740. <ul>
  2741. <li>baz</li>
  2742. </ul>
  2743. </li>
  2744. </ul>
  2745. ```
  2746. The choice of four spaces is arbitrary. It can be learned, but it is
  2747. not likely to be guessed, and it trips up beginners regularly.
  2748. Would it help to adopt a two-space rule? The problem is that such
  2749. a rule, together with the rule allowing 1--3 spaces indentation of the
  2750. initial list marker, allows text that is indented *less than* the
  2751. original list marker to be included in the list item. For example,
  2752. `Markdown.pl` parses
  2753. ``` markdown
  2754. - one
  2755. two
  2756. ```
  2757. as a single list item, with `two` a continuation paragraph:
  2758. ``` html
  2759. <ul>
  2760. <li>
  2761. <p>one</p>
  2762. <p>two</p>
  2763. </li>
  2764. </ul>
  2765. ```
  2766. and similarly
  2767. ``` markdown
  2768. > - one
  2769. >
  2770. > two
  2771. ```
  2772. as
  2773. ``` html
  2774. <blockquote>
  2775. <ul>
  2776. <li>
  2777. <p>one</p>
  2778. <p>two</p>
  2779. </li>
  2780. </ul>
  2781. </blockquote>
  2782. ```
  2783. This is extremely unintuitive.
  2784. Rather than requiring a fixed indent from the margin, we could require
  2785. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2786. may itself be indented). This proposal would remove the last anomaly
  2787. discussed. Unlike the spec presented above, it would count the following
  2788. as a list item with a subparagraph, even though the paragraph `bar`
  2789. is not indented as far as the first paragraph `foo`:
  2790. ``` markdown
  2791. 10. foo
  2792. bar
  2793. ```
  2794. Arguably this text does read like a list item with `bar` as a subparagraph,
  2795. which may count in favor of the proposal. However, on this proposal indented
  2796. code would have to be indented six spaces after the list marker. And this
  2797. would break a lot of existing Markdown, which has the pattern:
  2798. ``` markdown
  2799. 1. foo
  2800. indented code
  2801. ```
  2802. where the code is indented eight spaces. The spec above, by contrast, will
  2803. parse this text as expected, since the code block's indentation is measured
  2804. from the beginning of `foo`.
  2805. The one case that needs special treatment is a list item that *starts*
  2806. with indented code. How much indentation is required in that case, since
  2807. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2808. that in such cases, we require one space indentation from the list marker
  2809. (and then the normal four spaces for the indented code). This will match the
  2810. four-space rule in cases where the list marker plus its initial indentation
  2811. takes four spaces (a common case), but diverge in other cases.
  2812. ## Lists
  2813. A [list](@list) is a sequence of one or more
  2814. list items [of the same type](#of-the-same-type). The list items
  2815. may be separated by single [blank lines](#blank-line), but two
  2816. blank lines end all containing lists.
  2817. Two list items are [of the same type](@of-the-same-type)
  2818. if they begin with a [list
  2819. marker](#list-marker) of the same type. Two list markers are of the
  2820. same type if (a) they are bullet list markers using the same character
  2821. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  2822. delimiter (either `.` or `)`).
  2823. A list is an [ordered list](@ordered-list)
  2824. if its constituent list items begin with
  2825. [ordered list markers](#ordered-list-marker), and a [bullet
  2826. list](@bullet-list) if its constituent list
  2827. items begin with [bullet list markers](#bullet-list-marker).
  2828. The [start number](@start-number)
  2829. of an [ordered list](#ordered-list) is determined by the list number of
  2830. its initial list item. The numbers of subsequent list items are
  2831. disregarded.
  2832. A list is [loose](@loose) if it any of its constituent
  2833. list items are separated by blank lines, or if any of its constituent
  2834. list items directly contain two block-level elements with a blank line
  2835. between them. Otherwise a list is [tight](@tight).
  2836. (The difference in HTML output is that paragraphs in a loose list are
  2837. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  2838. Changing the bullet or ordered list delimiter starts a new list:
  2839. .
  2840. - foo
  2841. - bar
  2842. + baz
  2843. .
  2844. <ul>
  2845. <li>foo</li>
  2846. <li>bar</li>
  2847. </ul>
  2848. <ul>
  2849. <li>baz</li>
  2850. </ul>
  2851. .
  2852. .
  2853. 1. foo
  2854. 2. bar
  2855. 3) baz
  2856. .
  2857. <ol>
  2858. <li>foo</li>
  2859. <li>bar</li>
  2860. </ol>
  2861. <ol start="3">
  2862. <li>baz</li>
  2863. </ol>
  2864. .
  2865. In CommonMark, a list can interrupt a paragraph. That is,
  2866. no blank line is needed to separate a paragraph from a following
  2867. list:
  2868. .
  2869. Foo
  2870. - bar
  2871. - baz
  2872. .
  2873. <p>Foo</p>
  2874. <ul>
  2875. <li>bar</li>
  2876. <li>baz</li>
  2877. </ul>
  2878. .
  2879. `Markdown.pl` does not allow this, through fear of triggering a list
  2880. via a numeral in a hard-wrapped line:
  2881. .
  2882. The number of windows in my house is
  2883. 14. The number of doors is 6.
  2884. .
  2885. <p>The number of windows in my house is</p>
  2886. <ol start="14">
  2887. <li>The number of doors is 6.</li>
  2888. </ol>
  2889. .
  2890. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  2891. even though the same considerations might apply. We think that the two
  2892. cases should be treated the same. Here are two reasons for allowing
  2893. lists to interrupt paragraphs:
  2894. First, it is natural and not uncommon for people to start lists without
  2895. blank lines:
  2896. I need to buy
  2897. - new shoes
  2898. - a coat
  2899. - a plane ticket
  2900. Second, we are attracted to a
  2901. > [principle of uniformity](@principle-of-uniformity):
  2902. > if a chunk of text has a certain
  2903. > meaning, it will continue to have the same meaning when put into a
  2904. > container block (such as a list item or blockquote).
  2905. (Indeed, the spec for [list items](#list-item) and
  2906. [blockquotes](#block-quotes) presupposes this principle.)
  2907. This principle implies that if
  2908. * I need to buy
  2909. - new shoes
  2910. - a coat
  2911. - a plane ticket
  2912. is a list item containing a paragraph followed by a nested sublist,
  2913. as all Markdown implementations agree it is (though the paragraph
  2914. may be rendered without `<p>` tags, since the list is "tight"),
  2915. then
  2916. I need to buy
  2917. - new shoes
  2918. - a coat
  2919. - a plane ticket
  2920. by itself should be a paragraph followed by a nested sublist.
  2921. Our adherence to the [principle of uniformity](#principle-of-uniformity)
  2922. thus inclines us to think that there are two coherent packages:
  2923. 1. Require blank lines before *all* lists and blockquotes,
  2924. including lists that occur as sublists inside other list items.
  2925. 2. Require blank lines in none of these places.
  2926. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  2927. the first approach, for which there is much to be said. But the second
  2928. seems more consistent with established practice with Markdown.
  2929. There can be blank lines between items, but two blank lines end
  2930. a list:
  2931. .
  2932. - foo
  2933. - bar
  2934. - baz
  2935. .
  2936. <ul>
  2937. <li>
  2938. <p>foo</p>
  2939. </li>
  2940. <li>
  2941. <p>bar</p>
  2942. </li>
  2943. </ul>
  2944. <ul>
  2945. <li>baz</li>
  2946. </ul>
  2947. .
  2948. As illustrated above in the section on [list items](#list-item),
  2949. two blank lines between blocks *within* a list item will also end a
  2950. list:
  2951. .
  2952. - foo
  2953. bar
  2954. - baz
  2955. .
  2956. <ul>
  2957. <li>foo</li>
  2958. </ul>
  2959. <p>bar</p>
  2960. <ul>
  2961. <li>baz</li>
  2962. </ul>
  2963. .
  2964. Indeed, two blank lines will end *all* containing lists:
  2965. .
  2966. - foo
  2967. - bar
  2968. - baz
  2969. bim
  2970. .
  2971. <ul>
  2972. <li>foo
  2973. <ul>
  2974. <li>bar
  2975. <ul>
  2976. <li>baz</li>
  2977. </ul>
  2978. </li>
  2979. </ul>
  2980. </li>
  2981. </ul>
  2982. <pre><code> bim
  2983. </code></pre>
  2984. .
  2985. Thus, two blank lines can be used to separate consecutive lists of
  2986. the same type, or to separate a list from an indented code block
  2987. that would otherwise be parsed as a subparagraph of the final list
  2988. item:
  2989. .
  2990. - foo
  2991. - bar
  2992. - baz
  2993. - bim
  2994. .
  2995. <ul>
  2996. <li>foo</li>
  2997. <li>bar</li>
  2998. </ul>
  2999. <ul>
  3000. <li>baz</li>
  3001. <li>bim</li>
  3002. </ul>
  3003. .
  3004. .
  3005. - foo
  3006. notcode
  3007. - foo
  3008. code
  3009. .
  3010. <ul>
  3011. <li>
  3012. <p>foo</p>
  3013. <p>notcode</p>
  3014. </li>
  3015. <li>
  3016. <p>foo</p>
  3017. </li>
  3018. </ul>
  3019. <pre><code>code
  3020. </code></pre>
  3021. .
  3022. List items need not be indented to the same level. The following
  3023. list items will be treated as items at the same list level,
  3024. since none is indented enough to belong to the previous list
  3025. item:
  3026. .
  3027. - a
  3028. - b
  3029. - c
  3030. - d
  3031. - e
  3032. - f
  3033. - g
  3034. .
  3035. <ul>
  3036. <li>a</li>
  3037. <li>b</li>
  3038. <li>c</li>
  3039. <li>d</li>
  3040. <li>e</li>
  3041. <li>f</li>
  3042. <li>g</li>
  3043. </ul>
  3044. .
  3045. This is a loose list, because there is a blank line between
  3046. two of the list items:
  3047. .
  3048. - a
  3049. - b
  3050. - c
  3051. .
  3052. <ul>
  3053. <li>
  3054. <p>a</p>
  3055. </li>
  3056. <li>
  3057. <p>b</p>
  3058. </li>
  3059. <li>
  3060. <p>c</p>
  3061. </li>
  3062. </ul>
  3063. .
  3064. So is this, with a empty second item:
  3065. .
  3066. * a
  3067. *
  3068. * c
  3069. .
  3070. <ul>
  3071. <li>
  3072. <p>a</p>
  3073. </li>
  3074. <li></li>
  3075. <li>
  3076. <p>c</p>
  3077. </li>
  3078. </ul>
  3079. .
  3080. These are loose lists, even though there is no space between the items,
  3081. because one of the items directly contains two block-level elements
  3082. with a blank line between them:
  3083. .
  3084. - a
  3085. - b
  3086. c
  3087. - d
  3088. .
  3089. <ul>
  3090. <li>
  3091. <p>a</p>
  3092. </li>
  3093. <li>
  3094. <p>b</p>
  3095. <p>c</p>
  3096. </li>
  3097. <li>
  3098. <p>d</p>
  3099. </li>
  3100. </ul>
  3101. .
  3102. .
  3103. - a
  3104. - b
  3105. [ref]: /url
  3106. - d
  3107. .
  3108. <ul>
  3109. <li>
  3110. <p>a</p>
  3111. </li>
  3112. <li>
  3113. <p>b</p>
  3114. </li>
  3115. <li>
  3116. <p>d</p>
  3117. </li>
  3118. </ul>
  3119. .
  3120. This is a tight list, because the blank lines are in a code block:
  3121. .
  3122. - a
  3123. - ```
  3124. b
  3125. ```
  3126. - c
  3127. .
  3128. <ul>
  3129. <li>a</li>
  3130. <li>
  3131. <pre><code>b
  3132. </code></pre>
  3133. </li>
  3134. <li>c</li>
  3135. </ul>
  3136. .
  3137. This is a tight list, because the blank line is between two
  3138. paragraphs of a sublist. So the sublist is loose while
  3139. the outer list is tight:
  3140. .
  3141. - a
  3142. - b
  3143. c
  3144. - d
  3145. .
  3146. <ul>
  3147. <li>a
  3148. <ul>
  3149. <li>
  3150. <p>b</p>
  3151. <p>c</p>
  3152. </li>
  3153. </ul>
  3154. </li>
  3155. <li>d</li>
  3156. </ul>
  3157. .
  3158. This is a tight list, because the blank line is inside the
  3159. block quote:
  3160. .
  3161. * a
  3162. > b
  3163. >
  3164. * c
  3165. .
  3166. <ul>
  3167. <li>a
  3168. <blockquote>
  3169. <p>b</p>
  3170. </blockquote>
  3171. </li>
  3172. <li>c</li>
  3173. </ul>
  3174. .
  3175. This list is tight, because the consecutive block elements
  3176. are not separated by blank lines:
  3177. .
  3178. - a
  3179. > b
  3180. ```
  3181. c
  3182. ```
  3183. - d
  3184. .
  3185. <ul>
  3186. <li>a
  3187. <blockquote>
  3188. <p>b</p>
  3189. </blockquote>
  3190. <pre><code>c
  3191. </code></pre>
  3192. </li>
  3193. <li>d</li>
  3194. </ul>
  3195. .
  3196. A single-paragraph list is tight:
  3197. .
  3198. - a
  3199. .
  3200. <ul>
  3201. <li>a</li>
  3202. </ul>
  3203. .
  3204. .
  3205. - a
  3206. - b
  3207. .
  3208. <ul>
  3209. <li>a
  3210. <ul>
  3211. <li>b</li>
  3212. </ul>
  3213. </li>
  3214. </ul>
  3215. .
  3216. Here the outer list is loose, the inner list tight:
  3217. .
  3218. * foo
  3219. * bar
  3220. baz
  3221. .
  3222. <ul>
  3223. <li>
  3224. <p>foo</p>
  3225. <ul>
  3226. <li>bar</li>
  3227. </ul>
  3228. <p>baz</p>
  3229. </li>
  3230. </ul>
  3231. .
  3232. .
  3233. - a
  3234. - b
  3235. - c
  3236. - d
  3237. - e
  3238. - f
  3239. .
  3240. <ul>
  3241. <li>
  3242. <p>a</p>
  3243. <ul>
  3244. <li>b</li>
  3245. <li>c</li>
  3246. </ul>
  3247. </li>
  3248. <li>
  3249. <p>d</p>
  3250. <ul>
  3251. <li>e</li>
  3252. <li>f</li>
  3253. </ul>
  3254. </li>
  3255. </ul>
  3256. .
  3257. # Inlines
  3258. Inlines are parsed sequentially from the beginning of the character
  3259. stream to the end (left to right, in left-to-right languages).
  3260. Thus, for example, in
  3261. .
  3262. `hi`lo`
  3263. .
  3264. <p><code>hi</code>lo`</p>
  3265. .
  3266. `hi` is parsed as code, leaving the backtick at the end as a literal
  3267. backtick.
  3268. ## Backslash escapes
  3269. Any ASCII punctuation character may be backslash-escaped:
  3270. .
  3271. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3272. .
  3273. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3274. .
  3275. Backslashes before other characters are treated as literal
  3276. backslashes:
  3277. .
  3278. \→\A\a\ \3\φ\«
  3279. .
  3280. <p>\ \A\a\ \3\φ\«</p>
  3281. .
  3282. Escaped characters are treated as regular characters and do
  3283. not have their usual Markdown meanings:
  3284. .
  3285. \*not emphasized*
  3286. \<br/> not a tag
  3287. \[not a link](/foo)
  3288. \`not code`
  3289. 1\. not a list
  3290. \* not a list
  3291. \# not a header
  3292. \[foo]: /url "not a reference"
  3293. .
  3294. <p>*not emphasized*
  3295. &lt;br/&gt; not a tag
  3296. [not a link](/foo)
  3297. `not code`
  3298. 1. not a list
  3299. * not a list
  3300. # not a header
  3301. [foo]: /url &quot;not a reference&quot;</p>
  3302. .
  3303. If a backslash is itself escaped, the following character is not:
  3304. .
  3305. \\*emphasis*
  3306. .
  3307. <p>\<em>emphasis</em></p>
  3308. .
  3309. A backslash at the end of the line is a [hard line
  3310. break](#hard-line-break):
  3311. .
  3312. foo\
  3313. bar
  3314. .
  3315. <p>foo<br />
  3316. bar</p>
  3317. .
  3318. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3319. raw HTML:
  3320. .
  3321. `` \[\` ``
  3322. .
  3323. <p><code>\[\`</code></p>
  3324. .
  3325. .
  3326. \[\]
  3327. .
  3328. <pre><code>\[\]
  3329. </code></pre>
  3330. .
  3331. .
  3332. ~~~
  3333. \[\]
  3334. ~~~
  3335. .
  3336. <pre><code>\[\]
  3337. </code></pre>
  3338. .
  3339. .
  3340. <http://example.com?find=\*>
  3341. .
  3342. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3343. .
  3344. .
  3345. <a href="/bar\/)">
  3346. .
  3347. <p><a href="/bar\/)"></p>
  3348. .
  3349. But they work in all other contexts, including URLs and link titles,
  3350. link references, and info strings in [fenced code
  3351. blocks](#fenced-code-block):
  3352. .
  3353. [foo](/bar\* "ti\*tle")
  3354. .
  3355. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3356. .
  3357. .
  3358. [foo]
  3359. [foo]: /bar\* "ti\*tle"
  3360. .
  3361. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3362. .
  3363. .
  3364. ``` foo\+bar
  3365. foo
  3366. ```
  3367. .
  3368. <pre><code class="language-foo+bar">foo
  3369. </code></pre>
  3370. .
  3371. ## Entities
  3372. With the goal of making this standard as HTML-agnostic as possible, all
  3373. valid HTML entities in any context are recognized as such and
  3374. converted into unicode characters before they are stored in the AST.
  3375. This allows implementations that target HTML output to trivially escape
  3376. the entities when generating HTML, and simplifies the job of
  3377. implementations targetting other languages, as these will only need to
  3378. handle the unicode chars and need not be HTML-entity aware.
  3379. [Named entities](@name-entities) consist of `&`
  3380. + any of the valid HTML5 entity names + `;`. The
  3381. [following document](https://html.spec.whatwg.org/multipage/entities.json)
  3382. is used as an authoritative source of the valid entity names and their
  3383. corresponding codepoints.
  3384. Conforming implementations that target HTML don't need to generate
  3385. entities for all the valid named entities that exist, with the exception
  3386. of `"` (`&quot;`), `&` (`&amp;`), `<` (`&lt;`) and `>` (`&gt;`), which
  3387. always need to be written as entities for security reasons.
  3388. .
  3389. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3390. .
  3391. <p>  &amp; © Æ Ď ¾ ℋ ⅆ ∲</p>
  3392. .
  3393. [Decimal entities](@decimal-entities)
  3394. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3395. entities need to be recognised and tranformed into their corresponding
  3396. UTF8 codepoints. Invalid Unicode codepoints will be written as the
  3397. "unknown codepoint" character (`0xFFFD`)
  3398. .
  3399. &#35; &#1234; &#992; &#98765432;
  3400. .
  3401. <p># Ӓ Ϡ �</p>
  3402. .
  3403. [Hexadecimal entities](@hexadecimal-entities)
  3404. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3405. + `;`. They will also be parsed and turned into their corresponding UTF8 values in the AST.
  3406. .
  3407. &#X22; &#XD06; &#xcab;
  3408. .
  3409. <p>&quot; ആ ಫ</p>
  3410. .
  3411. Here are some nonentities:
  3412. .
  3413. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3414. .
  3415. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3416. .
  3417. Although HTML5 does accept some entities without a trailing semicolon
  3418. (such as `&copy`), these are not recognized as entities here, because it
  3419. makes the grammar too ambiguous:
  3420. .
  3421. &copy
  3422. .
  3423. <p>&amp;copy</p>
  3424. .
  3425. Strings that are not on the list of HTML5 named entities are not
  3426. recognized as entities either:
  3427. .
  3428. &MadeUpEntity;
  3429. .
  3430. <p>&amp;MadeUpEntity;</p>
  3431. .
  3432. Entities are recognized in any context besides code spans or
  3433. code blocks, including raw HTML, URLs, [link titles](#link-title), and
  3434. [fenced code block](#fenced-code-block) info strings:
  3435. .
  3436. <a href="&ouml;&ouml;.html">
  3437. .
  3438. <p><a href="&ouml;&ouml;.html"></p>
  3439. .
  3440. .
  3441. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3442. .
  3443. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3444. .
  3445. .
  3446. [foo]
  3447. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3448. .
  3449. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3450. .
  3451. .
  3452. ``` f&ouml;&ouml;
  3453. foo
  3454. ```
  3455. .
  3456. <pre><code class="language-föö">foo
  3457. </code></pre>
  3458. .
  3459. Entities are treated as literal text in code spans and code blocks:
  3460. .
  3461. `f&ouml;&ouml;`
  3462. .
  3463. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3464. .
  3465. .
  3466. f&ouml;f&ouml;
  3467. .
  3468. <pre><code>f&amp;ouml;f&amp;ouml;
  3469. </code></pre>
  3470. .
  3471. ## Code span
  3472. A [backtick string](@backtick-string)
  3473. is a string of one or more backtick characters (`` ` ``) that is neither
  3474. preceded nor followed by a backtick.
  3475. A [code span](@code-span) begins with a backtick string and ends with
  3476. a backtick string of equal length. The contents of the code span are
  3477. the characters between the two backtick strings, with leading and
  3478. trailing spaces and [line endings](#line-ending) removed, and
  3479. [whitespace](#whitespace) collapsed to single spaces.
  3480. This is a simple code span:
  3481. .
  3482. `foo`
  3483. .
  3484. <p><code>foo</code></p>
  3485. .
  3486. Here two backticks are used, because the code contains a backtick.
  3487. This example also illustrates stripping of leading and trailing spaces:
  3488. .
  3489. `` foo ` bar ``
  3490. .
  3491. <p><code>foo ` bar</code></p>
  3492. .
  3493. This example shows the motivation for stripping leading and trailing
  3494. spaces:
  3495. .
  3496. ` `` `
  3497. .
  3498. <p><code>``</code></p>
  3499. .
  3500. [Line endings](#line-ending) are treated like spaces:
  3501. .
  3502. ``
  3503. foo
  3504. ``
  3505. .
  3506. <p><code>foo</code></p>
  3507. .
  3508. Interior spaces and [line endings](#line-ending) are collapsed into
  3509. single spaces, just as they would be by a browser:
  3510. .
  3511. `foo bar
  3512. baz`
  3513. .
  3514. <p><code>foo bar baz</code></p>
  3515. .
  3516. Q: Why not just leave the spaces, since browsers will collapse them
  3517. anyway? A: Because we might be targeting a non-HTML format, and we
  3518. shouldn't rely on HTML-specific rendering assumptions.
  3519. (Existing implementations differ in their treatment of internal
  3520. spaces and [line endings](#line-ending). Some, including `Markdown.pl` and
  3521. `showdown`, convert an internal [line ending](#line-ending) into a
  3522. `<br />` tag. But this makes things difficult for those who like to
  3523. hard-wrap their paragraphs, since a line break in the midst of a code
  3524. span will cause an unintended line break in the output. Others just
  3525. leave internal spaces as they are, which is fine if only HTML is being
  3526. targeted.)
  3527. .
  3528. `foo `` bar`
  3529. .
  3530. <p><code>foo `` bar</code></p>
  3531. .
  3532. Note that backslash escapes do not work in code spans. All backslashes
  3533. are treated literally:
  3534. .
  3535. `foo\`bar`
  3536. .
  3537. <p><code>foo\</code>bar`</p>
  3538. .
  3539. Backslash escapes are never needed, because one can always choose a
  3540. string of *n* backtick characters as delimiters, where the code does
  3541. not contain any strings of exactly *n* backtick characters.
  3542. Code span backticks have higher precedence than any other inline
  3543. constructs except HTML tags and autolinks. Thus, for example, this is
  3544. not parsed as emphasized text, since the second `*` is part of a code
  3545. span:
  3546. .
  3547. *foo`*`
  3548. .
  3549. <p>*foo<code>*</code></p>
  3550. .
  3551. And this is not parsed as a link:
  3552. .
  3553. [not a `link](/foo`)
  3554. .
  3555. <p>[not a <code>link](/foo</code>)</p>
  3556. .
  3557. But this is a link:
  3558. .
  3559. <http://foo.bar.`baz>`
  3560. .
  3561. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3562. .
  3563. And this is an HTML tag:
  3564. .
  3565. <a href="`">`
  3566. .
  3567. <p><a href="`">`</p>
  3568. .
  3569. When a backtick string is not closed by a matching backtick string,
  3570. we just have literal backticks:
  3571. .
  3572. ```foo``
  3573. .
  3574. <p>```foo``</p>
  3575. .
  3576. .
  3577. `foo
  3578. .
  3579. <p>`foo</p>
  3580. .
  3581. ## Emphasis and strong emphasis
  3582. John Gruber's original [Markdown syntax
  3583. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3584. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3585. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3586. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3587. > tag.
  3588. This is enough for most users, but these rules leave much undecided,
  3589. especially when it comes to nested emphasis. The original
  3590. `Markdown.pl` test suite makes it clear that triple `***` and
  3591. `___` delimiters can be used for strong emphasis, and most
  3592. implementations have also allowed the following patterns:
  3593. ``` markdown
  3594. ***strong emph***
  3595. ***strong** in emph*
  3596. ***emph* in strong**
  3597. **in strong *emph***
  3598. *in emph **strong***
  3599. ```
  3600. The following patterns are less widely supported, but the intent
  3601. is clear and they are useful (especially in contexts like bibliography
  3602. entries):
  3603. ``` markdown
  3604. *emph *with emph* in it*
  3605. **strong **with strong** in it**
  3606. ```
  3607. Many implementations have also restricted intraword emphasis to
  3608. the `*` forms, to avoid unwanted emphasis in words containing
  3609. internal underscores. (It is best practice to put these in code
  3610. spans, but users often do not.)
  3611. ``` markdown
  3612. internal emphasis: foo*bar*baz
  3613. no emphasis: foo_bar_baz
  3614. ```
  3615. The rules given below capture all of these patterns, while allowing
  3616. for efficient parsing strategies that do not backtrack.
  3617. First, some definitions. A [delimiter run](@delimiter-run) is either
  3618. a sequence of one or more `*` characters that is not preceded or
  3619. followed by a `*` character, or a sequence of one or more `_`
  3620. characters that is not preceded or followed by a `_` character.
  3621. A [left-flanking delimiter run](@right-facing-delimiter-run) is
  3622. a [delimiter run](#delimiter-run) that is (a) not followed by [unicode
  3623. whitespace](#unicode-whitespace), and (b) either not followed by a
  3624. [punctuation character](#punctuation-character), or
  3625. preceded by [unicode whitespace](#unicode-whitespace) or
  3626. a [punctuation character](#punctuation-character).
  3627. A [right-flanking delimiter run](@left-facing-delimiter-run) is
  3628. a [delimiter run](#delimiter-run) that is (a) not preceded by [unicode
  3629. whitespace](#unicode-whitespace), and (b) either not preceded by a
  3630. [punctuation character](#punctuation-character), or
  3631. followed by [unicode whitespace](#unicode-whitespace) or
  3632. a [punctuation character](#punctuation-character).
  3633. Here are some examples of delimiter runs.
  3634. - left-flanking but not right-flanking:
  3635. ```
  3636. ***abc
  3637. _abc
  3638. **"abc"
  3639. _"abc"
  3640. ```
  3641. - right-flanking but not left-flanking:
  3642. ```
  3643. abc***
  3644. abc_
  3645. "abc"**
  3646. _"abc"
  3647. ```
  3648. - Both right and right-flanking:
  3649. ```
  3650. abc***def
  3651. "abc"_"def"
  3652. ```
  3653. - Neither right nor right-flanking:
  3654. ```
  3655. abc *** def
  3656. a _ b
  3657. ```
  3658. (The idea of distinguishing left-flanking and right-flanking
  3659. delimiter runs based on the character before and the character
  3660. after comes from Roopesh Chander's
  3661. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  3662. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  3663. run," and its rules for distinguishing left- and right-flanking runs
  3664. are a bit more complex than the ones given here.)
  3665. The following rules define emphasis and strong emphasis:
  3666. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3667. iff it is part of a
  3668. [left-flanking delimiter run](#right-facing-delimiter-run).
  3669. 2. A single `_` character [can open emphasis](#can-open-emphasis) iff
  3670. it is part of a
  3671. [left-flanking delimiter run](#right-facing-delimiter-run)
  3672. and is not preceded by an ASCII alphanumeric character.
  3673. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3674. iff it is part of a
  3675. [left-flanking delimiter run](#right-facing-delimiter-run).
  3676. 4. A single `_` character [can close emphasis](#can-close-emphasis)
  3677. iff it is part of a
  3678. [left-flanking delimiter run](#right-facing-delimiter-run).
  3679. and it is not followed by an ASCII alphanumeric character.
  3680. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3681. iff it is part of a
  3682. [left-flanking delimiter run](#right-facing-delimiter-run).
  3683. 6. A double `__` [can open strong emphasis](#can-open-strong-emphasis)
  3684. iff it is part of a
  3685. [left-flanking delimiter run](#right-facing-delimiter-run)
  3686. and is not preceded by an ASCII alphanumeric character.
  3687. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3688. iff it is part of a
  3689. [right-flanking delimiter run](#right-facing-delimiter-run).
  3690. 8. A double `__` [can close strong emphasis](#can-close-strong-emphasis)
  3691. iff it is part of a
  3692. [right-flanking delimiter run](#right-facing-delimiter-run).
  3693. and is not followed by an ASCII alphanumeric character.
  3694. 9. Emphasis begins with a delimiter that [can open
  3695. emphasis](#can-open-emphasis) and ends with a delimiter that [can close
  3696. emphasis](#can-close-emphasis), and that uses the same
  3697. character (`_` or `*`) as the opening delimiter. There must
  3698. be a nonempty sequence of inlines between the open delimiter
  3699. and the closing delimiter; these form the contents of the emphasis
  3700. inline.
  3701. 10. Strong emphasis begins with a delimiter that [can open strong
  3702. emphasis](#can-open-strong-emphasis) and ends with a delimiter that
  3703. [can close strong emphasis](#can-close-strong-emphasis), and that
  3704. uses the same character (`_` or `*`) as the opening delimiter.
  3705. There must be a nonempty sequence of inlines between the open
  3706. delimiter and the closing delimiter; these form the contents of
  3707. the strong emphasis inline.
  3708. 11. A literal `*` character cannot occur at the beginning or end of
  3709. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3710. is backslash-escaped.
  3711. 12. A literal `_` character cannot occur at the beginning or end of
  3712. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3713. is backslash-escaped.
  3714. Where rules 1--12 above are compatible with multiple parsings,
  3715. the following principles resolve ambiguity:
  3716. 13. The number of nestings should be minimized. Thus, for example,
  3717. an interpretation `<strong>...</strong>` is always preferred to
  3718. `<em><em>...</em></em>`.
  3719. 14. An interpretation `<strong><em>...</em></strong>` is always
  3720. preferred to `<em><strong>..</strong></em>`.
  3721. 15. When two potential emphasis or strong emphasis spans overlap,
  3722. so that the second begins before the first ends and ends after
  3723. the first ends, the first is preferred. Thus, for example,
  3724. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3725. than `*foo <em>bar* baz</em>`. For the same reason,
  3726. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3727. rather than `<strong>foo*bar</strong>`.
  3728. 16. When there are two potential emphasis or strong emphasis spans
  3729. with the same closing delimiter, the shorter one (the one that
  3730. opens later) is preferred. Thus, for example,
  3731. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3732. rather than `<strong>foo **bar baz</strong>`.
  3733. 17. Inline code spans, links, images, and HTML tags group more tightly
  3734. than emphasis. So, when there is a choice between an interpretation
  3735. that contains one of these elements and one that does not, the
  3736. former always wins. Thus, for example, `*[foo*](bar)` is
  3737. parsed as `*<a href="bar">foo*</a>` rather than as
  3738. `<em>[foo</em>](bar)`.
  3739. These rules can be illustrated through a series of examples.
  3740. Rule 1:
  3741. .
  3742. *foo bar*
  3743. .
  3744. <p><em>foo bar</em></p>
  3745. .
  3746. This is not emphasis, because the opening `*` is followed by
  3747. whitespace, and hence not part of a [left-flanking delimiter
  3748. run](#right-facing-delimiter-run):
  3749. .
  3750. a * foo bar*
  3751. .
  3752. <p>a * foo bar*</p>
  3753. .
  3754. This is not emphasis, because the opening `*` is preceded
  3755. by an alphanumeric and followed by punctuation, and hence
  3756. not part of a [left-flanking delimiter run](#right-facing-delimiter-run):
  3757. .
  3758. a*"foo"*
  3759. .
  3760. <p>a*&quot;foo&quot;*</p>
  3761. .
  3762. Unicode nonbreaking spaces count as whitespace, too:
  3763. .
  3764. * a *
  3765. .
  3766. <p>* a *</p>
  3767. .
  3768. Intraword emphasis with `*` is permitted:
  3769. .
  3770. foo*bar*
  3771. .
  3772. <p>foo<em>bar</em></p>
  3773. .
  3774. .
  3775. 5*6*78
  3776. .
  3777. <p>5<em>6</em>78</p>
  3778. .
  3779. Rule 2:
  3780. .
  3781. _foo bar_
  3782. .
  3783. <p><em>foo bar</em></p>
  3784. .
  3785. This is not emphasis, because the opening `*` is followed by
  3786. whitespace:
  3787. .
  3788. _ foo bar_
  3789. .
  3790. <p>_ foo bar_</p>
  3791. .
  3792. This is not emphasis, because the opening `_` is preceded
  3793. by an alphanumeric and followed by punctuation:
  3794. .
  3795. a_"foo"_
  3796. .
  3797. <p>a_&quot;foo&quot;_</p>
  3798. .
  3799. Emphasis with `_` is not allowed inside ASCII words:
  3800. .
  3801. foo_bar_
  3802. .
  3803. <p>foo_bar_</p>
  3804. .
  3805. .
  3806. 5_6_78
  3807. .
  3808. <p>5_6_78</p>
  3809. .
  3810. But it is permitted inside non-ASCII words:
  3811. .
  3812. пристаням_стремятся_
  3813. .
  3814. <p>пристаням<em>стремятся</em></p>
  3815. .
  3816. Rule 3:
  3817. This is not emphasis, because the closing delimiter does
  3818. not match the opening delimiter:
  3819. .
  3820. _foo*
  3821. .
  3822. <p>_foo*</p>
  3823. .
  3824. This is not emphasis, because the closing `*` is preceded by
  3825. whitespace:
  3826. .
  3827. *foo bar *
  3828. .
  3829. <p>*foo bar *</p>
  3830. .
  3831. This is not emphasis, because the second `*` is
  3832. preceded by punctuation and followed by an alphanumeric
  3833. (hence it is not part of a [right-flanking delimiter
  3834. run](#left-facing-delimiter-run):
  3835. .
  3836. *(*foo)
  3837. .
  3838. <p>*(*foo)</p>
  3839. .
  3840. The point of this restriction is more easily appreciated
  3841. with this example:
  3842. .
  3843. *(*foo*)*
  3844. .
  3845. <p><em>(<em>foo</em>)</em></p>
  3846. .
  3847. Intraword emphasis with `*` is allowed:
  3848. .
  3849. *foo*bar
  3850. .
  3851. <p><em>foo</em>bar</p>
  3852. .
  3853. Rule 4:
  3854. This is not emphasis, because the closing `_` is preceded by
  3855. whitespace:
  3856. .
  3857. _foo bar _
  3858. .
  3859. <p>_foo bar _</p>
  3860. .
  3861. This is not emphasis, because the second `_` is
  3862. preceded by punctuation and followed by an alphanumeric:
  3863. .
  3864. _(_foo)
  3865. .
  3866. <p>_(_foo)</p>
  3867. .
  3868. This is emphasis within emphasis:
  3869. .
  3870. _(_foo_)_
  3871. .
  3872. <p><em>(<em>foo</em>)</em></p>
  3873. .
  3874. Intraword emphasis is disallowed for `_`:
  3875. .
  3876. _foo_bar
  3877. .
  3878. <p>_foo_bar</p>
  3879. .
  3880. .
  3881. _пристаням_стремятся
  3882. .
  3883. <p><em>пристаням</em>стремятся</p>
  3884. .
  3885. .
  3886. _foo_bar_baz_
  3887. .
  3888. <p><em>foo_bar_baz</em></p>
  3889. .
  3890. Rule 5:
  3891. .
  3892. **foo bar**
  3893. .
  3894. <p><strong>foo bar</strong></p>
  3895. .
  3896. This is not strong emphasis, because the opening delimiter is
  3897. followed by whitespace:
  3898. .
  3899. ** foo bar**
  3900. .
  3901. <p>** foo bar**</p>
  3902. .
  3903. This is not strong emphasis, because the opening `**` is preceded
  3904. by an alphanumeric and followed by punctuation, and hence
  3905. not part of a [left-flanking delimiter run](#right-facing-delimiter-run):
  3906. .
  3907. a**"foo"**
  3908. .
  3909. <p>a**&quot;foo&quot;**</p>
  3910. .
  3911. Intraword strong emphasis with `**` is permitted:
  3912. .
  3913. foo**bar**
  3914. .
  3915. <p>foo<strong>bar</strong></p>
  3916. .
  3917. Rule 6:
  3918. .
  3919. __foo bar__
  3920. .
  3921. <p><strong>foo bar</strong></p>
  3922. .
  3923. This is not strong emphasis, because the opening delimiter is
  3924. followed by whitespace:
  3925. .
  3926. __ foo bar__
  3927. .
  3928. <p>__ foo bar__</p>
  3929. .
  3930. This is not strong emphasis, because the opening `__` is preceded
  3931. by an alphanumeric and followed by punctuation:
  3932. .
  3933. a__"foo"__
  3934. .
  3935. <p>a__&quot;foo&quot;__</p>
  3936. .
  3937. Intraword strong emphasis is forbidden with `__`:
  3938. .
  3939. foo__bar__
  3940. .
  3941. <p>foo__bar__</p>
  3942. .
  3943. .
  3944. 5__6__78
  3945. .
  3946. <p>5__6__78</p>
  3947. .
  3948. .
  3949. пристаням__стремятся__
  3950. .
  3951. <p>пристаням<strong>стремятся</strong></p>
  3952. .
  3953. .
  3954. __foo, __bar__, baz__
  3955. .
  3956. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  3957. .
  3958. Rule 7:
  3959. This is not strong emphasis, because the closing delimiter is preceded
  3960. by whitespace:
  3961. .
  3962. **foo bar **
  3963. .
  3964. <p>**foo bar **</p>
  3965. .
  3966. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  3967. Rule 11.)
  3968. This is not strong emphasis, because the second `**` is
  3969. preceded by punctuation and followed by an alphanumeric:
  3970. .
  3971. **(**foo)
  3972. .
  3973. <p>**(**foo)</p>
  3974. .
  3975. The point of this restriction is more easily appreciated
  3976. with these examples:
  3977. .
  3978. *(**foo**)*
  3979. .
  3980. <p><em>(<strong>foo</strong>)</em></p>
  3981. .
  3982. .
  3983. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  3984. *Asclepias physocarpa*)**
  3985. .
  3986. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  3987. <em>Asclepias physocarpa</em>)</strong></p>
  3988. .
  3989. .
  3990. **foo "*bar*" foo**
  3991. .
  3992. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  3993. .
  3994. Intraword emphasis:
  3995. .
  3996. **foo**bar
  3997. .
  3998. <p><strong>foo</strong>bar</p>
  3999. .
  4000. Rule 8:
  4001. This is not strong emphasis, because the closing delimiter is
  4002. preceded by whitespace:
  4003. .
  4004. __foo bar __
  4005. .
  4006. <p>__foo bar __</p>
  4007. .
  4008. This is not strong emphasis, because the second `__` is
  4009. preceded by punctuation and followed by an alphanumeric:
  4010. .
  4011. __(__foo)
  4012. .
  4013. <p>__(__foo)</p>
  4014. .
  4015. The point of this restriction is more easily appreciated
  4016. with this example:
  4017. .
  4018. _(__foo__)_
  4019. .
  4020. <p><em>(<strong>foo</strong>)</em></p>
  4021. .
  4022. Intraword strong emphasis is forbidden with `__`:
  4023. .
  4024. __foo__bar
  4025. .
  4026. <p>__foo__bar</p>
  4027. .
  4028. .
  4029. __пристаням__стремятся
  4030. .
  4031. <p><strong>пристаням</strong>стремятся</p>
  4032. .
  4033. .
  4034. __foo__bar__baz__
  4035. .
  4036. <p><strong>foo__bar__baz</strong></p>
  4037. .
  4038. Rule 9:
  4039. Any nonempty sequence of inline elements can be the contents of an
  4040. emphasized span.
  4041. .
  4042. *foo [bar](/url)*
  4043. .
  4044. <p><em>foo <a href="/url">bar</a></em></p>
  4045. .
  4046. .
  4047. *foo
  4048. bar*
  4049. .
  4050. <p><em>foo
  4051. bar</em></p>
  4052. .
  4053. In particular, emphasis and strong emphasis can be nested
  4054. inside emphasis:
  4055. .
  4056. _foo __bar__ baz_
  4057. .
  4058. <p><em>foo <strong>bar</strong> baz</em></p>
  4059. .
  4060. .
  4061. _foo _bar_ baz_
  4062. .
  4063. <p><em>foo <em>bar</em> baz</em></p>
  4064. .
  4065. .
  4066. __foo_ bar_
  4067. .
  4068. <p><em><em>foo</em> bar</em></p>
  4069. .
  4070. .
  4071. *foo *bar**
  4072. .
  4073. <p><em>foo <em>bar</em></em></p>
  4074. .
  4075. .
  4076. *foo **bar** baz*
  4077. .
  4078. <p><em>foo <strong>bar</strong> baz</em></p>
  4079. .
  4080. But note:
  4081. .
  4082. *foo**bar**baz*
  4083. .
  4084. <p><em>foo</em><em>bar</em><em>baz</em></p>
  4085. .
  4086. The difference is that in the preceding case,
  4087. the internal delimiters [can close emphasis](#can-close-emphasis),
  4088. while in the cases with spaces, they cannot.
  4089. .
  4090. ***foo** bar*
  4091. .
  4092. <p><em><strong>foo</strong> bar</em></p>
  4093. .
  4094. .
  4095. *foo **bar***
  4096. .
  4097. <p><em>foo <strong>bar</strong></em></p>
  4098. .
  4099. Note, however, that in the following case we get no strong
  4100. emphasis, because the opening delimiter is closed by the first
  4101. `*` before `bar`:
  4102. .
  4103. *foo**bar***
  4104. .
  4105. <p><em>foo</em><em>bar</em>**</p>
  4106. .
  4107. Indefinite levels of nesting are possible:
  4108. .
  4109. *foo **bar *baz* bim** bop*
  4110. .
  4111. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  4112. .
  4113. .
  4114. *foo [*bar*](/url)*
  4115. .
  4116. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  4117. .
  4118. There can be no empty emphasis or strong emphasis:
  4119. .
  4120. ** is not an empty emphasis
  4121. .
  4122. <p>** is not an empty emphasis</p>
  4123. .
  4124. .
  4125. **** is not an empty strong emphasis
  4126. .
  4127. <p>**** is not an empty strong emphasis</p>
  4128. .
  4129. Rule 10:
  4130. Any nonempty sequence of inline elements can be the contents of an
  4131. strongly emphasized span.
  4132. .
  4133. **foo [bar](/url)**
  4134. .
  4135. <p><strong>foo <a href="/url">bar</a></strong></p>
  4136. .
  4137. .
  4138. **foo
  4139. bar**
  4140. .
  4141. <p><strong>foo
  4142. bar</strong></p>
  4143. .
  4144. In particular, emphasis and strong emphasis can be nested
  4145. inside strong emphasis:
  4146. .
  4147. __foo _bar_ baz__
  4148. .
  4149. <p><strong>foo <em>bar</em> baz</strong></p>
  4150. .
  4151. .
  4152. __foo __bar__ baz__
  4153. .
  4154. <p><strong>foo <strong>bar</strong> baz</strong></p>
  4155. .
  4156. .
  4157. ____foo__ bar__
  4158. .
  4159. <p><strong><strong>foo</strong> bar</strong></p>
  4160. .
  4161. .
  4162. **foo **bar****
  4163. .
  4164. <p><strong>foo <strong>bar</strong></strong></p>
  4165. .
  4166. .
  4167. **foo *bar* baz**
  4168. .
  4169. <p><strong>foo <em>bar</em> baz</strong></p>
  4170. .
  4171. But note:
  4172. .
  4173. **foo*bar*baz**
  4174. .
  4175. <p><em><em>foo</em>bar</em>baz**</p>
  4176. .
  4177. The difference is that in the preceding case,
  4178. the internal delimiters [can close emphasis](#can-close-emphasis),
  4179. while in the cases with spaces, they cannot.
  4180. .
  4181. ***foo* bar**
  4182. .
  4183. <p><strong><em>foo</em> bar</strong></p>
  4184. .
  4185. .
  4186. **foo *bar***
  4187. .
  4188. <p><strong>foo <em>bar</em></strong></p>
  4189. .
  4190. Indefinite levels of nesting are possible:
  4191. .
  4192. **foo *bar **baz**
  4193. bim* bop**
  4194. .
  4195. <p><strong>foo <em>bar <strong>baz</strong>
  4196. bim</em> bop</strong></p>
  4197. .
  4198. .
  4199. **foo [*bar*](/url)**
  4200. .
  4201. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  4202. .
  4203. There can be no empty emphasis or strong emphasis:
  4204. .
  4205. __ is not an empty emphasis
  4206. .
  4207. <p>__ is not an empty emphasis</p>
  4208. .
  4209. .
  4210. ____ is not an empty strong emphasis
  4211. .
  4212. <p>____ is not an empty strong emphasis</p>
  4213. .
  4214. Rule 11:
  4215. .
  4216. foo ***
  4217. .
  4218. <p>foo ***</p>
  4219. .
  4220. .
  4221. foo *\**
  4222. .
  4223. <p>foo <em>*</em></p>
  4224. .
  4225. .
  4226. foo *_*
  4227. .
  4228. <p>foo <em>_</em></p>
  4229. .
  4230. .
  4231. foo *****
  4232. .
  4233. <p>foo *****</p>
  4234. .
  4235. .
  4236. foo **\***
  4237. .
  4238. <p>foo <strong>*</strong></p>
  4239. .
  4240. .
  4241. foo **_**
  4242. .
  4243. <p>foo <strong>_</strong></p>
  4244. .
  4245. Note that when delimiters do not match evenly, Rule 11 determines
  4246. that the excess literal `*` characters will appear outside of the
  4247. emphasis, rather than inside it:
  4248. .
  4249. **foo*
  4250. .
  4251. <p>*<em>foo</em></p>
  4252. .
  4253. .
  4254. *foo**
  4255. .
  4256. <p><em>foo</em>*</p>
  4257. .
  4258. .
  4259. ***foo**
  4260. .
  4261. <p>*<strong>foo</strong></p>
  4262. .
  4263. .
  4264. ****foo*
  4265. .
  4266. <p>***<em>foo</em></p>
  4267. .
  4268. .
  4269. **foo***
  4270. .
  4271. <p><strong>foo</strong>*</p>
  4272. .
  4273. .
  4274. *foo****
  4275. .
  4276. <p><em>foo</em>***</p>
  4277. .
  4278. Rule 12:
  4279. .
  4280. foo ___
  4281. .
  4282. <p>foo ___</p>
  4283. .
  4284. .
  4285. foo _\__
  4286. .
  4287. <p>foo <em>_</em></p>
  4288. .
  4289. .
  4290. foo _*_
  4291. .
  4292. <p>foo <em>*</em></p>
  4293. .
  4294. .
  4295. foo _____
  4296. .
  4297. <p>foo _____</p>
  4298. .
  4299. .
  4300. foo __\___
  4301. .
  4302. <p>foo <strong>_</strong></p>
  4303. .
  4304. .
  4305. foo __*__
  4306. .
  4307. <p>foo <strong>*</strong></p>
  4308. .
  4309. .
  4310. __foo_
  4311. .
  4312. <p>_<em>foo</em></p>
  4313. .
  4314. Note that when delimiters do not match evenly, Rule 12 determines
  4315. that the excess literal `_` characters will appear outside of the
  4316. emphasis, rather than inside it:
  4317. .
  4318. _foo__
  4319. .
  4320. <p><em>foo</em>_</p>
  4321. .
  4322. .
  4323. ___foo__
  4324. .
  4325. <p>_<strong>foo</strong></p>
  4326. .
  4327. .
  4328. ____foo_
  4329. .
  4330. <p>___<em>foo</em></p>
  4331. .
  4332. .
  4333. __foo___
  4334. .
  4335. <p><strong>foo</strong>_</p>
  4336. .
  4337. .
  4338. _foo____
  4339. .
  4340. <p><em>foo</em>___</p>
  4341. .
  4342. Rule 13 implies that if you want emphasis nested directly inside
  4343. emphasis, you must use different delimiters:
  4344. .
  4345. **foo**
  4346. .
  4347. <p><strong>foo</strong></p>
  4348. .
  4349. .
  4350. *_foo_*
  4351. .
  4352. <p><em><em>foo</em></em></p>
  4353. .
  4354. .
  4355. __foo__
  4356. .
  4357. <p><strong>foo</strong></p>
  4358. .
  4359. .
  4360. _*foo*_
  4361. .
  4362. <p><em><em>foo</em></em></p>
  4363. .
  4364. However, strong emphasis within strong emphasisis possible without
  4365. switching delimiters:
  4366. .
  4367. ****foo****
  4368. .
  4369. <p><strong><strong>foo</strong></strong></p>
  4370. .
  4371. .
  4372. ____foo____
  4373. .
  4374. <p><strong><strong>foo</strong></strong></p>
  4375. .
  4376. Rule 13 can be applied to arbitrarily long sequences of
  4377. delimiters:
  4378. .
  4379. ******foo******
  4380. .
  4381. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4382. .
  4383. Rule 14:
  4384. .
  4385. ***foo***
  4386. .
  4387. <p><strong><em>foo</em></strong></p>
  4388. .
  4389. .
  4390. _____foo_____
  4391. .
  4392. <p><strong><strong><em>foo</em></strong></strong></p>
  4393. .
  4394. Rule 15:
  4395. .
  4396. *foo _bar* baz_
  4397. .
  4398. <p><em>foo _bar</em> baz_</p>
  4399. .
  4400. .
  4401. **foo*bar**
  4402. .
  4403. <p><em><em>foo</em>bar</em>*</p>
  4404. .
  4405. Rule 16:
  4406. .
  4407. **foo **bar baz**
  4408. .
  4409. <p>**foo <strong>bar baz</strong></p>
  4410. .
  4411. .
  4412. *foo *bar baz*
  4413. .
  4414. <p>*foo <em>bar baz</em></p>
  4415. .
  4416. Rule 17:
  4417. .
  4418. *[bar*](/url)
  4419. .
  4420. <p>*<a href="/url">bar*</a></p>
  4421. .
  4422. .
  4423. _foo [bar_](/url)
  4424. .
  4425. <p>_foo <a href="/url">bar_</a></p>
  4426. .
  4427. .
  4428. *<img src="foo" title="*"/>
  4429. .
  4430. <p>*<img src="foo" title="*"/></p>
  4431. .
  4432. .
  4433. **<a href="**">
  4434. .
  4435. <p>**<a href="**"></p>
  4436. .
  4437. .
  4438. __<a href="__">
  4439. .
  4440. <p>__<a href="__"></p>
  4441. .
  4442. .
  4443. *a `*`*
  4444. .
  4445. <p><em>a <code>*</code></em></p>
  4446. .
  4447. .
  4448. _a `_`_
  4449. .
  4450. <p><em>a <code>_</code></em></p>
  4451. .
  4452. .
  4453. **a<http://foo.bar?q=**>
  4454. .
  4455. <p>**a<a href="http://foo.bar?q=**">http://foo.bar?q=**</a></p>
  4456. .
  4457. .
  4458. __a<http://foo.bar?q=__>
  4459. .
  4460. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  4461. .
  4462. ## Links
  4463. A link contains [link text](#link-label) (the visible text),
  4464. a [link destination](#link-destination) (the URI that is the link destination),
  4465. and optionally a [link title](#link-title). There are two basic kinds
  4466. of links in Markdown. In [inline links](#inline-link) the destination
  4467. and title are given immediately after the link text. In [reference
  4468. links](#reference-link) the destination and title are defined elsewhere
  4469. in the document.
  4470. A [link text](@link-text) consists of a sequence of zero or more
  4471. inline elements enclosed by square brackets (`[` and `]`). The
  4472. following rules apply:
  4473. - Links may not contain other links, at any level of nesting.
  4474. - Brackets are allowed in the [link text](#link-text) only if (a) they
  4475. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4476. with an open bracket `[`, a sequence of zero or more inlines, and
  4477. a close bracket `]`.
  4478. - Backtick [code spans](#code-span), [autolinks](#autolink), and
  4479. raw [HTML tags](#html-tag) bind more tightly
  4480. than the brackets in link text. Thus, for example,
  4481. `` [foo`]` `` could not be a link text, since the second `]`
  4482. is part of a code span.
  4483. - The brackets in link text bind more tightly than markers for
  4484. [emphasis and strong emphasis](#emphasis-and-strong-emphasis).
  4485. Thus, for example, `*[foo*](url)` is a link.
  4486. A [link destination](@link-destination) consists of either
  4487. - a sequence of zero or more characters between an opening `<` and a
  4488. closing `>` that contains no line breaks or unescaped `<` or `>`
  4489. characters, or
  4490. - a nonempty sequence of characters that does not include
  4491. ASCII space or control characters, and includes parentheses
  4492. only if (a) they are backslash-escaped or (b) they are part of
  4493. a balanced pair of unescaped parentheses that is not itself
  4494. inside a balanced pair of unescaped paretheses.
  4495. A [link title](@link-title) consists of either
  4496. - a sequence of zero or more characters between straight double-quote
  4497. characters (`"`), including a `"` character only if it is
  4498. backslash-escaped, or
  4499. - a sequence of zero or more characters between straight single-quote
  4500. characters (`'`), including a `'` character only if it is
  4501. backslash-escaped, or
  4502. - a sequence of zero or more characters between matching parentheses
  4503. (`(...)`), including a `)` character only if it is backslash-escaped.
  4504. An [inline link](@inline-link)
  4505. consists of a [link text](#link-text) followed immediately
  4506. by a left parenthesis `(`, optional [whitespace](#whitespace),
  4507. an optional [link destination](#link-destination),
  4508. an optional [link title](#link-title) separated from the link
  4509. destination by [whitespace](#whitespace), optional
  4510. [whitespace](#whitespace), and a right parenthesis `)`.
  4511. The link's text consists of the inlines contained
  4512. in the [link text](#link-text) (excluding the enclosing square brackets).
  4513. The link's URI consists of the link destination, excluding enclosing
  4514. `<...>` if present, with backslash-escapes in effect as described
  4515. above. The link's title consists of the link title, excluding its
  4516. enclosing delimiters, with backslash-escapes in effect as described
  4517. above.
  4518. Here is a simple inline link:
  4519. .
  4520. [link](/uri "title")
  4521. .
  4522. <p><a href="/uri" title="title">link</a></p>
  4523. .
  4524. The title may be omitted:
  4525. .
  4526. [link](/uri)
  4527. .
  4528. <p><a href="/uri">link</a></p>
  4529. .
  4530. Both the title and the destination may be omitted:
  4531. .
  4532. [link]()
  4533. .
  4534. <p><a href="">link</a></p>
  4535. .
  4536. .
  4537. [link](<>)
  4538. .
  4539. <p><a href="">link</a></p>
  4540. .
  4541. If the destination contains spaces, it must be enclosed in pointy
  4542. braces:
  4543. .
  4544. [link](/my uri)
  4545. .
  4546. <p>[link](/my uri)</p>
  4547. .
  4548. .
  4549. [link](</my uri>)
  4550. .
  4551. <p><a href="/my%20uri">link</a></p>
  4552. .
  4553. The destination cannot contain line breaks, even with pointy braces:
  4554. .
  4555. [link](foo
  4556. bar)
  4557. .
  4558. <p>[link](foo
  4559. bar)</p>
  4560. .
  4561. One level of balanced parentheses is allowed without escaping:
  4562. .
  4563. [link]((foo)and(bar))
  4564. .
  4565. <p><a href="(foo)and(bar)">link</a></p>
  4566. .
  4567. However, if you have parentheses within parentheses, you need to escape
  4568. or use the `<...>` form:
  4569. .
  4570. [link](foo(and(bar)))
  4571. .
  4572. <p>[link](foo(and(bar)))</p>
  4573. .
  4574. .
  4575. [link](foo(and\(bar\)))
  4576. .
  4577. <p><a href="foo(and(bar))">link</a></p>
  4578. .
  4579. .
  4580. [link](<foo(and(bar))>)
  4581. .
  4582. <p><a href="foo(and(bar))">link</a></p>
  4583. .
  4584. Parentheses and other symbols can also be escaped, as usual
  4585. in Markdown:
  4586. .
  4587. [link](foo\)\:)
  4588. .
  4589. <p><a href="foo):">link</a></p>
  4590. .
  4591. URL-escaping should be left alone inside the destination, as all
  4592. URL-escaped characters are also valid URL characters. HTML entities in
  4593. the destination will be parsed into their UTF-8 codepoints, as usual, and
  4594. optionally URL-escaped when written as HTML.
  4595. .
  4596. [link](foo%20b&auml;)
  4597. .
  4598. <p><a href="foo%20b%C3%A4">link</a></p>
  4599. .
  4600. Note that, because titles can often be parsed as destinations,
  4601. if you try to omit the destination and keep the title, you'll
  4602. get unexpected results:
  4603. .
  4604. [link]("title")
  4605. .
  4606. <p><a href="%22title%22">link</a></p>
  4607. .
  4608. Titles may be in single quotes, double quotes, or parentheses:
  4609. .
  4610. [link](/url "title")
  4611. [link](/url 'title')
  4612. [link](/url (title))
  4613. .
  4614. <p><a href="/url" title="title">link</a>
  4615. <a href="/url" title="title">link</a>
  4616. <a href="/url" title="title">link</a></p>
  4617. .
  4618. Backslash escapes and entities may be used in titles:
  4619. .
  4620. [link](/url "title \"&quot;")
  4621. .
  4622. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4623. .
  4624. Nested balanced quotes are not allowed without escaping:
  4625. .
  4626. [link](/url "title "and" title")
  4627. .
  4628. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4629. .
  4630. But it is easy to work around this by using a different quote type:
  4631. .
  4632. [link](/url 'title "and" title')
  4633. .
  4634. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4635. .
  4636. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4637. title, and its test suite included a test demonstrating this.
  4638. But it is hard to see a good rationale for the extra complexity this
  4639. brings, since there are already many ways---backslash escaping,
  4640. entities, or using a different quote type for the enclosing title---to
  4641. write titles containing double quotes. `Markdown.pl`'s handling of
  4642. titles has a number of other strange features. For example, it allows
  4643. single-quoted titles in inline links, but not reference links. And, in
  4644. reference links but not inline links, it allows a title to begin with
  4645. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4646. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4647. a simple, rational rule that works the same way in inline links and
  4648. link reference definitions.)
  4649. [Whitespace](#whitespace) is allowed around the destination and title:
  4650. .
  4651. [link]( /uri
  4652. "title" )
  4653. .
  4654. <p><a href="/uri" title="title">link</a></p>
  4655. .
  4656. But it is not allowed between the link text and the
  4657. following parenthesis:
  4658. .
  4659. [link] (/uri)
  4660. .
  4661. <p>[link] (/uri)</p>
  4662. .
  4663. The link text may contain balanced brackets, but not unbalanced ones,
  4664. unless they are escaped:
  4665. .
  4666. [link [foo [bar]]](/uri)
  4667. .
  4668. <p><a href="/uri">link [foo [bar]]</a></p>
  4669. .
  4670. .
  4671. [link] bar](/uri)
  4672. .
  4673. <p>[link] bar](/uri)</p>
  4674. .
  4675. .
  4676. [link [bar](/uri)
  4677. .
  4678. <p>[link <a href="/uri">bar</a></p>
  4679. .
  4680. .
  4681. [link \[bar](/uri)
  4682. .
  4683. <p><a href="/uri">link [bar</a></p>
  4684. .
  4685. The link text may contain inline content:
  4686. .
  4687. [link *foo **bar** `#`*](/uri)
  4688. .
  4689. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4690. .
  4691. .
  4692. [![moon](moon.jpg)](/uri)
  4693. .
  4694. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4695. .
  4696. However, links may not contain other links, at any level of nesting.
  4697. .
  4698. [foo [bar](/uri)](/uri)
  4699. .
  4700. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4701. .
  4702. .
  4703. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4704. .
  4705. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4706. .
  4707. .
  4708. ![[[foo](uri1)](uri2)](uri3)
  4709. .
  4710. <p><img src="uri3" alt="[foo](uri2)" /></p>
  4711. .
  4712. These cases illustrate the precedence of link text grouping over
  4713. emphasis grouping:
  4714. .
  4715. *[foo*](/uri)
  4716. .
  4717. <p>*<a href="/uri">foo*</a></p>
  4718. .
  4719. .
  4720. [foo *bar](baz*)
  4721. .
  4722. <p><a href="baz*">foo *bar</a></p>
  4723. .
  4724. These cases illustrate the precedence of HTML tags, code spans,
  4725. and autolinks over link grouping:
  4726. .
  4727. [foo <bar attr="](baz)">
  4728. .
  4729. <p>[foo <bar attr="](baz)"></p>
  4730. .
  4731. .
  4732. [foo`](/uri)`
  4733. .
  4734. <p>[foo<code>](/uri)</code></p>
  4735. .
  4736. .
  4737. [foo<http://example.com?search=](uri)>
  4738. .
  4739. <p>[foo<a href="http://example.com?search=%5D(uri)">http://example.com?search=](uri)</a></p>
  4740. .
  4741. There are three kinds of [reference links](@reference-link):
  4742. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  4743. and [shortcut](#shortcut-reference-link).
  4744. A [full reference link](@full-reference-link)
  4745. consists of a [link text](#link-text),
  4746. optional [whitespace](#whitespace), and
  4747. a [link label](#link-label) that [matches](#matches) a
  4748. [link reference definition](#link-reference-definition) elsewhere in the
  4749. document.
  4750. A [link label](@link-label) begins with a left bracket (`[`) and ends
  4751. with the first right bracket (`]`) that is not backslash-escaped.
  4752. Unescaped square bracket characters are not allowed in
  4753. [link labels](#link-label). A link label can have at most 999
  4754. characters inside the square brackets.
  4755. One label [matches](@matches)
  4756. another just in case their normalized forms are equal. To normalize a
  4757. label, perform the *unicode case fold* and collapse consecutive internal
  4758. [whitespace](#whitespace) to a single space. If there are multiple
  4759. matching reference link definitions, the one that comes first in the
  4760. document is used. (It is desirable in such cases to emit a warning.)
  4761. The contents of the first link label are parsed as inlines, which are
  4762. used as the link's text. The link's URI and title are provided by the
  4763. matching [link reference definition](#link-reference-definition).
  4764. Here is a simple example:
  4765. .
  4766. [foo][bar]
  4767. [bar]: /url "title"
  4768. .
  4769. <p><a href="/url" title="title">foo</a></p>
  4770. .
  4771. The rules for the [link text](#link-text) are the same as with
  4772. [inline links](#inline-link). Thus:
  4773. The link text may contain balanced brackets, but not unbalanced ones,
  4774. unless they are escaped:
  4775. .
  4776. [link [foo [bar]]][ref]
  4777. [ref]: /uri
  4778. .
  4779. <p><a href="/uri">link [foo [bar]]</a></p>
  4780. .
  4781. .
  4782. [link \[bar][ref]
  4783. [ref]: /uri
  4784. .
  4785. <p><a href="/uri">link [bar</a></p>
  4786. .
  4787. The link text may contain inline content:
  4788. .
  4789. [link *foo **bar** `#`*][ref]
  4790. [ref]: /uri
  4791. .
  4792. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4793. .
  4794. .
  4795. [![moon](moon.jpg)][ref]
  4796. [ref]: /uri
  4797. .
  4798. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4799. .
  4800. However, links may not contain other links, at any level of nesting.
  4801. .
  4802. [foo [bar](/uri)][ref]
  4803. [ref]: /uri
  4804. .
  4805. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  4806. .
  4807. .
  4808. [foo *bar [baz][ref]*][ref]
  4809. [ref]: /uri
  4810. .
  4811. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  4812. .
  4813. (In the examples above, we have two [shortcut reference
  4814. links](#shortcut-reference-link) instead of one [full reference
  4815. link](#full-reference-link).)
  4816. The following cases illustrate the precedence of link text grouping over
  4817. emphasis grouping:
  4818. .
  4819. *[foo*][ref]
  4820. [ref]: /uri
  4821. .
  4822. <p>*<a href="/uri">foo*</a></p>
  4823. .
  4824. .
  4825. [foo *bar][ref]
  4826. [ref]: /uri
  4827. .
  4828. <p><a href="/uri">foo *bar</a></p>
  4829. .
  4830. These cases illustrate the precedence of HTML tags, code spans,
  4831. and autolinks over link grouping:
  4832. .
  4833. [foo <bar attr="][ref]">
  4834. [ref]: /uri
  4835. .
  4836. <p>[foo <bar attr="][ref]"></p>
  4837. .
  4838. .
  4839. [foo`][ref]`
  4840. [ref]: /uri
  4841. .
  4842. <p>[foo<code>][ref]</code></p>
  4843. .
  4844. .
  4845. [foo<http://example.com?search=][ref]>
  4846. [ref]: /uri
  4847. .
  4848. <p>[foo<a href="http://example.com?search=%5D%5Bref%5D">http://example.com?search=][ref]</a></p>
  4849. .
  4850. Matching is case-insensitive:
  4851. .
  4852. [foo][BaR]
  4853. [bar]: /url "title"
  4854. .
  4855. <p><a href="/url" title="title">foo</a></p>
  4856. .
  4857. Unicode case fold is used:
  4858. .
  4859. [Толпой][Толпой] is a Russian word.
  4860. [ТОЛПОЙ]: /url
  4861. .
  4862. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4863. .
  4864. Consecutive internal [whitespace](#whitespace) is treated as one space for
  4865. purposes of determining matching:
  4866. .
  4867. [Foo
  4868. bar]: /url
  4869. [Baz][Foo bar]
  4870. .
  4871. <p><a href="/url">Baz</a></p>
  4872. .
  4873. There can be [whitespace](#whitespace) between the
  4874. [link text](#link-text) and the [link label](#link-label):
  4875. .
  4876. [foo] [bar]
  4877. [bar]: /url "title"
  4878. .
  4879. <p><a href="/url" title="title">foo</a></p>
  4880. .
  4881. .
  4882. [foo]
  4883. [bar]
  4884. [bar]: /url "title"
  4885. .
  4886. <p><a href="/url" title="title">foo</a></p>
  4887. .
  4888. When there are multiple matching [link reference
  4889. definitions](#link-reference-definition), the first is used:
  4890. .
  4891. [foo]: /url1
  4892. [foo]: /url2
  4893. [bar][foo]
  4894. .
  4895. <p><a href="/url1">bar</a></p>
  4896. .
  4897. Note that matching is performed on normalized strings, not parsed
  4898. inline content. So the following does not match, even though the
  4899. labels define equivalent inline content:
  4900. .
  4901. [bar][foo\!]
  4902. [foo!]: /url
  4903. .
  4904. <p>[bar][foo!]</p>
  4905. .
  4906. [Link labels](#link-label) cannot contain brackets, unless they are
  4907. backslash-escaped:
  4908. .
  4909. [foo][ref[]
  4910. [ref[]: /uri
  4911. .
  4912. <p>[foo][ref[]</p>
  4913. <p>[ref[]: /uri</p>
  4914. .
  4915. .
  4916. [foo][ref[bar]]
  4917. [ref[bar]]: /uri
  4918. .
  4919. <p>[foo][ref[bar]]</p>
  4920. <p>[ref[bar]]: /uri</p>
  4921. .
  4922. .
  4923. [[[foo]]]
  4924. [[[foo]]]: /url
  4925. .
  4926. <p>[[[foo]]]</p>
  4927. <p>[[[foo]]]: /url</p>
  4928. .
  4929. .
  4930. [foo][ref\[]
  4931. [ref\[]: /uri
  4932. .
  4933. <p><a href="/uri">foo</a></p>
  4934. .
  4935. A [collapsed reference link](@collapsed-reference-link)
  4936. consists of a [link
  4937. label](#link-label) that [matches](#matches) a [link reference
  4938. definition](#link-reference-definition) elsewhere in the
  4939. document, optional [whitespace](#whitespace), and the string `[]`.
  4940. The contents of the first link label are parsed as inlines,
  4941. which are used as the link's text. The link's URI and title are
  4942. provided by the matching reference link definition. Thus,
  4943. `[foo][]` is equivalent to `[foo][foo]`.
  4944. .
  4945. [foo][]
  4946. [foo]: /url "title"
  4947. .
  4948. <p><a href="/url" title="title">foo</a></p>
  4949. .
  4950. .
  4951. [*foo* bar][]
  4952. [*foo* bar]: /url "title"
  4953. .
  4954. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4955. .
  4956. The link labels are case-insensitive:
  4957. .
  4958. [Foo][]
  4959. [foo]: /url "title"
  4960. .
  4961. <p><a href="/url" title="title">Foo</a></p>
  4962. .
  4963. As with full reference links, [whitespace](#whitespace) is allowed
  4964. between the two sets of brackets:
  4965. .
  4966. [foo]
  4967. []
  4968. [foo]: /url "title"
  4969. .
  4970. <p><a href="/url" title="title">foo</a></p>
  4971. .
  4972. A [shortcut reference link](@shortcut-reference-link)
  4973. consists of a [link
  4974. label](#link-label) that [matches](#matches) a [link reference
  4975. definition](#link-reference-definition) elsewhere in the
  4976. document and is not followed by `[]` or a link label.
  4977. The contents of the first link label are parsed as inlines,
  4978. which are used as the link's text. the link's URI and title
  4979. are provided by the matching link reference definition.
  4980. Thus, `[foo]` is equivalent to `[foo][]`.
  4981. .
  4982. [foo]
  4983. [foo]: /url "title"
  4984. .
  4985. <p><a href="/url" title="title">foo</a></p>
  4986. .
  4987. .
  4988. [*foo* bar]
  4989. [*foo* bar]: /url "title"
  4990. .
  4991. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4992. .
  4993. .
  4994. [[*foo* bar]]
  4995. [*foo* bar]: /url "title"
  4996. .
  4997. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4998. .
  4999. The link labels are case-insensitive:
  5000. .
  5001. [Foo]
  5002. [foo]: /url "title"
  5003. .
  5004. <p><a href="/url" title="title">Foo</a></p>
  5005. .
  5006. A space after the link text should be preserved:
  5007. .
  5008. [foo] bar
  5009. [foo]: /url
  5010. .
  5011. <p><a href="/url">foo</a> bar</p>
  5012. .
  5013. If you just want bracketed text, you can backslash-escape the
  5014. opening bracket to avoid links:
  5015. .
  5016. \[foo]
  5017. [foo]: /url "title"
  5018. .
  5019. <p>[foo]</p>
  5020. .
  5021. Note that this is a link, because a link label ends with the first
  5022. following closing bracket:
  5023. .
  5024. [foo*]: /url
  5025. *[foo*]
  5026. .
  5027. <p>*<a href="/url">foo*</a></p>
  5028. .
  5029. This is a link too, for the same reason:
  5030. .
  5031. [foo`]: /url
  5032. [foo`]`
  5033. .
  5034. <p>[foo<code>]</code></p>
  5035. .
  5036. Full references take precedence over shortcut references:
  5037. .
  5038. [foo][bar]
  5039. [foo]: /url1
  5040. [bar]: /url2
  5041. .
  5042. <p><a href="/url2">foo</a></p>
  5043. .
  5044. In the following case `[bar][baz]` is parsed as a reference,
  5045. `[foo]` as normal text:
  5046. .
  5047. [foo][bar][baz]
  5048. [baz]: /url
  5049. .
  5050. <p>[foo]<a href="/url">bar</a></p>
  5051. .
  5052. Here, though, `[foo][bar]` is parsed as a reference, since
  5053. `[bar]` is defined:
  5054. .
  5055. [foo][bar][baz]
  5056. [baz]: /url1
  5057. [bar]: /url2
  5058. .
  5059. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  5060. .
  5061. Here `[foo]` is not parsed as a shortcut reference, because it
  5062. is followed by a link label (even though `[bar]` is not defined):
  5063. .
  5064. [foo][bar][baz]
  5065. [baz]: /url1
  5066. [foo]: /url2
  5067. .
  5068. <p>[foo]<a href="/url1">bar</a></p>
  5069. .
  5070. ## Images
  5071. Syntax for images is like the syntax for links, with one
  5072. difference. Instead of [link text](#link-text), we have an [image
  5073. description](@image-description). The rules for this are the
  5074. same as for [link text](#link-text), except that (a) an
  5075. image description starts with `![` rather than `[`, and
  5076. (b) an image description may contain links.
  5077. An image description has inline elements
  5078. as its contents. When an image is rendered to HTML,
  5079. this is standardly used as the image's `alt` attribute.
  5080. .
  5081. ![foo](/url "title")
  5082. .
  5083. <p><img src="/url" alt="foo" title="title" /></p>
  5084. .
  5085. .
  5086. ![foo *bar*]
  5087. [foo *bar*]: train.jpg "train & tracks"
  5088. .
  5089. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5090. .
  5091. .
  5092. ![foo ![bar](/url)](/url2)
  5093. .
  5094. <p><img src="/url2" alt="foo bar" /></p>
  5095. .
  5096. .
  5097. ![foo [bar](/url)](/url2)
  5098. .
  5099. <p><img src="/url2" alt="foo bar" /></p>
  5100. .
  5101. Though this spec is concerned with parsing, not rendering, it is
  5102. recommended that in rendering to HTML, only the plain string content
  5103. of the [image description](#image-description) be used. Note that in
  5104. the above example, the alt attribute's value is `foo bar`, not `foo
  5105. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  5106. content is rendered, without formatting.
  5107. .
  5108. ![foo *bar*][]
  5109. [foo *bar*]: train.jpg "train & tracks"
  5110. .
  5111. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5112. .
  5113. .
  5114. ![foo *bar*][foobar]
  5115. [FOOBAR]: train.jpg "train & tracks"
  5116. .
  5117. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5118. .
  5119. .
  5120. ![foo](train.jpg)
  5121. .
  5122. <p><img src="train.jpg" alt="foo" /></p>
  5123. .
  5124. .
  5125. My ![foo bar](/path/to/train.jpg "title" )
  5126. .
  5127. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  5128. .
  5129. .
  5130. ![foo](<url>)
  5131. .
  5132. <p><img src="url" alt="foo" /></p>
  5133. .
  5134. .
  5135. ![](/url)
  5136. .
  5137. <p><img src="/url" alt="" /></p>
  5138. .
  5139. Reference-style:
  5140. .
  5141. ![foo] [bar]
  5142. [bar]: /url
  5143. .
  5144. <p><img src="/url" alt="foo" /></p>
  5145. .
  5146. .
  5147. ![foo] [bar]
  5148. [BAR]: /url
  5149. .
  5150. <p><img src="/url" alt="foo" /></p>
  5151. .
  5152. Collapsed:
  5153. .
  5154. ![foo][]
  5155. [foo]: /url "title"
  5156. .
  5157. <p><img src="/url" alt="foo" title="title" /></p>
  5158. .
  5159. .
  5160. ![*foo* bar][]
  5161. [*foo* bar]: /url "title"
  5162. .
  5163. <p><img src="/url" alt="foo bar" title="title" /></p>
  5164. .
  5165. The labels are case-insensitive:
  5166. .
  5167. ![Foo][]
  5168. [foo]: /url "title"
  5169. .
  5170. <p><img src="/url" alt="Foo" title="title" /></p>
  5171. .
  5172. As with full reference links, [whitespace](#whitespace) is allowed
  5173. between the two sets of brackets:
  5174. .
  5175. ![foo]
  5176. []
  5177. [foo]: /url "title"
  5178. .
  5179. <p><img src="/url" alt="foo" title="title" /></p>
  5180. .
  5181. Shortcut:
  5182. .
  5183. ![foo]
  5184. [foo]: /url "title"
  5185. .
  5186. <p><img src="/url" alt="foo" title="title" /></p>
  5187. .
  5188. .
  5189. ![*foo* bar]
  5190. [*foo* bar]: /url "title"
  5191. .
  5192. <p><img src="/url" alt="foo bar" title="title" /></p>
  5193. .
  5194. Note that link labels cannot contain unescaped brackets:
  5195. .
  5196. ![[foo]]
  5197. [[foo]]: /url "title"
  5198. .
  5199. <p>![[foo]]</p>
  5200. <p>[[foo]]: /url &quot;title&quot;</p>
  5201. .
  5202. The link labels are case-insensitive:
  5203. .
  5204. ![Foo]
  5205. [foo]: /url "title"
  5206. .
  5207. <p><img src="/url" alt="Foo" title="title" /></p>
  5208. .
  5209. If you just want bracketed text, you can backslash-escape the
  5210. opening `!` and `[`:
  5211. .
  5212. \!\[foo]
  5213. [foo]: /url "title"
  5214. .
  5215. <p>![foo]</p>
  5216. .
  5217. If you want a link after a literal `!`, backslash-escape the
  5218. `!`:
  5219. .
  5220. \![foo]
  5221. [foo]: /url "title"
  5222. .
  5223. <p>!<a href="/url" title="title">foo</a></p>
  5224. .
  5225. ## Autolinks
  5226. [Autolinks](@autolink) are absolute URIs and email addresses inside `<` and `>`.
  5227. They are parsed as links, with the URL or email address as the link
  5228. label.
  5229. A [URI autolink](@uri-autolink)
  5230. consists of `<`, followed by an [absolute
  5231. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  5232. as a link to the URI, with the URI as the link's label.
  5233. An [absolute URI](@absolute-uri),
  5234. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  5235. followed by zero or more characters other than ASCII
  5236. [whitespace](#whitespace) and control characters, `<`, and `>`. If
  5237. the URI includes these characters, you must use percent-encoding
  5238. (e.g. `%20` for a space).
  5239. The following [schemes](@scheme)
  5240. are recognized (case-insensitive):
  5241. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5242. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5243. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5244. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5245. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5246. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5247. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5248. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5249. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5250. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5251. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5252. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5253. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5254. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5255. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5256. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5257. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5258. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5259. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5260. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5261. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5262. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5263. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5264. Here are some valid autolinks:
  5265. .
  5266. <http://foo.bar.baz>
  5267. .
  5268. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5269. .
  5270. .
  5271. <http://foo.bar.baz?q=hello&id=22&boolean>
  5272. .
  5273. <p><a href="http://foo.bar.baz?q=hello&amp;id=22&amp;boolean">http://foo.bar.baz?q=hello&amp;id=22&amp;boolean</a></p>
  5274. .
  5275. .
  5276. <irc://foo.bar:2233/baz>
  5277. .
  5278. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5279. .
  5280. Uppercase is also fine:
  5281. .
  5282. <MAILTO:FOO@BAR.BAZ>
  5283. .
  5284. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5285. .
  5286. Spaces are not allowed in autolinks:
  5287. .
  5288. <http://foo.bar/baz bim>
  5289. .
  5290. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5291. .
  5292. An [email autolink](@email-autolink)
  5293. consists of `<`, followed by an [email address](#email-address),
  5294. followed by `>`. The link's label is the email address,
  5295. and the URL is `mailto:` followed by the email address.
  5296. An [email address](@email-address),
  5297. for these purposes, is anything that matches
  5298. the [non-normative regex from the HTML5
  5299. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  5300. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5301. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5302. Examples of email autolinks:
  5303. .
  5304. <foo@bar.example.com>
  5305. .
  5306. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5307. .
  5308. .
  5309. <foo+special@Bar.baz-bar0.com>
  5310. .
  5311. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5312. .
  5313. These are not autolinks:
  5314. .
  5315. <>
  5316. .
  5317. <p>&lt;&gt;</p>
  5318. .
  5319. .
  5320. <heck://bing.bong>
  5321. .
  5322. <p>&lt;heck://bing.bong&gt;</p>
  5323. .
  5324. .
  5325. < http://foo.bar >
  5326. .
  5327. <p>&lt; http://foo.bar &gt;</p>
  5328. .
  5329. .
  5330. <foo.bar.baz>
  5331. .
  5332. <p>&lt;foo.bar.baz&gt;</p>
  5333. .
  5334. .
  5335. <localhost:5001/foo>
  5336. .
  5337. <p>&lt;localhost:5001/foo&gt;</p>
  5338. .
  5339. .
  5340. http://example.com
  5341. .
  5342. <p>http://example.com</p>
  5343. .
  5344. .
  5345. foo@bar.example.com
  5346. .
  5347. <p>foo@bar.example.com</p>
  5348. .
  5349. ## Raw HTML
  5350. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5351. raw HTML tag and will be rendered in HTML without escaping.
  5352. Tag and attribute names are not limited to current HTML tags,
  5353. so custom tags (and even, say, DocBook tags) may be used.
  5354. Here is the grammar for tags:
  5355. A [tag name](@tag-name) consists of an ASCII letter
  5356. followed by zero or more ASCII letters or digits.
  5357. An [attribute](@attribute) consists of [whitespace](#whitespace),
  5358. an [attribute name](#attribute-name), and an optional
  5359. [attribute value specification](#attribute-value-specification).
  5360. An [attribute name](@attribute-name)
  5361. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5362. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5363. specification restricted to ASCII. HTML5 is laxer.)
  5364. An [attribute value specification](@attribute-value-specification)
  5365. consists of optional [whitespace](#whitespace),
  5366. a `=` character, optional [whitespace](#whitespace), and an [attribute
  5367. value](#attribute-value).
  5368. An [attribute value](@attribute-value)
  5369. consists of an [unquoted attribute value](#unquoted-attribute-value),
  5370. a [single-quoted attribute value](#single-quoted-attribute-value),
  5371. or a [double-quoted attribute value](#double-quoted-attribute-value).
  5372. An [unquoted attribute value](@unquoted-attribute-value)
  5373. is a nonempty string of characters not
  5374. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5375. A [single-quoted attribute value](@single-quoted-attribute-value)
  5376. consists of `'`, zero or more
  5377. characters not including `'`, and a final `'`.
  5378. A [double-quoted attribute value](@double-quoted-attribute-value)
  5379. consists of `"`, zero or more
  5380. characters not including `"`, and a final `"`.
  5381. An [open tag](@open-tag) consists of a `<` character,
  5382. a [tag name](#tag-name), zero or more [attributes](#attribute),
  5383. optional [whitespace](#whitespace), an optional `/` character, and a
  5384. `>` character.
  5385. A [closing tag](@closing-tag) consists of the
  5386. string `</`, a [tag name](#tag-name), optional
  5387. [whitespace](#whitespace), and the character `>`.
  5388. An [HTML comment](@html-comment) consists of the
  5389. string `<!--`, a string of characters not including the string `--`, and
  5390. the string `-->`.
  5391. A [processing instruction](@processing-instruction)
  5392. consists of the string `<?`, a string
  5393. of characters not including the string `?>`, and the string
  5394. `?>`.
  5395. A [declaration](@declaration) consists of the
  5396. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5397. [whitespace](#whitespace), a string of characters not including the
  5398. character `>`, and the character `>`.
  5399. A [CDATA section](@cdata-section) consists of
  5400. the string `<![CDATA[`, a string of characters not including the string
  5401. `]]>`, and the string `]]>`.
  5402. An [HTML tag](@html-tag) consists of an [open
  5403. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  5404. comment](#html-comment), a [processing instruction](#processing-instruction),
  5405. a [declaration](#declaration), or a [CDATA section](#cdata-section).
  5406. Here are some simple open tags:
  5407. .
  5408. <a><bab><c2c>
  5409. .
  5410. <p><a><bab><c2c></p>
  5411. .
  5412. Empty elements:
  5413. .
  5414. <a/><b2/>
  5415. .
  5416. <p><a/><b2/></p>
  5417. .
  5418. [Whitespace](#whitespace) is allowed:
  5419. .
  5420. <a /><b2
  5421. data="foo" >
  5422. .
  5423. <p><a /><b2
  5424. data="foo" ></p>
  5425. .
  5426. With attributes:
  5427. .
  5428. <a foo="bar" bam = 'baz <em>"</em>'
  5429. _boolean zoop:33=zoop:33 />
  5430. .
  5431. <p><a foo="bar" bam = 'baz <em>"</em>'
  5432. _boolean zoop:33=zoop:33 /></p>
  5433. .
  5434. Illegal tag names, not parsed as HTML:
  5435. .
  5436. <33> <__>
  5437. .
  5438. <p>&lt;33&gt; &lt;__&gt;</p>
  5439. .
  5440. Illegal attribute names:
  5441. .
  5442. <a h*#ref="hi">
  5443. .
  5444. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5445. .
  5446. Illegal attribute values:
  5447. .
  5448. <a href="hi'> <a href=hi'>
  5449. .
  5450. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5451. .
  5452. Illegal [whitespace](#whitespace):
  5453. .
  5454. < a><
  5455. foo><bar/ >
  5456. .
  5457. <p>&lt; a&gt;&lt;
  5458. foo&gt;&lt;bar/ &gt;</p>
  5459. .
  5460. Missing [whitespace](#whitespace):
  5461. .
  5462. <a href='bar'title=title>
  5463. .
  5464. <p>&lt;a href='bar'title=title&gt;</p>
  5465. .
  5466. Closing tags:
  5467. .
  5468. </a>
  5469. </foo >
  5470. .
  5471. <p></a>
  5472. </foo ></p>
  5473. .
  5474. Illegal attributes in closing tag:
  5475. .
  5476. </a href="foo">
  5477. .
  5478. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5479. .
  5480. Comments:
  5481. .
  5482. foo <!-- this is a
  5483. comment - with hyphen -->
  5484. .
  5485. <p>foo <!-- this is a
  5486. comment - with hyphen --></p>
  5487. .
  5488. .
  5489. foo <!-- not a comment -- two hyphens -->
  5490. .
  5491. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5492. .
  5493. Processing instructions:
  5494. .
  5495. foo <?php echo $a; ?>
  5496. .
  5497. <p>foo <?php echo $a; ?></p>
  5498. .
  5499. Declarations:
  5500. .
  5501. foo <!ELEMENT br EMPTY>
  5502. .
  5503. <p>foo <!ELEMENT br EMPTY></p>
  5504. .
  5505. CDATA sections:
  5506. .
  5507. foo <![CDATA[>&<]]>
  5508. .
  5509. <p>foo <![CDATA[>&<]]></p>
  5510. .
  5511. Entities are preserved in HTML attributes:
  5512. .
  5513. <a href="&ouml;">
  5514. .
  5515. <p><a href="&ouml;"></p>
  5516. .
  5517. Backslash escapes do not work in HTML attributes:
  5518. .
  5519. <a href="\*">
  5520. .
  5521. <p><a href="\*"></p>
  5522. .
  5523. .
  5524. <a href="\"">
  5525. .
  5526. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5527. .
  5528. ## Hard line breaks
  5529. A line break (not in a code span or HTML tag) that is preceded
  5530. by two or more spaces and does not occur at the end of a block
  5531. is parsed as a [hard line break](@hard-line-break) (rendered
  5532. in HTML as a `<br />` tag):
  5533. .
  5534. foo
  5535. baz
  5536. .
  5537. <p>foo<br />
  5538. baz</p>
  5539. .
  5540. For a more visible alternative, a backslash before the
  5541. [line ending](#line-ending) may be used instead of two spaces:
  5542. .
  5543. foo\
  5544. baz
  5545. .
  5546. <p>foo<br />
  5547. baz</p>
  5548. .
  5549. More than two spaces can be used:
  5550. .
  5551. foo
  5552. baz
  5553. .
  5554. <p>foo<br />
  5555. baz</p>
  5556. .
  5557. Leading spaces at the beginning of the next line are ignored:
  5558. .
  5559. foo
  5560. bar
  5561. .
  5562. <p>foo<br />
  5563. bar</p>
  5564. .
  5565. .
  5566. foo\
  5567. bar
  5568. .
  5569. <p>foo<br />
  5570. bar</p>
  5571. .
  5572. Line breaks can occur inside emphasis, links, and other constructs
  5573. that allow inline content:
  5574. .
  5575. *foo
  5576. bar*
  5577. .
  5578. <p><em>foo<br />
  5579. bar</em></p>
  5580. .
  5581. .
  5582. *foo\
  5583. bar*
  5584. .
  5585. <p><em>foo<br />
  5586. bar</em></p>
  5587. .
  5588. Line breaks do not occur inside code spans
  5589. .
  5590. `code
  5591. span`
  5592. .
  5593. <p><code>code span</code></p>
  5594. .
  5595. .
  5596. `code\
  5597. span`
  5598. .
  5599. <p><code>code\ span</code></p>
  5600. .
  5601. or HTML tags:
  5602. .
  5603. <a href="foo
  5604. bar">
  5605. .
  5606. <p><a href="foo
  5607. bar"></p>
  5608. .
  5609. .
  5610. <a href="foo\
  5611. bar">
  5612. .
  5613. <p><a href="foo\
  5614. bar"></p>
  5615. .
  5616. Hard line breaks are for separating inline content within a block.
  5617. Neither syntax for hard line breaks works at the end of a paragraph or
  5618. other block element:
  5619. .
  5620. foo\
  5621. .
  5622. <p>foo\</p>
  5623. .
  5624. .
  5625. foo
  5626. .
  5627. <p>foo</p>
  5628. .
  5629. .
  5630. ### foo\
  5631. .
  5632. <h3>foo\</h3>
  5633. .
  5634. .
  5635. ### foo
  5636. .
  5637. <h3>foo</h3>
  5638. .
  5639. ## Soft line breaks
  5640. A regular line break (not in a code span or HTML tag) that is not
  5641. preceded by two or more spaces is parsed as a softbreak. (A
  5642. softbreak may be rendered in HTML either as a
  5643. [line ending](#line-ending) or as a space. The result will be the same
  5644. in browsers. In the examples here, a [line ending](#line-ending) will
  5645. be used.)
  5646. .
  5647. foo
  5648. baz
  5649. .
  5650. <p>foo
  5651. baz</p>
  5652. .
  5653. Spaces at the end of the line and beginning of the next line are
  5654. removed:
  5655. .
  5656. foo
  5657. baz
  5658. .
  5659. <p>foo
  5660. baz</p>
  5661. .
  5662. A conforming parser may render a soft line break in HTML either as a
  5663. line break or as a space.
  5664. A renderer may also provide an option to render soft line breaks
  5665. as hard line breaks.
  5666. ## Textual content
  5667. Any characters not given an interpretation by the above rules will
  5668. be parsed as plain textual content.
  5669. .
  5670. hello $.;'there
  5671. .
  5672. <p>hello $.;'there</p>
  5673. .
  5674. .
  5675. Foo χρῆν
  5676. .
  5677. <p>Foo χρῆν</p>
  5678. .
  5679. Internal spaces are preserved verbatim:
  5680. .
  5681. Multiple spaces
  5682. .
  5683. <p>Multiple spaces</p>
  5684. .
  5685. <!-- END TESTS -->
  5686. # Appendix A: A parsing strategy {-}
  5687. ## Overview {-}
  5688. Parsing has two phases:
  5689. 1. In the first phase, lines of input are consumed and the block
  5690. structure of the document---its division into paragraphs, block quotes,
  5691. list items, and so on---is constructed. Text is assigned to these
  5692. blocks but not parsed. Link reference definitions are parsed and a
  5693. map of links is constructed.
  5694. 2. In the second phase, the raw text contents of paragraphs and headers
  5695. are parsed into sequences of Markdown inline elements (strings,
  5696. code spans, links, emphasis, and so on), using the map of link
  5697. references constructed in phase 1.
  5698. ## The document tree {-}
  5699. At each point in processing, the document is represented as a tree of
  5700. **blocks**. The root of the tree is a `document` block. The `document`
  5701. may have any number of other blocks as **children**. These children
  5702. may, in turn, have other blocks as children. The last child of a block
  5703. is normally considered **open**, meaning that subsequent lines of input
  5704. can alter its contents. (Blocks that are not open are **closed**.)
  5705. Here, for example, is a possible document tree, with the open blocks
  5706. marked by arrows:
  5707. ``` tree
  5708. -> document
  5709. -> block_quote
  5710. paragraph
  5711. "Lorem ipsum dolor\nsit amet."
  5712. -> list (type=bullet tight=true bullet_char=-)
  5713. list_item
  5714. paragraph
  5715. "Qui *quodsi iracundia*"
  5716. -> list_item
  5717. -> paragraph
  5718. "aliquando id"
  5719. ```
  5720. ## How source lines alter the document tree {-}
  5721. Each line that is processed has an effect on this tree. The line is
  5722. analyzed and, depending on its contents, the document may be altered
  5723. in one or more of the following ways:
  5724. 1. One or more open blocks may be closed.
  5725. 2. One or more new blocks may be created as children of the
  5726. last open block.
  5727. 3. Text may be added to the last (deepest) open block remaining
  5728. on the tree.
  5729. Once a line has been incorporated into the tree in this way,
  5730. it can be discarded, so input can be read in a stream.
  5731. We can see how this works by considering how the tree above is
  5732. generated by four lines of Markdown:
  5733. ``` markdown
  5734. > Lorem ipsum dolor
  5735. sit amet.
  5736. > - Qui *quodsi iracundia*
  5737. > - aliquando id
  5738. ```
  5739. At the outset, our document model is just
  5740. ``` tree
  5741. -> document
  5742. ```
  5743. The first line of our text,
  5744. ``` markdown
  5745. > Lorem ipsum dolor
  5746. ```
  5747. causes a `block_quote` block to be created as a child of our
  5748. open `document` block, and a `paragraph` block as a child of
  5749. the `block_quote`. Then the text is added to the last open
  5750. block, the `paragraph`:
  5751. ``` tree
  5752. -> document
  5753. -> block_quote
  5754. -> paragraph
  5755. "Lorem ipsum dolor"
  5756. ```
  5757. The next line,
  5758. ``` markdown
  5759. sit amet.
  5760. ```
  5761. is a "lazy continuation" of the open `paragraph`, so it gets added
  5762. to the paragraph's text:
  5763. ``` tree
  5764. -> document
  5765. -> block_quote
  5766. -> paragraph
  5767. "Lorem ipsum dolor\nsit amet."
  5768. ```
  5769. The third line,
  5770. ``` markdown
  5771. > - Qui *quodsi iracundia*
  5772. ```
  5773. causes the `paragraph` block to be closed, and a new `list` block
  5774. opened as a child of the `block_quote`. A `list_item` is also
  5775. added as a child of the `list`, and a `paragraph` as a child of
  5776. the `list_item`. The text is then added to the new `paragraph`:
  5777. ``` tree
  5778. -> document
  5779. -> block_quote
  5780. paragraph
  5781. "Lorem ipsum dolor\nsit amet."
  5782. -> list (type=bullet tight=true bullet_char=-)
  5783. -> list_item
  5784. -> paragraph
  5785. "Qui *quodsi iracundia*"
  5786. ```
  5787. The fourth line,
  5788. ``` markdown
  5789. > - aliquando id
  5790. ```
  5791. causes the `list_item` (and its child the `paragraph`) to be closed,
  5792. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5793. is added as a child of the new `list_item`, to contain the text.
  5794. We thus obtain the final tree:
  5795. ``` tree
  5796. -> document
  5797. -> block_quote
  5798. paragraph
  5799. "Lorem ipsum dolor\nsit amet."
  5800. -> list (type=bullet tight=true bullet_char=-)
  5801. list_item
  5802. paragraph
  5803. "Qui *quodsi iracundia*"
  5804. -> list_item
  5805. -> paragraph
  5806. "aliquando id"
  5807. ```
  5808. ## From block structure to the final document {-}
  5809. Once all of the input has been parsed, all open blocks are closed.
  5810. We then "walk the tree," visiting every node, and parse raw
  5811. string contents of paragraphs and headers as inlines. At this
  5812. point we have seen all the link reference definitions, so we can
  5813. resolve reference links as we go.
  5814. ``` tree
  5815. document
  5816. block_quote
  5817. paragraph
  5818. str "Lorem ipsum dolor"
  5819. softbreak
  5820. str "sit amet."
  5821. list (type=bullet tight=true bullet_char=-)
  5822. list_item
  5823. paragraph
  5824. str "Qui "
  5825. emph
  5826. str "quodsi iracundia"
  5827. list_item
  5828. paragraph
  5829. str "aliquando id"
  5830. ```
  5831. Notice how the [line ending](#line-ending) in the first paragraph has
  5832. been parsed as a `softbreak`, and the asterisks in the first list item
  5833. have become an `emph`.
  5834. The document can be rendered as HTML, or in any other format, given
  5835. an appropriate renderer.