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