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