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