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