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