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