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