aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 25e0cc014dbd9c28d4d951478c2f9d0662f977b3 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.15
  5. date: 2014-12-31
  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. Here the outer list is loose, the inner list tight:
  3228. .
  3229. * foo
  3230. * bar
  3231. baz
  3232. .
  3233. <ul>
  3234. <li>
  3235. <p>foo</p>
  3236. <ul>
  3237. <li>bar</li>
  3238. </ul>
  3239. <p>baz</p>
  3240. </li>
  3241. </ul>
  3242. .
  3243. .
  3244. - a
  3245. - b
  3246. - c
  3247. - d
  3248. - e
  3249. - f
  3250. .
  3251. <ul>
  3252. <li>
  3253. <p>a</p>
  3254. <ul>
  3255. <li>b</li>
  3256. <li>c</li>
  3257. </ul>
  3258. </li>
  3259. <li>
  3260. <p>d</p>
  3261. <ul>
  3262. <li>e</li>
  3263. <li>f</li>
  3264. </ul>
  3265. </li>
  3266. </ul>
  3267. .
  3268. # Inlines
  3269. Inlines are parsed sequentially from the beginning of the character
  3270. stream to the end (left to right, in left-to-right languages).
  3271. Thus, for example, in
  3272. .
  3273. `hi`lo`
  3274. .
  3275. <p><code>hi</code>lo`</p>
  3276. .
  3277. `hi` is parsed as code, leaving the backtick at the end as a literal
  3278. backtick.
  3279. ## Backslash escapes
  3280. Any ASCII punctuation character may be backslash-escaped:
  3281. .
  3282. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3283. .
  3284. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3285. .
  3286. Backslashes before other characters are treated as literal
  3287. backslashes:
  3288. .
  3289. \→\A\a\ \3\φ\«
  3290. .
  3291. <p>\ \A\a\ \3\φ\«</p>
  3292. .
  3293. Escaped characters are treated as regular characters and do
  3294. not have their usual Markdown meanings:
  3295. .
  3296. \*not emphasized*
  3297. \<br/> not a tag
  3298. \[not a link](/foo)
  3299. \`not code`
  3300. 1\. not a list
  3301. \* not a list
  3302. \# not a header
  3303. \[foo]: /url "not a reference"
  3304. .
  3305. <p>*not emphasized*
  3306. &lt;br/&gt; not a tag
  3307. [not a link](/foo)
  3308. `not code`
  3309. 1. not a list
  3310. * not a list
  3311. # not a header
  3312. [foo]: /url &quot;not a reference&quot;</p>
  3313. .
  3314. If a backslash is itself escaped, the following character is not:
  3315. .
  3316. \\*emphasis*
  3317. .
  3318. <p>\<em>emphasis</em></p>
  3319. .
  3320. A backslash at the end of the line is a [hard line break]:
  3321. .
  3322. foo\
  3323. bar
  3324. .
  3325. <p>foo<br />
  3326. bar</p>
  3327. .
  3328. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3329. raw HTML:
  3330. .
  3331. `` \[\` ``
  3332. .
  3333. <p><code>\[\`</code></p>
  3334. .
  3335. .
  3336. \[\]
  3337. .
  3338. <pre><code>\[\]
  3339. </code></pre>
  3340. .
  3341. .
  3342. ~~~
  3343. \[\]
  3344. ~~~
  3345. .
  3346. <pre><code>\[\]
  3347. </code></pre>
  3348. .
  3349. .
  3350. <http://example.com?find=\*>
  3351. .
  3352. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3353. .
  3354. .
  3355. <a href="/bar\/)">
  3356. .
  3357. <p><a href="/bar\/)"></p>
  3358. .
  3359. But they work in all other contexts, including URLs and link titles,
  3360. link references, and [info string]s in [fenced code block]s:
  3361. .
  3362. [foo](/bar\* "ti\*tle")
  3363. .
  3364. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3365. .
  3366. .
  3367. [foo]
  3368. [foo]: /bar\* "ti\*tle"
  3369. .
  3370. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3371. .
  3372. .
  3373. ``` foo\+bar
  3374. foo
  3375. ```
  3376. .
  3377. <pre><code class="language-foo+bar">foo
  3378. </code></pre>
  3379. .
  3380. ## Entities
  3381. With the goal of making this standard as HTML-agnostic as possible, all
  3382. valid HTML entities in any context are recognized as such and
  3383. converted into unicode characters before they are stored in the AST.
  3384. This allows implementations that target HTML output to trivially escape
  3385. the entities when generating HTML, and simplifies the job of
  3386. implementations targetting other languages, as these will only need to
  3387. handle the unicode chars and need not be HTML-entity aware.
  3388. [Named entities](@name-entities) consist of `&`
  3389. + any of the valid HTML5 entity names + `;`. The
  3390. [following document](https://html.spec.whatwg.org/multipage/entities.json)
  3391. is used as an authoritative source of the valid entity names and their
  3392. corresponding codepoints.
  3393. Conforming implementations that target HTML don't need to generate
  3394. entities for all the valid named entities that exist, with the exception
  3395. of `"` (`&quot;`), `&` (`&amp;`), `<` (`&lt;`) and `>` (`&gt;`), which
  3396. always need to be written as entities for security reasons.
  3397. .
  3398. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3399. .
  3400. <p>  &amp; © Æ Ď ¾ ℋ ⅆ ∲</p>
  3401. .
  3402. [Decimal entities](@decimal-entities)
  3403. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3404. entities need to be recognised and tranformed into their corresponding
  3405. UTF8 codepoints. Invalid Unicode codepoints will be written as the
  3406. "unknown codepoint" character (`0xFFFD`)
  3407. .
  3408. &#35; &#1234; &#992; &#98765432;
  3409. .
  3410. <p># Ӓ Ϡ �</p>
  3411. .
  3412. [Hexadecimal entities](@hexadecimal-entities)
  3413. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3414. + `;`. They will also be parsed and turned into their corresponding UTF8 values in the AST.
  3415. .
  3416. &#X22; &#XD06; &#xcab;
  3417. .
  3418. <p>&quot; ആ ಫ</p>
  3419. .
  3420. Here are some nonentities:
  3421. .
  3422. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3423. .
  3424. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3425. .
  3426. Although HTML5 does accept some entities without a trailing semicolon
  3427. (such as `&copy`), these are not recognized as entities here, because it
  3428. makes the grammar too ambiguous:
  3429. .
  3430. &copy
  3431. .
  3432. <p>&amp;copy</p>
  3433. .
  3434. Strings that are not on the list of HTML5 named entities are not
  3435. recognized as entities either:
  3436. .
  3437. &MadeUpEntity;
  3438. .
  3439. <p>&amp;MadeUpEntity;</p>
  3440. .
  3441. Entities are recognized in any context besides code spans or
  3442. code blocks, including raw HTML, URLs, [link title]s, and
  3443. [fenced code block] [info string]s:
  3444. .
  3445. <a href="&ouml;&ouml;.html">
  3446. .
  3447. <p><a href="&ouml;&ouml;.html"></p>
  3448. .
  3449. .
  3450. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3451. .
  3452. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3453. .
  3454. .
  3455. [foo]
  3456. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3457. .
  3458. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3459. .
  3460. .
  3461. ``` f&ouml;&ouml;
  3462. foo
  3463. ```
  3464. .
  3465. <pre><code class="language-föö">foo
  3466. </code></pre>
  3467. .
  3468. Entities are treated as literal text in code spans and code blocks:
  3469. .
  3470. `f&ouml;&ouml;`
  3471. .
  3472. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3473. .
  3474. .
  3475. f&ouml;f&ouml;
  3476. .
  3477. <pre><code>f&amp;ouml;f&amp;ouml;
  3478. </code></pre>
  3479. .
  3480. ## Code spans
  3481. A [backtick string](@backtick-string)
  3482. is a string of one or more backtick characters (`` ` ``) that is neither
  3483. preceded nor followed by a backtick.
  3484. A [code span](@code-span) begins with a backtick string and ends with
  3485. a backtick string of equal length. The contents of the code span are
  3486. the characters between the two backtick strings, with leading and
  3487. trailing spaces and [line ending]s removed, and
  3488. [whitespace] collapsed to single spaces.
  3489. This is a simple code span:
  3490. .
  3491. `foo`
  3492. .
  3493. <p><code>foo</code></p>
  3494. .
  3495. Here two backticks are used, because the code contains a backtick.
  3496. This example also illustrates stripping of leading and trailing spaces:
  3497. .
  3498. `` foo ` bar ``
  3499. .
  3500. <p><code>foo ` bar</code></p>
  3501. .
  3502. This example shows the motivation for stripping leading and trailing
  3503. spaces:
  3504. .
  3505. ` `` `
  3506. .
  3507. <p><code>``</code></p>
  3508. .
  3509. [Line ending]s are treated like spaces:
  3510. .
  3511. ``
  3512. foo
  3513. ``
  3514. .
  3515. <p><code>foo</code></p>
  3516. .
  3517. Interior spaces and [line ending]s are collapsed into
  3518. single spaces, just as they would be by a browser:
  3519. .
  3520. `foo bar
  3521. baz`
  3522. .
  3523. <p><code>foo bar baz</code></p>
  3524. .
  3525. Q: Why not just leave the spaces, since browsers will collapse them
  3526. anyway? A: Because we might be targeting a non-HTML format, and we
  3527. shouldn't rely on HTML-specific rendering assumptions.
  3528. (Existing implementations differ in their treatment of internal
  3529. spaces and [line ending]s. Some, including `Markdown.pl` and
  3530. `showdown`, convert an internal [line ending] into a
  3531. `<br />` tag. But this makes things difficult for those who like to
  3532. hard-wrap their paragraphs, since a line break in the midst of a code
  3533. span will cause an unintended line break in the output. Others just
  3534. leave internal spaces as they are, which is fine if only HTML is being
  3535. targeted.)
  3536. .
  3537. `foo `` bar`
  3538. .
  3539. <p><code>foo `` bar</code></p>
  3540. .
  3541. Note that backslash escapes do not work in code spans. All backslashes
  3542. are treated literally:
  3543. .
  3544. `foo\`bar`
  3545. .
  3546. <p><code>foo\</code>bar`</p>
  3547. .
  3548. Backslash escapes are never needed, because one can always choose a
  3549. string of *n* backtick characters as delimiters, where the code does
  3550. not contain any strings of exactly *n* backtick characters.
  3551. Code span backticks have higher precedence than any other inline
  3552. constructs except HTML tags and autolinks. Thus, for example, this is
  3553. not parsed as emphasized text, since the second `*` is part of a code
  3554. span:
  3555. .
  3556. *foo`*`
  3557. .
  3558. <p>*foo<code>*</code></p>
  3559. .
  3560. And this is not parsed as a link:
  3561. .
  3562. [not a `link](/foo`)
  3563. .
  3564. <p>[not a <code>link](/foo</code>)</p>
  3565. .
  3566. Code spans, HTML tags, and autolinks have the same precedence.
  3567. Thus, this is code:
  3568. .
  3569. `<a href="`">`
  3570. .
  3571. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  3572. .
  3573. But this is an HTML tag:
  3574. .
  3575. <a href="`">`
  3576. .
  3577. <p><a href="`">`</p>
  3578. .
  3579. And this is code:
  3580. .
  3581. `<http://foo.bar.`baz>`
  3582. .
  3583. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  3584. .
  3585. But this is an autolink:
  3586. .
  3587. <http://foo.bar.`baz>`
  3588. .
  3589. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3590. .
  3591. When a backtick string is not closed by a matching backtick string,
  3592. we just have literal backticks:
  3593. .
  3594. ```foo``
  3595. .
  3596. <p>```foo``</p>
  3597. .
  3598. .
  3599. `foo
  3600. .
  3601. <p>`foo</p>
  3602. .
  3603. ## Emphasis and strong emphasis
  3604. John Gruber's original [Markdown syntax
  3605. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3606. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3607. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3608. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3609. > tag.
  3610. This is enough for most users, but these rules leave much undecided,
  3611. especially when it comes to nested emphasis. The original
  3612. `Markdown.pl` test suite makes it clear that triple `***` and
  3613. `___` delimiters can be used for strong emphasis, and most
  3614. implementations have also allowed the following patterns:
  3615. ``` markdown
  3616. ***strong emph***
  3617. ***strong** in emph*
  3618. ***emph* in strong**
  3619. **in strong *emph***
  3620. *in emph **strong***
  3621. ```
  3622. The following patterns are less widely supported, but the intent
  3623. is clear and they are useful (especially in contexts like bibliography
  3624. entries):
  3625. ``` markdown
  3626. *emph *with emph* in it*
  3627. **strong **with strong** in it**
  3628. ```
  3629. Many implementations have also restricted intraword emphasis to
  3630. the `*` forms, to avoid unwanted emphasis in words containing
  3631. internal underscores. (It is best practice to put these in code
  3632. spans, but users often do not.)
  3633. ``` markdown
  3634. internal emphasis: foo*bar*baz
  3635. no emphasis: foo_bar_baz
  3636. ```
  3637. The rules given below capture all of these patterns, while allowing
  3638. for efficient parsing strategies that do not backtrack.
  3639. First, some definitions. A [delimiter run](@delimiter-run) is either
  3640. a sequence of one or more `*` characters that is not preceded or
  3641. followed by a `*` character, or a sequence of one or more `_`
  3642. characters that is not preceded or followed by a `_` character.
  3643. A [left-flanking delimiter run](@left-flanking-delimiter-run) is
  3644. a [delimiter run] that is (a) not followed by [unicode whitespace],
  3645. and (b) either not followed by a [punctuation character], or
  3646. preceded by [unicode whitespace] or a [punctuation character].
  3647. A [right-flanking delimiter run](@right-flanking-delimiter-run) is
  3648. a [delimiter run] that is (a) not preceded by [unicode whitespace],
  3649. and (b) either not preceded by a [punctuation character], or
  3650. followed by [unicode whitespace] or a [punctuation character].
  3651. Here are some examples of delimiter runs.
  3652. - left-flanking but not right-flanking:
  3653. ```
  3654. ***abc
  3655. _abc
  3656. **"abc"
  3657. _"abc"
  3658. ```
  3659. - right-flanking but not left-flanking:
  3660. ```
  3661. abc***
  3662. abc_
  3663. "abc"**
  3664. _"abc"
  3665. ```
  3666. - Both right and right-flanking:
  3667. ```
  3668. abc***def
  3669. "abc"_"def"
  3670. ```
  3671. - Neither right nor right-flanking:
  3672. ```
  3673. abc *** def
  3674. a _ b
  3675. ```
  3676. (The idea of distinguishing left-flanking and right-flanking
  3677. delimiter runs based on the character before and the character
  3678. after comes from Roopesh Chander's
  3679. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  3680. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  3681. run," and its rules for distinguishing left- and right-flanking runs
  3682. are a bit more complex than the ones given here.)
  3683. The following rules define emphasis and strong emphasis:
  3684. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3685. iff it is part of a [left-flanking delimiter run].
  3686. 2. A single `_` character [can open emphasis] iff
  3687. it is part of a [left-flanking delimiter run]
  3688. and is not preceded by an ASCII alphanumeric character.
  3689. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3690. iff it is part of a [right-flanking delimiter run].
  3691. 4. A single `_` character [can close emphasis]
  3692. iff it is part of a [right-flanking delimiter run].
  3693. and it is not followed by an ASCII alphanumeric character.
  3694. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3695. iff it is part of a [left-flanking delimiter run].
  3696. 6. A double `__` [can open strong emphasis]
  3697. iff it is part of a [left-flanking delimiter run]
  3698. and is not preceded by an ASCII alphanumeric character.
  3699. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3700. iff it is part of a [right-flanking delimiter run].
  3701. 8. A double `__` [can close strong emphasis]
  3702. iff it is part of a [right-flanking delimiter run]
  3703. and is not followed by an ASCII alphanumeric character.
  3704. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  3705. with a delimiter that [can close emphasis], and that uses the same
  3706. character (`_` or `*`) as the opening delimiter. There must
  3707. be a nonempty sequence of inlines between the open delimiter
  3708. and the closing delimiter; these form the contents of the emphasis
  3709. inline.
  3710. 10. Strong emphasis begins with a delimiter that
  3711. [can open strong emphasis] and ends with a delimiter that
  3712. [can close strong emphasis], and that uses the same character
  3713. (`_` or `*`) as the opening delimiter.
  3714. There must be a nonempty sequence of inlines between the open
  3715. delimiter and the closing delimiter; these form the contents of
  3716. the strong emphasis inline.
  3717. 11. A literal `*` character cannot occur at the beginning or end of
  3718. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3719. is backslash-escaped.
  3720. 12. A literal `_` character cannot occur at the beginning or end of
  3721. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3722. is backslash-escaped.
  3723. Where rules 1--12 above are compatible with multiple parsings,
  3724. the following principles resolve ambiguity:
  3725. 13. The number of nestings should be minimized. Thus, for example,
  3726. an interpretation `<strong>...</strong>` is always preferred to
  3727. `<em><em>...</em></em>`.
  3728. 14. An interpretation `<strong><em>...</em></strong>` is always
  3729. preferred to `<em><strong>..</strong></em>`.
  3730. 15. When two potential emphasis or strong emphasis spans overlap,
  3731. so that the second begins before the first ends and ends after
  3732. the first ends, the first takes precedence. Thus, for example,
  3733. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3734. than `*foo <em>bar* baz</em>`. For the same reason,
  3735. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3736. rather than `<strong>foo*bar</strong>`.
  3737. 16. When there are two potential emphasis or strong emphasis spans
  3738. with the same closing delimiter, the shorter one (the one that
  3739. opens later) takes precedence. Thus, for example,
  3740. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3741. rather than `<strong>foo **bar baz</strong>`.
  3742. 17. Inline code spans, links, images, and HTML tags group more tightly
  3743. than emphasis. So, when there is a choice between an interpretation
  3744. that contains one of these elements and one that does not, the
  3745. former always wins. Thus, for example, `*[foo*](bar)` is
  3746. parsed as `*<a href="bar">foo*</a>` rather than as
  3747. `<em>[foo</em>](bar)`.
  3748. These rules can be illustrated through a series of examples.
  3749. Rule 1:
  3750. .
  3751. *foo bar*
  3752. .
  3753. <p><em>foo bar</em></p>
  3754. .
  3755. This is not emphasis, because the opening `*` is followed by
  3756. whitespace, and hence not part of a [left-flanking delimiter run]:
  3757. .
  3758. a * foo bar*
  3759. .
  3760. <p>a * foo bar*</p>
  3761. .
  3762. This is not emphasis, because the opening `*` is preceded
  3763. by an alphanumeric and followed by punctuation, and hence
  3764. not part of a [left-flanking delimiter run]:
  3765. .
  3766. a*"foo"*
  3767. .
  3768. <p>a*&quot;foo&quot;*</p>
  3769. .
  3770. Unicode nonbreaking spaces count as whitespace, too:
  3771. .
  3772. * a *
  3773. .
  3774. <p>* a *</p>
  3775. .
  3776. Intraword emphasis with `*` is permitted:
  3777. .
  3778. foo*bar*
  3779. .
  3780. <p>foo<em>bar</em></p>
  3781. .
  3782. .
  3783. 5*6*78
  3784. .
  3785. <p>5<em>6</em>78</p>
  3786. .
  3787. Rule 2:
  3788. .
  3789. _foo bar_
  3790. .
  3791. <p><em>foo bar</em></p>
  3792. .
  3793. This is not emphasis, because the opening `_` is followed by
  3794. whitespace:
  3795. .
  3796. _ foo bar_
  3797. .
  3798. <p>_ foo bar_</p>
  3799. .
  3800. This is not emphasis, because the opening `_` is preceded
  3801. by an alphanumeric and followed by punctuation:
  3802. .
  3803. a_"foo"_
  3804. .
  3805. <p>a_&quot;foo&quot;_</p>
  3806. .
  3807. Emphasis with `_` is not allowed inside ASCII words:
  3808. .
  3809. foo_bar_
  3810. .
  3811. <p>foo_bar_</p>
  3812. .
  3813. .
  3814. 5_6_78
  3815. .
  3816. <p>5_6_78</p>
  3817. .
  3818. But it is permitted inside non-ASCII words:
  3819. .
  3820. пристаням_стремятся_
  3821. .
  3822. <p>пристаням<em>стремятся</em></p>
  3823. .
  3824. Rule 3:
  3825. This is not emphasis, because the closing delimiter does
  3826. not match the opening delimiter:
  3827. .
  3828. _foo*
  3829. .
  3830. <p>_foo*</p>
  3831. .
  3832. This is not emphasis, because the closing `*` is preceded by
  3833. whitespace:
  3834. .
  3835. *foo bar *
  3836. .
  3837. <p>*foo bar *</p>
  3838. .
  3839. This is not emphasis, because the second `*` is
  3840. preceded by punctuation and followed by an alphanumeric
  3841. (hence it is not part of a [right-flanking delimiter run]:
  3842. .
  3843. *(*foo)
  3844. .
  3845. <p>*(*foo)</p>
  3846. .
  3847. The point of this restriction is more easily appreciated
  3848. with this example:
  3849. .
  3850. *(*foo*)*
  3851. .
  3852. <p><em>(<em>foo</em>)</em></p>
  3853. .
  3854. Intraword emphasis with `*` is allowed:
  3855. .
  3856. *foo*bar
  3857. .
  3858. <p><em>foo</em>bar</p>
  3859. .
  3860. Rule 4:
  3861. This is not emphasis, because the closing `_` is preceded by
  3862. whitespace:
  3863. .
  3864. _foo bar _
  3865. .
  3866. <p>_foo bar _</p>
  3867. .
  3868. This is not emphasis, because the second `_` is
  3869. preceded by punctuation and followed by an alphanumeric:
  3870. .
  3871. _(_foo)
  3872. .
  3873. <p>_(_foo)</p>
  3874. .
  3875. This is emphasis within emphasis:
  3876. .
  3877. _(_foo_)_
  3878. .
  3879. <p><em>(<em>foo</em>)</em></p>
  3880. .
  3881. Intraword emphasis is disallowed for `_`:
  3882. .
  3883. _foo_bar
  3884. .
  3885. <p>_foo_bar</p>
  3886. .
  3887. .
  3888. _пристаням_стремятся
  3889. .
  3890. <p><em>пристаням</em>стремятся</p>
  3891. .
  3892. .
  3893. _foo_bar_baz_
  3894. .
  3895. <p><em>foo_bar_baz</em></p>
  3896. .
  3897. Rule 5:
  3898. .
  3899. **foo bar**
  3900. .
  3901. <p><strong>foo bar</strong></p>
  3902. .
  3903. This is not strong emphasis, because the opening delimiter is
  3904. followed by whitespace:
  3905. .
  3906. ** foo bar**
  3907. .
  3908. <p>** foo bar**</p>
  3909. .
  3910. This is not strong emphasis, because the opening `**` is preceded
  3911. by an alphanumeric and followed by punctuation, and hence
  3912. not part of a [left-flanking delimiter run]:
  3913. .
  3914. a**"foo"**
  3915. .
  3916. <p>a**&quot;foo&quot;**</p>
  3917. .
  3918. Intraword strong emphasis with `**` is permitted:
  3919. .
  3920. foo**bar**
  3921. .
  3922. <p>foo<strong>bar</strong></p>
  3923. .
  3924. Rule 6:
  3925. .
  3926. __foo bar__
  3927. .
  3928. <p><strong>foo bar</strong></p>
  3929. .
  3930. This is not strong emphasis, because the opening delimiter is
  3931. followed by whitespace:
  3932. .
  3933. __ foo bar__
  3934. .
  3935. <p>__ foo bar__</p>
  3936. .
  3937. This is not strong emphasis, because the opening `__` is preceded
  3938. by an alphanumeric and followed by punctuation:
  3939. .
  3940. a__"foo"__
  3941. .
  3942. <p>a__&quot;foo&quot;__</p>
  3943. .
  3944. Intraword strong emphasis is forbidden with `__`:
  3945. .
  3946. foo__bar__
  3947. .
  3948. <p>foo__bar__</p>
  3949. .
  3950. .
  3951. 5__6__78
  3952. .
  3953. <p>5__6__78</p>
  3954. .
  3955. .
  3956. пристаням__стремятся__
  3957. .
  3958. <p>пристаням<strong>стремятся</strong></p>
  3959. .
  3960. .
  3961. __foo, __bar__, baz__
  3962. .
  3963. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  3964. .
  3965. Rule 7:
  3966. This is not strong emphasis, because the closing delimiter is preceded
  3967. by whitespace:
  3968. .
  3969. **foo bar **
  3970. .
  3971. <p>**foo bar **</p>
  3972. .
  3973. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  3974. Rule 11.)
  3975. This is not strong emphasis, because the second `**` is
  3976. preceded by punctuation and followed by an alphanumeric:
  3977. .
  3978. **(**foo)
  3979. .
  3980. <p>**(**foo)</p>
  3981. .
  3982. The point of this restriction is more easily appreciated
  3983. with these examples:
  3984. .
  3985. *(**foo**)*
  3986. .
  3987. <p><em>(<strong>foo</strong>)</em></p>
  3988. .
  3989. .
  3990. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  3991. *Asclepias physocarpa*)**
  3992. .
  3993. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  3994. <em>Asclepias physocarpa</em>)</strong></p>
  3995. .
  3996. .
  3997. **foo "*bar*" foo**
  3998. .
  3999. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4000. .
  4001. Intraword emphasis:
  4002. .
  4003. **foo**bar
  4004. .
  4005. <p><strong>foo</strong>bar</p>
  4006. .
  4007. Rule 8:
  4008. This is not strong emphasis, because the closing delimiter is
  4009. preceded by whitespace:
  4010. .
  4011. __foo bar __
  4012. .
  4013. <p>__foo bar __</p>
  4014. .
  4015. This is not strong emphasis, because the second `__` is
  4016. preceded by punctuation and followed by an alphanumeric:
  4017. .
  4018. __(__foo)
  4019. .
  4020. <p>__(__foo)</p>
  4021. .
  4022. The point of this restriction is more easily appreciated
  4023. with this example:
  4024. .
  4025. _(__foo__)_
  4026. .
  4027. <p><em>(<strong>foo</strong>)</em></p>
  4028. .
  4029. Intraword strong emphasis is forbidden with `__`:
  4030. .
  4031. __foo__bar
  4032. .
  4033. <p>__foo__bar</p>
  4034. .
  4035. .
  4036. __пристаням__стремятся
  4037. .
  4038. <p><strong>пристаням</strong>стремятся</p>
  4039. .
  4040. .
  4041. __foo__bar__baz__
  4042. .
  4043. <p><strong>foo__bar__baz</strong></p>
  4044. .
  4045. Rule 9:
  4046. Any nonempty sequence of inline elements can be the contents of an
  4047. emphasized span.
  4048. .
  4049. *foo [bar](/url)*
  4050. .
  4051. <p><em>foo <a href="/url">bar</a></em></p>
  4052. .
  4053. .
  4054. *foo
  4055. bar*
  4056. .
  4057. <p><em>foo
  4058. bar</em></p>
  4059. .
  4060. In particular, emphasis and strong emphasis can be nested
  4061. inside emphasis:
  4062. .
  4063. _foo __bar__ baz_
  4064. .
  4065. <p><em>foo <strong>bar</strong> baz</em></p>
  4066. .
  4067. .
  4068. _foo _bar_ baz_
  4069. .
  4070. <p><em>foo <em>bar</em> baz</em></p>
  4071. .
  4072. .
  4073. __foo_ bar_
  4074. .
  4075. <p><em><em>foo</em> bar</em></p>
  4076. .
  4077. .
  4078. *foo *bar**
  4079. .
  4080. <p><em>foo <em>bar</em></em></p>
  4081. .
  4082. .
  4083. *foo **bar** baz*
  4084. .
  4085. <p><em>foo <strong>bar</strong> baz</em></p>
  4086. .
  4087. But note:
  4088. .
  4089. *foo**bar**baz*
  4090. .
  4091. <p><em>foo</em><em>bar</em><em>baz</em></p>
  4092. .
  4093. The difference is that in the preceding case, the internal delimiters
  4094. [can close emphasis], while in the cases with spaces, they cannot.
  4095. .
  4096. ***foo** bar*
  4097. .
  4098. <p><em><strong>foo</strong> bar</em></p>
  4099. .
  4100. .
  4101. *foo **bar***
  4102. .
  4103. <p><em>foo <strong>bar</strong></em></p>
  4104. .
  4105. Note, however, that in the following case we get no strong
  4106. emphasis, because the opening delimiter is closed by the first
  4107. `*` before `bar`:
  4108. .
  4109. *foo**bar***
  4110. .
  4111. <p><em>foo</em><em>bar</em>**</p>
  4112. .
  4113. Indefinite levels of nesting are possible:
  4114. .
  4115. *foo **bar *baz* bim** bop*
  4116. .
  4117. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  4118. .
  4119. .
  4120. *foo [*bar*](/url)*
  4121. .
  4122. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  4123. .
  4124. There can be no empty emphasis or strong emphasis:
  4125. .
  4126. ** is not an empty emphasis
  4127. .
  4128. <p>** is not an empty emphasis</p>
  4129. .
  4130. .
  4131. **** is not an empty strong emphasis
  4132. .
  4133. <p>**** is not an empty strong emphasis</p>
  4134. .
  4135. Rule 10:
  4136. Any nonempty sequence of inline elements can be the contents of an
  4137. strongly emphasized span.
  4138. .
  4139. **foo [bar](/url)**
  4140. .
  4141. <p><strong>foo <a href="/url">bar</a></strong></p>
  4142. .
  4143. .
  4144. **foo
  4145. bar**
  4146. .
  4147. <p><strong>foo
  4148. bar</strong></p>
  4149. .
  4150. In particular, emphasis and strong emphasis can be nested
  4151. inside strong emphasis:
  4152. .
  4153. __foo _bar_ baz__
  4154. .
  4155. <p><strong>foo <em>bar</em> baz</strong></p>
  4156. .
  4157. .
  4158. __foo __bar__ baz__
  4159. .
  4160. <p><strong>foo <strong>bar</strong> baz</strong></p>
  4161. .
  4162. .
  4163. ____foo__ bar__
  4164. .
  4165. <p><strong><strong>foo</strong> bar</strong></p>
  4166. .
  4167. .
  4168. **foo **bar****
  4169. .
  4170. <p><strong>foo <strong>bar</strong></strong></p>
  4171. .
  4172. .
  4173. **foo *bar* baz**
  4174. .
  4175. <p><strong>foo <em>bar</em> baz</strong></p>
  4176. .
  4177. But note:
  4178. .
  4179. **foo*bar*baz**
  4180. .
  4181. <p><em><em>foo</em>bar</em>baz**</p>
  4182. .
  4183. The difference is that in the preceding case, the internal delimiters
  4184. [can close emphasis], while in the cases with spaces, they cannot.
  4185. .
  4186. ***foo* bar**
  4187. .
  4188. <p><strong><em>foo</em> bar</strong></p>
  4189. .
  4190. .
  4191. **foo *bar***
  4192. .
  4193. <p><strong>foo <em>bar</em></strong></p>
  4194. .
  4195. Indefinite levels of nesting are possible:
  4196. .
  4197. **foo *bar **baz**
  4198. bim* bop**
  4199. .
  4200. <p><strong>foo <em>bar <strong>baz</strong>
  4201. bim</em> bop</strong></p>
  4202. .
  4203. .
  4204. **foo [*bar*](/url)**
  4205. .
  4206. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  4207. .
  4208. There can be no empty emphasis or strong emphasis:
  4209. .
  4210. __ is not an empty emphasis
  4211. .
  4212. <p>__ is not an empty emphasis</p>
  4213. .
  4214. .
  4215. ____ is not an empty strong emphasis
  4216. .
  4217. <p>____ is not an empty strong emphasis</p>
  4218. .
  4219. Rule 11:
  4220. .
  4221. foo ***
  4222. .
  4223. <p>foo ***</p>
  4224. .
  4225. .
  4226. foo *\**
  4227. .
  4228. <p>foo <em>*</em></p>
  4229. .
  4230. .
  4231. foo *_*
  4232. .
  4233. <p>foo <em>_</em></p>
  4234. .
  4235. .
  4236. foo *****
  4237. .
  4238. <p>foo *****</p>
  4239. .
  4240. .
  4241. foo **\***
  4242. .
  4243. <p>foo <strong>*</strong></p>
  4244. .
  4245. .
  4246. foo **_**
  4247. .
  4248. <p>foo <strong>_</strong></p>
  4249. .
  4250. Note that when delimiters do not match evenly, Rule 11 determines
  4251. that the excess literal `*` characters will appear outside of the
  4252. emphasis, rather than inside it:
  4253. .
  4254. **foo*
  4255. .
  4256. <p>*<em>foo</em></p>
  4257. .
  4258. .
  4259. *foo**
  4260. .
  4261. <p><em>foo</em>*</p>
  4262. .
  4263. .
  4264. ***foo**
  4265. .
  4266. <p>*<strong>foo</strong></p>
  4267. .
  4268. .
  4269. ****foo*
  4270. .
  4271. <p>***<em>foo</em></p>
  4272. .
  4273. .
  4274. **foo***
  4275. .
  4276. <p><strong>foo</strong>*</p>
  4277. .
  4278. .
  4279. *foo****
  4280. .
  4281. <p><em>foo</em>***</p>
  4282. .
  4283. Rule 12:
  4284. .
  4285. foo ___
  4286. .
  4287. <p>foo ___</p>
  4288. .
  4289. .
  4290. foo _\__
  4291. .
  4292. <p>foo <em>_</em></p>
  4293. .
  4294. .
  4295. foo _*_
  4296. .
  4297. <p>foo <em>*</em></p>
  4298. .
  4299. .
  4300. foo _____
  4301. .
  4302. <p>foo _____</p>
  4303. .
  4304. .
  4305. foo __\___
  4306. .
  4307. <p>foo <strong>_</strong></p>
  4308. .
  4309. .
  4310. foo __*__
  4311. .
  4312. <p>foo <strong>*</strong></p>
  4313. .
  4314. .
  4315. __foo_
  4316. .
  4317. <p>_<em>foo</em></p>
  4318. .
  4319. Note that when delimiters do not match evenly, Rule 12 determines
  4320. that the excess literal `_` characters will appear outside of the
  4321. emphasis, rather than inside it:
  4322. .
  4323. _foo__
  4324. .
  4325. <p><em>foo</em>_</p>
  4326. .
  4327. .
  4328. ___foo__
  4329. .
  4330. <p>_<strong>foo</strong></p>
  4331. .
  4332. .
  4333. ____foo_
  4334. .
  4335. <p>___<em>foo</em></p>
  4336. .
  4337. .
  4338. __foo___
  4339. .
  4340. <p><strong>foo</strong>_</p>
  4341. .
  4342. .
  4343. _foo____
  4344. .
  4345. <p><em>foo</em>___</p>
  4346. .
  4347. Rule 13 implies that if you want emphasis nested directly inside
  4348. emphasis, you must use different delimiters:
  4349. .
  4350. **foo**
  4351. .
  4352. <p><strong>foo</strong></p>
  4353. .
  4354. .
  4355. *_foo_*
  4356. .
  4357. <p><em><em>foo</em></em></p>
  4358. .
  4359. .
  4360. __foo__
  4361. .
  4362. <p><strong>foo</strong></p>
  4363. .
  4364. .
  4365. _*foo*_
  4366. .
  4367. <p><em><em>foo</em></em></p>
  4368. .
  4369. However, strong emphasis within strong emphasisis possible without
  4370. switching delimiters:
  4371. .
  4372. ****foo****
  4373. .
  4374. <p><strong><strong>foo</strong></strong></p>
  4375. .
  4376. .
  4377. ____foo____
  4378. .
  4379. <p><strong><strong>foo</strong></strong></p>
  4380. .
  4381. Rule 13 can be applied to arbitrarily long sequences of
  4382. delimiters:
  4383. .
  4384. ******foo******
  4385. .
  4386. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4387. .
  4388. Rule 14:
  4389. .
  4390. ***foo***
  4391. .
  4392. <p><strong><em>foo</em></strong></p>
  4393. .
  4394. .
  4395. _____foo_____
  4396. .
  4397. <p><strong><strong><em>foo</em></strong></strong></p>
  4398. .
  4399. Rule 15:
  4400. .
  4401. *foo _bar* baz_
  4402. .
  4403. <p><em>foo _bar</em> baz_</p>
  4404. .
  4405. .
  4406. **foo*bar**
  4407. .
  4408. <p><em><em>foo</em>bar</em>*</p>
  4409. .
  4410. Rule 16:
  4411. .
  4412. **foo **bar baz**
  4413. .
  4414. <p>**foo <strong>bar baz</strong></p>
  4415. .
  4416. .
  4417. *foo *bar baz*
  4418. .
  4419. <p>*foo <em>bar baz</em></p>
  4420. .
  4421. Rule 17:
  4422. .
  4423. *[bar*](/url)
  4424. .
  4425. <p>*<a href="/url">bar*</a></p>
  4426. .
  4427. .
  4428. _foo [bar_](/url)
  4429. .
  4430. <p>_foo <a href="/url">bar_</a></p>
  4431. .
  4432. .
  4433. *<img src="foo" title="*"/>
  4434. .
  4435. <p>*<img src="foo" title="*"/></p>
  4436. .
  4437. .
  4438. **<a href="**">
  4439. .
  4440. <p>**<a href="**"></p>
  4441. .
  4442. .
  4443. __<a href="__">
  4444. .
  4445. <p>__<a href="__"></p>
  4446. .
  4447. .
  4448. *a `*`*
  4449. .
  4450. <p><em>a <code>*</code></em></p>
  4451. .
  4452. .
  4453. _a `_`_
  4454. .
  4455. <p><em>a <code>_</code></em></p>
  4456. .
  4457. .
  4458. **a<http://foo.bar?q=**>
  4459. .
  4460. <p>**a<a href="http://foo.bar?q=**">http://foo.bar?q=**</a></p>
  4461. .
  4462. .
  4463. __a<http://foo.bar?q=__>
  4464. .
  4465. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  4466. .
  4467. ## Links
  4468. A link contains [link text] (the visible text), a [link destination]
  4469. (the URI that is the link destination), and optionally a [link title].
  4470. There are two basic kinds of links in Markdown. In [inline link]s the
  4471. destination and title are given immediately after the link text. In
  4472. [reference link]s the destination and title are defined elsewhere in
  4473. the document.
  4474. A [link text](@link-text) consists of a sequence of zero or more
  4475. inline elements enclosed by square brackets (`[` and `]`). The
  4476. following rules apply:
  4477. - Links may not contain other links, at any level of nesting.
  4478. - Brackets are allowed in the [link text] only if (a) they
  4479. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4480. with an open bracket `[`, a sequence of zero or more inlines, and
  4481. a close bracket `]`.
  4482. - Backtick [code span]s, [autolink]s, and raw [HTML tag]s bind more tightly
  4483. than the brackets in link text. Thus, for example,
  4484. `` [foo`]` `` could not be a link text, since the second `]`
  4485. is part of a code span.
  4486. - The brackets in link text bind more tightly than markers for
  4487. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  4488. A [link destination](@link-destination) consists of either
  4489. - a sequence of zero or more characters between an opening `<` and a
  4490. closing `>` that contains no line breaks or unescaped `<` or `>`
  4491. characters, or
  4492. - a nonempty sequence of characters that does not include
  4493. ASCII space or control characters, and includes parentheses
  4494. only if (a) they are backslash-escaped or (b) they are part of
  4495. a balanced pair of unescaped parentheses that is not itself
  4496. inside a balanced pair of unescaped paretheses.
  4497. A [link title](@link-title) consists of either
  4498. - a sequence of zero or more characters between straight double-quote
  4499. characters (`"`), including a `"` character only if it is
  4500. backslash-escaped, or
  4501. - a sequence of zero or more characters between straight single-quote
  4502. characters (`'`), including a `'` character only if it is
  4503. backslash-escaped, or
  4504. - a sequence of zero or more characters between matching parentheses
  4505. (`(...)`), including a `)` character only if it is backslash-escaped.
  4506. An [inline link](@inline-link) consists of a [link text] followed immediately
  4507. by a left parenthesis `(`, optional [whitespace], an optional
  4508. [link destination], an optional [link title] separated from the link
  4509. destination by [whitespace], optional [whitespace], and a right
  4510. parenthesis `)`. The link's text consists of the inlines contained
  4511. in the [link text] (excluding the enclosing square brackets).
  4512. The link's URI consists of the link destination, excluding enclosing
  4513. `<...>` if present, with backslash-escapes in effect as described
  4514. above. The link's title consists of the link title, excluding its
  4515. enclosing delimiters, with backslash-escapes in effect as described
  4516. above.
  4517. Here is a simple inline link:
  4518. .
  4519. [link](/uri "title")
  4520. .
  4521. <p><a href="/uri" title="title">link</a></p>
  4522. .
  4523. The title may be omitted:
  4524. .
  4525. [link](/uri)
  4526. .
  4527. <p><a href="/uri">link</a></p>
  4528. .
  4529. Both the title and the destination may be omitted:
  4530. .
  4531. [link]()
  4532. .
  4533. <p><a href="">link</a></p>
  4534. .
  4535. .
  4536. [link](<>)
  4537. .
  4538. <p><a href="">link</a></p>
  4539. .
  4540. If the destination contains spaces, it must be enclosed in pointy
  4541. braces:
  4542. .
  4543. [link](/my uri)
  4544. .
  4545. <p>[link](/my uri)</p>
  4546. .
  4547. .
  4548. [link](</my uri>)
  4549. .
  4550. <p><a href="/my%20uri">link</a></p>
  4551. .
  4552. The destination cannot contain line breaks, even with pointy braces:
  4553. .
  4554. [link](foo
  4555. bar)
  4556. .
  4557. <p>[link](foo
  4558. bar)</p>
  4559. .
  4560. One level of balanced parentheses is allowed without escaping:
  4561. .
  4562. [link]((foo)and(bar))
  4563. .
  4564. <p><a href="(foo)and(bar)">link</a></p>
  4565. .
  4566. However, if you have parentheses within parentheses, you need to escape
  4567. or use the `<...>` form:
  4568. .
  4569. [link](foo(and(bar)))
  4570. .
  4571. <p>[link](foo(and(bar)))</p>
  4572. .
  4573. .
  4574. [link](foo(and\(bar\)))
  4575. .
  4576. <p><a href="foo(and(bar))">link</a></p>
  4577. .
  4578. .
  4579. [link](<foo(and(bar))>)
  4580. .
  4581. <p><a href="foo(and(bar))">link</a></p>
  4582. .
  4583. Parentheses and other symbols can also be escaped, as usual
  4584. in Markdown:
  4585. .
  4586. [link](foo\)\:)
  4587. .
  4588. <p><a href="foo):">link</a></p>
  4589. .
  4590. URL-escaping should be left alone inside the destination, as all
  4591. URL-escaped characters are also valid URL characters. HTML entities in
  4592. the destination will be parsed into their UTF-8 codepoints, as usual, and
  4593. optionally URL-escaped when written as HTML.
  4594. .
  4595. [link](foo%20b&auml;)
  4596. .
  4597. <p><a href="foo%20b%C3%A4">link</a></p>
  4598. .
  4599. Note that, because titles can often be parsed as destinations,
  4600. if you try to omit the destination and keep the title, you'll
  4601. get unexpected results:
  4602. .
  4603. [link]("title")
  4604. .
  4605. <p><a href="%22title%22">link</a></p>
  4606. .
  4607. Titles may be in single quotes, double quotes, or parentheses:
  4608. .
  4609. [link](/url "title")
  4610. [link](/url 'title')
  4611. [link](/url (title))
  4612. .
  4613. <p><a href="/url" title="title">link</a>
  4614. <a href="/url" title="title">link</a>
  4615. <a href="/url" title="title">link</a></p>
  4616. .
  4617. Backslash escapes and entities may be used in titles:
  4618. .
  4619. [link](/url "title \"&quot;")
  4620. .
  4621. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4622. .
  4623. Nested balanced quotes are not allowed without escaping:
  4624. .
  4625. [link](/url "title "and" title")
  4626. .
  4627. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4628. .
  4629. But it is easy to work around this by using a different quote type:
  4630. .
  4631. [link](/url 'title "and" title')
  4632. .
  4633. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4634. .
  4635. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4636. title, and its test suite included a test demonstrating this.
  4637. But it is hard to see a good rationale for the extra complexity this
  4638. brings, since there are already many ways---backslash escaping,
  4639. entities, or using a different quote type for the enclosing title---to
  4640. write titles containing double quotes. `Markdown.pl`'s handling of
  4641. titles has a number of other strange features. For example, it allows
  4642. single-quoted titles in inline links, but not reference links. And, in
  4643. reference links but not inline links, it allows a title to begin with
  4644. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4645. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4646. a simple, rational rule that works the same way in inline links and
  4647. link reference definitions.)
  4648. [Whitespace] is allowed around the destination and title:
  4649. .
  4650. [link]( /uri
  4651. "title" )
  4652. .
  4653. <p><a href="/uri" title="title">link</a></p>
  4654. .
  4655. But it is not allowed between the link text and the
  4656. following parenthesis:
  4657. .
  4658. [link] (/uri)
  4659. .
  4660. <p>[link] (/uri)</p>
  4661. .
  4662. The link text may contain balanced brackets, but not unbalanced ones,
  4663. unless they are escaped:
  4664. .
  4665. [link [foo [bar]]](/uri)
  4666. .
  4667. <p><a href="/uri">link [foo [bar]]</a></p>
  4668. .
  4669. .
  4670. [link] bar](/uri)
  4671. .
  4672. <p>[link] bar](/uri)</p>
  4673. .
  4674. .
  4675. [link [bar](/uri)
  4676. .
  4677. <p>[link <a href="/uri">bar</a></p>
  4678. .
  4679. .
  4680. [link \[bar](/uri)
  4681. .
  4682. <p><a href="/uri">link [bar</a></p>
  4683. .
  4684. The link text may contain inline content:
  4685. .
  4686. [link *foo **bar** `#`*](/uri)
  4687. .
  4688. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4689. .
  4690. .
  4691. [![moon](moon.jpg)](/uri)
  4692. .
  4693. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4694. .
  4695. However, links may not contain other links, at any level of nesting.
  4696. .
  4697. [foo [bar](/uri)](/uri)
  4698. .
  4699. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4700. .
  4701. .
  4702. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4703. .
  4704. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4705. .
  4706. .
  4707. ![[[foo](uri1)](uri2)](uri3)
  4708. .
  4709. <p><img src="uri3" alt="[foo](uri2)" /></p>
  4710. .
  4711. These cases illustrate the precedence of link text grouping over
  4712. emphasis grouping:
  4713. .
  4714. *[foo*](/uri)
  4715. .
  4716. <p>*<a href="/uri">foo*</a></p>
  4717. .
  4718. .
  4719. [foo *bar](baz*)
  4720. .
  4721. <p><a href="baz*">foo *bar</a></p>
  4722. .
  4723. Note that brackets that *aren't* part of links do not take
  4724. precedence:
  4725. .
  4726. *foo [bar* baz]
  4727. .
  4728. <p><em>foo [bar</em> baz]</p>
  4729. .
  4730. These cases illustrate the precedence of HTML tags, code spans,
  4731. and autolinks over link grouping:
  4732. .
  4733. [foo <bar attr="](baz)">
  4734. .
  4735. <p>[foo <bar attr="](baz)"></p>
  4736. .
  4737. .
  4738. [foo`](/uri)`
  4739. .
  4740. <p>[foo<code>](/uri)</code></p>
  4741. .
  4742. .
  4743. [foo<http://example.com?search=](uri)>
  4744. .
  4745. <p>[foo<a href="http://example.com?search=%5D(uri)">http://example.com?search=](uri)</a></p>
  4746. .
  4747. There are three kinds of [reference link](@reference-link)s:
  4748. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  4749. and [shortcut](#shortcut-reference-link).
  4750. A [full reference link](@full-reference-link)
  4751. consists of a [link text], optional [whitespace], and a [link label]
  4752. that [matches] a [link reference definition] elsewhere in the document.
  4753. A [link label](@link-label) begins with a left bracket (`[`) and ends
  4754. with the first right bracket (`]`) that is not backslash-escaped.
  4755. Unescaped square bracket characters are not allowed in
  4756. [link label]s. A link label can have at most 999
  4757. characters inside the square brackets.
  4758. One label [matches](@matches)
  4759. another just in case their normalized forms are equal. To normalize a
  4760. label, perform the *unicode case fold* and collapse consecutive internal
  4761. [whitespace] to a single space. If there are multiple
  4762. matching reference link definitions, the one that comes first in the
  4763. document is used. (It is desirable in such cases to emit a warning.)
  4764. The contents of the first link label are parsed as inlines, which are
  4765. used as the link's text. The link's URI and title are provided by the
  4766. matching [link reference definition].
  4767. Here is a simple example:
  4768. .
  4769. [foo][bar]
  4770. [bar]: /url "title"
  4771. .
  4772. <p><a href="/url" title="title">foo</a></p>
  4773. .
  4774. The rules for the [link text] are the same as with
  4775. [inline link]s. Thus:
  4776. The link text may contain balanced brackets, but not unbalanced ones,
  4777. unless they are escaped:
  4778. .
  4779. [link [foo [bar]]][ref]
  4780. [ref]: /uri
  4781. .
  4782. <p><a href="/uri">link [foo [bar]]</a></p>
  4783. .
  4784. .
  4785. [link \[bar][ref]
  4786. [ref]: /uri
  4787. .
  4788. <p><a href="/uri">link [bar</a></p>
  4789. .
  4790. The link text may contain inline content:
  4791. .
  4792. [link *foo **bar** `#`*][ref]
  4793. [ref]: /uri
  4794. .
  4795. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4796. .
  4797. .
  4798. [![moon](moon.jpg)][ref]
  4799. [ref]: /uri
  4800. .
  4801. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4802. .
  4803. However, links may not contain other links, at any level of nesting.
  4804. .
  4805. [foo [bar](/uri)][ref]
  4806. [ref]: /uri
  4807. .
  4808. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  4809. .
  4810. .
  4811. [foo *bar [baz][ref]*][ref]
  4812. [ref]: /uri
  4813. .
  4814. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  4815. .
  4816. (In the examples above, we have two [shortcut reference link]s
  4817. instead of one [full reference link].)
  4818. The following cases illustrate the precedence of link text grouping over
  4819. emphasis grouping:
  4820. .
  4821. *[foo*][ref]
  4822. [ref]: /uri
  4823. .
  4824. <p>*<a href="/uri">foo*</a></p>
  4825. .
  4826. .
  4827. [foo *bar][ref]
  4828. [ref]: /uri
  4829. .
  4830. <p><a href="/uri">foo *bar</a></p>
  4831. .
  4832. These cases illustrate the precedence of HTML tags, code spans,
  4833. and autolinks over link grouping:
  4834. .
  4835. [foo <bar attr="][ref]">
  4836. [ref]: /uri
  4837. .
  4838. <p>[foo <bar attr="][ref]"></p>
  4839. .
  4840. .
  4841. [foo`][ref]`
  4842. [ref]: /uri
  4843. .
  4844. <p>[foo<code>][ref]</code></p>
  4845. .
  4846. .
  4847. [foo<http://example.com?search=][ref]>
  4848. [ref]: /uri
  4849. .
  4850. <p>[foo<a href="http://example.com?search=%5D%5Bref%5D">http://example.com?search=][ref]</a></p>
  4851. .
  4852. Matching is case-insensitive:
  4853. .
  4854. [foo][BaR]
  4855. [bar]: /url "title"
  4856. .
  4857. <p><a href="/url" title="title">foo</a></p>
  4858. .
  4859. Unicode case fold is used:
  4860. .
  4861. [Толпой][Толпой] is a Russian word.
  4862. [ТОЛПОЙ]: /url
  4863. .
  4864. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4865. .
  4866. Consecutive internal [whitespace] is treated as one space for
  4867. purposes of determining matching:
  4868. .
  4869. [Foo
  4870. bar]: /url
  4871. [Baz][Foo bar]
  4872. .
  4873. <p><a href="/url">Baz</a></p>
  4874. .
  4875. There can be [whitespace] between the [link text] and the [link label]:
  4876. .
  4877. [foo] [bar]
  4878. [bar]: /url "title"
  4879. .
  4880. <p><a href="/url" title="title">foo</a></p>
  4881. .
  4882. .
  4883. [foo]
  4884. [bar]
  4885. [bar]: /url "title"
  4886. .
  4887. <p><a href="/url" title="title">foo</a></p>
  4888. .
  4889. When there are multiple matching [link reference definition]s,
  4890. the first is used:
  4891. .
  4892. [foo]: /url1
  4893. [foo]: /url2
  4894. [bar][foo]
  4895. .
  4896. <p><a href="/url1">bar</a></p>
  4897. .
  4898. Note that matching is performed on normalized strings, not parsed
  4899. inline content. So the following does not match, even though the
  4900. labels define equivalent inline content:
  4901. .
  4902. [bar][foo\!]
  4903. [foo!]: /url
  4904. .
  4905. <p>[bar][foo!]</p>
  4906. .
  4907. [Link label]s cannot contain brackets, unless they are
  4908. backslash-escaped:
  4909. .
  4910. [foo][ref[]
  4911. [ref[]: /uri
  4912. .
  4913. <p>[foo][ref[]</p>
  4914. <p>[ref[]: /uri</p>
  4915. .
  4916. .
  4917. [foo][ref[bar]]
  4918. [ref[bar]]: /uri
  4919. .
  4920. <p>[foo][ref[bar]]</p>
  4921. <p>[ref[bar]]: /uri</p>
  4922. .
  4923. .
  4924. [[[foo]]]
  4925. [[[foo]]]: /url
  4926. .
  4927. <p>[[[foo]]]</p>
  4928. <p>[[[foo]]]: /url</p>
  4929. .
  4930. .
  4931. [foo][ref\[]
  4932. [ref\[]: /uri
  4933. .
  4934. <p><a href="/uri">foo</a></p>
  4935. .
  4936. A [collapsed reference link](@collapsed-reference-link)
  4937. consists of a [link label] that [matches] a
  4938. [link reference definition] elsewhere in the
  4939. document, optional [whitespace], and the string `[]`.
  4940. The contents of the first link label are parsed as inlines,
  4941. which are used as the link's text. The link's URI and title are
  4942. provided by the matching reference link definition. Thus,
  4943. `[foo][]` is equivalent to `[foo][foo]`.
  4944. .
  4945. [foo][]
  4946. [foo]: /url "title"
  4947. .
  4948. <p><a href="/url" title="title">foo</a></p>
  4949. .
  4950. .
  4951. [*foo* bar][]
  4952. [*foo* bar]: /url "title"
  4953. .
  4954. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4955. .
  4956. The link labels are case-insensitive:
  4957. .
  4958. [Foo][]
  4959. [foo]: /url "title"
  4960. .
  4961. <p><a href="/url" title="title">Foo</a></p>
  4962. .
  4963. As with full reference links, [whitespace] is allowed
  4964. between the two sets of brackets:
  4965. .
  4966. [foo]
  4967. []
  4968. [foo]: /url "title"
  4969. .
  4970. <p><a href="/url" title="title">foo</a></p>
  4971. .
  4972. A [shortcut reference link](@shortcut-reference-link)
  4973. consists of a [link label] that [matches] a
  4974. [link reference definition] elsewhere in the
  4975. document and is not followed by `[]` or a link label.
  4976. The contents of the first link label are parsed as inlines,
  4977. which are used as the link's text. the link's URI and title
  4978. are provided by the matching link reference definition.
  4979. Thus, `[foo]` is equivalent to `[foo][]`.
  4980. .
  4981. [foo]
  4982. [foo]: /url "title"
  4983. .
  4984. <p><a href="/url" title="title">foo</a></p>
  4985. .
  4986. .
  4987. [*foo* bar]
  4988. [*foo* bar]: /url "title"
  4989. .
  4990. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4991. .
  4992. .
  4993. [[*foo* bar]]
  4994. [*foo* bar]: /url "title"
  4995. .
  4996. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4997. .
  4998. The link labels are case-insensitive:
  4999. .
  5000. [Foo]
  5001. [foo]: /url "title"
  5002. .
  5003. <p><a href="/url" title="title">Foo</a></p>
  5004. .
  5005. A space after the link text should be preserved:
  5006. .
  5007. [foo] bar
  5008. [foo]: /url
  5009. .
  5010. <p><a href="/url">foo</a> bar</p>
  5011. .
  5012. If you just want bracketed text, you can backslash-escape the
  5013. opening bracket to avoid links:
  5014. .
  5015. \[foo]
  5016. [foo]: /url "title"
  5017. .
  5018. <p>[foo]</p>
  5019. .
  5020. Note that this is a link, because a link label ends with the first
  5021. following closing bracket:
  5022. .
  5023. [foo*]: /url
  5024. *[foo*]
  5025. .
  5026. <p>*<a href="/url">foo*</a></p>
  5027. .
  5028. This is a link too, for the same reason:
  5029. .
  5030. [foo`]: /url
  5031. [foo`]`
  5032. .
  5033. <p>[foo<code>]</code></p>
  5034. .
  5035. Full references take precedence over shortcut references:
  5036. .
  5037. [foo][bar]
  5038. [foo]: /url1
  5039. [bar]: /url2
  5040. .
  5041. <p><a href="/url2">foo</a></p>
  5042. .
  5043. In the following case `[bar][baz]` is parsed as a reference,
  5044. `[foo]` as normal text:
  5045. .
  5046. [foo][bar][baz]
  5047. [baz]: /url
  5048. .
  5049. <p>[foo]<a href="/url">bar</a></p>
  5050. .
  5051. Here, though, `[foo][bar]` is parsed as a reference, since
  5052. `[bar]` is defined:
  5053. .
  5054. [foo][bar][baz]
  5055. [baz]: /url1
  5056. [bar]: /url2
  5057. .
  5058. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  5059. .
  5060. Here `[foo]` is not parsed as a shortcut reference, because it
  5061. is followed by a link label (even though `[bar]` is not defined):
  5062. .
  5063. [foo][bar][baz]
  5064. [baz]: /url1
  5065. [foo]: /url2
  5066. .
  5067. <p>[foo]<a href="/url1">bar</a></p>
  5068. .
  5069. ## Images
  5070. Syntax for images is like the syntax for links, with one
  5071. difference. Instead of [link text], we have an
  5072. [image description](@image-description). The rules for this are the
  5073. same as for [link text], except that (a) an
  5074. image description starts with `![` rather than `[`, and
  5075. (b) an image description may contain links.
  5076. An image description has inline elements
  5077. as its contents. When an image is rendered to HTML,
  5078. this is standardly used as the image's `alt` attribute.
  5079. .
  5080. ![foo](/url "title")
  5081. .
  5082. <p><img src="/url" alt="foo" title="title" /></p>
  5083. .
  5084. .
  5085. ![foo *bar*]
  5086. [foo *bar*]: train.jpg "train & tracks"
  5087. .
  5088. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5089. .
  5090. .
  5091. ![foo ![bar](/url)](/url2)
  5092. .
  5093. <p><img src="/url2" alt="foo bar" /></p>
  5094. .
  5095. .
  5096. ![foo [bar](/url)](/url2)
  5097. .
  5098. <p><img src="/url2" alt="foo bar" /></p>
  5099. .
  5100. Though this spec is concerned with parsing, not rendering, it is
  5101. recommended that in rendering to HTML, only the plain string content
  5102. of the [image description] be used. Note that in
  5103. the above example, the alt attribute's value is `foo bar`, not `foo
  5104. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  5105. content is rendered, without formatting.
  5106. .
  5107. ![foo *bar*][]
  5108. [foo *bar*]: train.jpg "train & tracks"
  5109. .
  5110. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5111. .
  5112. .
  5113. ![foo *bar*][foobar]
  5114. [FOOBAR]: train.jpg "train & tracks"
  5115. .
  5116. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5117. .
  5118. .
  5119. ![foo](train.jpg)
  5120. .
  5121. <p><img src="train.jpg" alt="foo" /></p>
  5122. .
  5123. .
  5124. My ![foo bar](/path/to/train.jpg "title" )
  5125. .
  5126. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  5127. .
  5128. .
  5129. ![foo](<url>)
  5130. .
  5131. <p><img src="url" alt="foo" /></p>
  5132. .
  5133. .
  5134. ![](/url)
  5135. .
  5136. <p><img src="/url" alt="" /></p>
  5137. .
  5138. Reference-style:
  5139. .
  5140. ![foo] [bar]
  5141. [bar]: /url
  5142. .
  5143. <p><img src="/url" alt="foo" /></p>
  5144. .
  5145. .
  5146. ![foo] [bar]
  5147. [BAR]: /url
  5148. .
  5149. <p><img src="/url" alt="foo" /></p>
  5150. .
  5151. Collapsed:
  5152. .
  5153. ![foo][]
  5154. [foo]: /url "title"
  5155. .
  5156. <p><img src="/url" alt="foo" title="title" /></p>
  5157. .
  5158. .
  5159. ![*foo* bar][]
  5160. [*foo* bar]: /url "title"
  5161. .
  5162. <p><img src="/url" alt="foo bar" title="title" /></p>
  5163. .
  5164. The labels are case-insensitive:
  5165. .
  5166. ![Foo][]
  5167. [foo]: /url "title"
  5168. .
  5169. <p><img src="/url" alt="Foo" title="title" /></p>
  5170. .
  5171. As with full reference links, [whitespace] is allowed
  5172. between the two sets of brackets:
  5173. .
  5174. ![foo]
  5175. []
  5176. [foo]: /url "title"
  5177. .
  5178. <p><img src="/url" alt="foo" title="title" /></p>
  5179. .
  5180. Shortcut:
  5181. .
  5182. ![foo]
  5183. [foo]: /url "title"
  5184. .
  5185. <p><img src="/url" alt="foo" title="title" /></p>
  5186. .
  5187. .
  5188. ![*foo* bar]
  5189. [*foo* bar]: /url "title"
  5190. .
  5191. <p><img src="/url" alt="foo bar" title="title" /></p>
  5192. .
  5193. Note that link labels cannot contain unescaped brackets:
  5194. .
  5195. ![[foo]]
  5196. [[foo]]: /url "title"
  5197. .
  5198. <p>![[foo]]</p>
  5199. <p>[[foo]]: /url &quot;title&quot;</p>
  5200. .
  5201. The link labels are case-insensitive:
  5202. .
  5203. ![Foo]
  5204. [foo]: /url "title"
  5205. .
  5206. <p><img src="/url" alt="Foo" title="title" /></p>
  5207. .
  5208. If you just want bracketed text, you can backslash-escape the
  5209. opening `!` and `[`:
  5210. .
  5211. \!\[foo]
  5212. [foo]: /url "title"
  5213. .
  5214. <p>![foo]</p>
  5215. .
  5216. If you want a link after a literal `!`, backslash-escape the
  5217. `!`:
  5218. .
  5219. \![foo]
  5220. [foo]: /url "title"
  5221. .
  5222. <p>!<a href="/url" title="title">foo</a></p>
  5223. .
  5224. ## Autolinks
  5225. [Autolink](@autolink)s are absolute URIs and email addresses inside
  5226. `<` and `>`. They are parsed as links, with the URL or email address
  5227. as the link label.
  5228. A [URI autolink](@uri-autolink) consists of `<`, followed by an
  5229. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  5230. a link to the URI, with the URI as the link's label.
  5231. An [absolute URI](@absolute-uri),
  5232. for these purposes, consists of a [scheme] followed by a colon (`:`)
  5233. followed by zero or more characters other than ASCII
  5234. [whitespace] and control characters, `<`, and `>`. If
  5235. the URI includes these characters, you must use percent-encoding
  5236. (e.g. `%20` for a space).
  5237. The following [schemes](@scheme)
  5238. are recognized (case-insensitive):
  5239. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5240. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5241. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5242. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5243. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5244. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5245. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5246. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5247. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5248. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5249. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5250. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5251. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5252. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5253. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5254. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5255. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5256. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5257. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5258. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5259. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5260. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5261. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5262. Here are some valid autolinks:
  5263. .
  5264. <http://foo.bar.baz>
  5265. .
  5266. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5267. .
  5268. .
  5269. <http://foo.bar.baz?q=hello&id=22&boolean>
  5270. .
  5271. <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>
  5272. .
  5273. .
  5274. <irc://foo.bar:2233/baz>
  5275. .
  5276. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5277. .
  5278. Uppercase is also fine:
  5279. .
  5280. <MAILTO:FOO@BAR.BAZ>
  5281. .
  5282. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5283. .
  5284. Spaces are not allowed in autolinks:
  5285. .
  5286. <http://foo.bar/baz bim>
  5287. .
  5288. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5289. .
  5290. An [email autolink](@email-autolink)
  5291. consists of `<`, followed by an [email address],
  5292. followed by `>`. The link's label is the email address,
  5293. and the URL is `mailto:` followed by the email address.
  5294. An [email address](@email-address),
  5295. for these purposes, is anything that matches
  5296. the [non-normative regex from the HTML5
  5297. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  5298. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5299. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5300. Examples of email autolinks:
  5301. .
  5302. <foo@bar.example.com>
  5303. .
  5304. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5305. .
  5306. .
  5307. <foo+special@Bar.baz-bar0.com>
  5308. .
  5309. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5310. .
  5311. These are not autolinks:
  5312. .
  5313. <>
  5314. .
  5315. <p>&lt;&gt;</p>
  5316. .
  5317. .
  5318. <heck://bing.bong>
  5319. .
  5320. <p>&lt;heck://bing.bong&gt;</p>
  5321. .
  5322. .
  5323. < http://foo.bar >
  5324. .
  5325. <p>&lt; http://foo.bar &gt;</p>
  5326. .
  5327. .
  5328. <foo.bar.baz>
  5329. .
  5330. <p>&lt;foo.bar.baz&gt;</p>
  5331. .
  5332. .
  5333. <localhost:5001/foo>
  5334. .
  5335. <p>&lt;localhost:5001/foo&gt;</p>
  5336. .
  5337. .
  5338. http://example.com
  5339. .
  5340. <p>http://example.com</p>
  5341. .
  5342. .
  5343. foo@bar.example.com
  5344. .
  5345. <p>foo@bar.example.com</p>
  5346. .
  5347. ## Raw HTML
  5348. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5349. raw HTML tag and will be rendered in HTML without escaping.
  5350. Tag and attribute names are not limited to current HTML tags,
  5351. so custom tags (and even, say, DocBook tags) may be used.
  5352. Here is the grammar for tags:
  5353. A [tag name](@tag-name) consists of an ASCII letter
  5354. followed by zero or more ASCII letters or digits.
  5355. An [attribute](@attribute) consists of [whitespace],
  5356. an [attribute name], and an optional
  5357. [attribute value specification].
  5358. An [attribute name](@attribute-name)
  5359. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5360. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5361. specification restricted to ASCII. HTML5 is laxer.)
  5362. An [attribute value specification](@attribute-value-specification)
  5363. consists of optional [whitespace],
  5364. a `=` character, optional [whitespace], and an [attribute
  5365. value].
  5366. An [attribute value](@attribute-value)
  5367. consists of an [unquoted attribute value],
  5368. a [single-quoted attribute value], or a [double-quoted attribute value].
  5369. An [unquoted attribute value](@unquoted-attribute-value)
  5370. is a nonempty string of characters not
  5371. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5372. A [single-quoted attribute value](@single-quoted-attribute-value)
  5373. consists of `'`, zero or more
  5374. characters not including `'`, and a final `'`.
  5375. A [double-quoted attribute value](@double-quoted-attribute-value)
  5376. consists of `"`, zero or more
  5377. characters not including `"`, and a final `"`.
  5378. An [open tag](@open-tag) consists of a `<` character, a [tag name],
  5379. zero or more [attributes], optional [whitespace], an optional `/`
  5380. character, and a `>` character.
  5381. A [closing tag](@closing-tag) consists of the string `</`, a
  5382. [tag name], optional [whitespace], and the character `>`.
  5383. An [HTML comment](@html-comment) consists of `<!--` + *text* + `-->`,
  5384. where *text* does not start with `>` or `->`, does not end with `-`,
  5385. and does not contain `--`. (See the
  5386. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  5387. A [processing instruction](@processing-instruction)
  5388. consists of the string `<?`, a string
  5389. of characters not including the string `?>`, and the string
  5390. `?>`.
  5391. A [declaration](@declaration) consists of the
  5392. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5393. [whitespace], a string of characters not including the
  5394. character `>`, and the character `>`.
  5395. A [CDATA section](@cdata-section) consists of
  5396. the string `<![CDATA[`, a string of characters not including the string
  5397. `]]>`, and the string `]]>`.
  5398. An [HTML tag](@html-tag) consists of an [open tag], a [closing tag],
  5399. an [HTML comment], a [processing instruction], a [declaration],
  5400. or a [CDATA section].
  5401. Here are some simple open tags:
  5402. .
  5403. <a><bab><c2c>
  5404. .
  5405. <p><a><bab><c2c></p>
  5406. .
  5407. Empty elements:
  5408. .
  5409. <a/><b2/>
  5410. .
  5411. <p><a/><b2/></p>
  5412. .
  5413. [Whitespace] is allowed:
  5414. .
  5415. <a /><b2
  5416. data="foo" >
  5417. .
  5418. <p><a /><b2
  5419. data="foo" ></p>
  5420. .
  5421. With attributes:
  5422. .
  5423. <a foo="bar" bam = 'baz <em>"</em>'
  5424. _boolean zoop:33=zoop:33 />
  5425. .
  5426. <p><a foo="bar" bam = 'baz <em>"</em>'
  5427. _boolean zoop:33=zoop:33 /></p>
  5428. .
  5429. Illegal tag names, not parsed as HTML:
  5430. .
  5431. <33> <__>
  5432. .
  5433. <p>&lt;33&gt; &lt;__&gt;</p>
  5434. .
  5435. Illegal attribute names:
  5436. .
  5437. <a h*#ref="hi">
  5438. .
  5439. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5440. .
  5441. Illegal attribute values:
  5442. .
  5443. <a href="hi'> <a href=hi'>
  5444. .
  5445. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5446. .
  5447. Illegal [whitespace]:
  5448. .
  5449. < a><
  5450. foo><bar/ >
  5451. .
  5452. <p>&lt; a&gt;&lt;
  5453. foo&gt;&lt;bar/ &gt;</p>
  5454. .
  5455. Missing [whitespace]:
  5456. .
  5457. <a href='bar'title=title>
  5458. .
  5459. <p>&lt;a href='bar'title=title&gt;</p>
  5460. .
  5461. Closing tags:
  5462. .
  5463. </a>
  5464. </foo >
  5465. .
  5466. <p></a>
  5467. </foo ></p>
  5468. .
  5469. Illegal attributes in closing tag:
  5470. .
  5471. </a href="foo">
  5472. .
  5473. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5474. .
  5475. Comments:
  5476. .
  5477. foo <!-- this is a
  5478. comment - with hyphen -->
  5479. .
  5480. <p>foo <!-- this is a
  5481. comment - with hyphen --></p>
  5482. .
  5483. .
  5484. foo <!-- not a comment -- two hyphens -->
  5485. .
  5486. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5487. .
  5488. Not comments:
  5489. .
  5490. foo <!--> foo -->
  5491. foo <!-- foo--->
  5492. .
  5493. <p>foo &lt;!--&gt; foo --&gt;</p>
  5494. <p>foo &lt;!-- foo---&gt;</p>
  5495. .
  5496. Processing instructions:
  5497. .
  5498. foo <?php echo $a; ?>
  5499. .
  5500. <p>foo <?php echo $a; ?></p>
  5501. .
  5502. Declarations:
  5503. .
  5504. foo <!ELEMENT br EMPTY>
  5505. .
  5506. <p>foo <!ELEMENT br EMPTY></p>
  5507. .
  5508. CDATA sections:
  5509. .
  5510. foo <![CDATA[>&<]]>
  5511. .
  5512. <p>foo <![CDATA[>&<]]></p>
  5513. .
  5514. Entities are preserved in HTML attributes:
  5515. .
  5516. <a href="&ouml;">
  5517. .
  5518. <p><a href="&ouml;"></p>
  5519. .
  5520. Backslash escapes do not work in HTML attributes:
  5521. .
  5522. <a href="\*">
  5523. .
  5524. <p><a href="\*"></p>
  5525. .
  5526. .
  5527. <a href="\"">
  5528. .
  5529. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5530. .
  5531. ## Hard line breaks
  5532. A line break (not in a code span or HTML tag) that is preceded
  5533. by two or more spaces and does not occur at the end of a block
  5534. is parsed as a [hard line break](@hard-line-break) (rendered
  5535. in HTML as a `<br />` tag):
  5536. .
  5537. foo
  5538. baz
  5539. .
  5540. <p>foo<br />
  5541. baz</p>
  5542. .
  5543. For a more visible alternative, a backslash before the
  5544. [line ending] may be used instead of two spaces:
  5545. .
  5546. foo\
  5547. baz
  5548. .
  5549. <p>foo<br />
  5550. baz</p>
  5551. .
  5552. More than two spaces can be used:
  5553. .
  5554. foo
  5555. baz
  5556. .
  5557. <p>foo<br />
  5558. baz</p>
  5559. .
  5560. Leading spaces at the beginning of the next line are ignored:
  5561. .
  5562. foo
  5563. bar
  5564. .
  5565. <p>foo<br />
  5566. bar</p>
  5567. .
  5568. .
  5569. foo\
  5570. bar
  5571. .
  5572. <p>foo<br />
  5573. bar</p>
  5574. .
  5575. Line breaks can occur inside emphasis, links, and other constructs
  5576. that allow inline content:
  5577. .
  5578. *foo
  5579. bar*
  5580. .
  5581. <p><em>foo<br />
  5582. bar</em></p>
  5583. .
  5584. .
  5585. *foo\
  5586. bar*
  5587. .
  5588. <p><em>foo<br />
  5589. bar</em></p>
  5590. .
  5591. Line breaks do not occur inside code spans
  5592. .
  5593. `code
  5594. span`
  5595. .
  5596. <p><code>code span</code></p>
  5597. .
  5598. .
  5599. `code\
  5600. span`
  5601. .
  5602. <p><code>code\ span</code></p>
  5603. .
  5604. or HTML tags:
  5605. .
  5606. <a href="foo
  5607. bar">
  5608. .
  5609. <p><a href="foo
  5610. bar"></p>
  5611. .
  5612. .
  5613. <a href="foo\
  5614. bar">
  5615. .
  5616. <p><a href="foo\
  5617. bar"></p>
  5618. .
  5619. Hard line breaks are for separating inline content within a block.
  5620. Neither syntax for hard line breaks works at the end of a paragraph or
  5621. other block element:
  5622. .
  5623. foo\
  5624. .
  5625. <p>foo\</p>
  5626. .
  5627. .
  5628. foo
  5629. .
  5630. <p>foo</p>
  5631. .
  5632. .
  5633. ### foo\
  5634. .
  5635. <h3>foo\</h3>
  5636. .
  5637. .
  5638. ### foo
  5639. .
  5640. <h3>foo</h3>
  5641. .
  5642. ## Soft line breaks
  5643. A regular line break (not in a code span or HTML tag) that is not
  5644. preceded by two or more spaces is parsed as a softbreak. (A
  5645. softbreak may be rendered in HTML either as a
  5646. [line ending] or as a space. The result will be the same
  5647. in browsers. In the examples here, a [line ending] will be used.)
  5648. .
  5649. foo
  5650. baz
  5651. .
  5652. <p>foo
  5653. baz</p>
  5654. .
  5655. Spaces at the end of the line and beginning of the next line are
  5656. removed:
  5657. .
  5658. foo
  5659. baz
  5660. .
  5661. <p>foo
  5662. baz</p>
  5663. .
  5664. A conforming parser may render a soft line break in HTML either as a
  5665. line break or as a space.
  5666. A renderer may also provide an option to render soft line breaks
  5667. as hard line breaks.
  5668. ## Textual content
  5669. Any characters not given an interpretation by the above rules will
  5670. be parsed as plain textual content.
  5671. .
  5672. hello $.;'there
  5673. .
  5674. <p>hello $.;'there</p>
  5675. .
  5676. .
  5677. Foo χρῆν
  5678. .
  5679. <p>Foo χρῆν</p>
  5680. .
  5681. Internal spaces are preserved verbatim:
  5682. .
  5683. Multiple spaces
  5684. .
  5685. <p>Multiple spaces</p>
  5686. .
  5687. <!-- END TESTS -->
  5688. # Appendix A: A parsing strategy {-}
  5689. ## Overview {-}
  5690. Parsing has two phases:
  5691. 1. In the first phase, lines of input are consumed and the block
  5692. structure of the document---its division into paragraphs, block quotes,
  5693. list items, and so on---is constructed. Text is assigned to these
  5694. blocks but not parsed. Link reference definitions are parsed and a
  5695. map of links is constructed.
  5696. 2. In the second phase, the raw text contents of paragraphs and headers
  5697. are parsed into sequences of Markdown inline elements (strings,
  5698. code spans, links, emphasis, and so on), using the map of link
  5699. references constructed in phase 1.
  5700. ## The document tree {-}
  5701. At each point in processing, the document is represented as a tree of
  5702. **blocks**. The root of the tree is a `document` block. The `document`
  5703. may have any number of other blocks as **children**. These children
  5704. may, in turn, have other blocks as children. The last child of a block
  5705. is normally considered **open**, meaning that subsequent lines of input
  5706. can alter its contents. (Blocks that are not open are **closed**.)
  5707. Here, for example, is a possible document tree, with the open blocks
  5708. marked by arrows:
  5709. ``` tree
  5710. -> document
  5711. -> block_quote
  5712. paragraph
  5713. "Lorem ipsum dolor\nsit amet."
  5714. -> list (type=bullet tight=true bullet_char=-)
  5715. list_item
  5716. paragraph
  5717. "Qui *quodsi iracundia*"
  5718. -> list_item
  5719. -> paragraph
  5720. "aliquando id"
  5721. ```
  5722. ## How source lines alter the document tree {-}
  5723. Each line that is processed has an effect on this tree. The line is
  5724. analyzed and, depending on its contents, the document may be altered
  5725. in one or more of the following ways:
  5726. 1. One or more open blocks may be closed.
  5727. 2. One or more new blocks may be created as children of the
  5728. last open block.
  5729. 3. Text may be added to the last (deepest) open block remaining
  5730. on the tree.
  5731. Once a line has been incorporated into the tree in this way,
  5732. it can be discarded, so input can be read in a stream.
  5733. We can see how this works by considering how the tree above is
  5734. generated by four lines of Markdown:
  5735. ``` markdown
  5736. > Lorem ipsum dolor
  5737. sit amet.
  5738. > - Qui *quodsi iracundia*
  5739. > - aliquando id
  5740. ```
  5741. At the outset, our document model is just
  5742. ``` tree
  5743. -> document
  5744. ```
  5745. The first line of our text,
  5746. ``` markdown
  5747. > Lorem ipsum dolor
  5748. ```
  5749. causes a `block_quote` block to be created as a child of our
  5750. open `document` block, and a `paragraph` block as a child of
  5751. the `block_quote`. Then the text is added to the last open
  5752. block, the `paragraph`:
  5753. ``` tree
  5754. -> document
  5755. -> block_quote
  5756. -> paragraph
  5757. "Lorem ipsum dolor"
  5758. ```
  5759. The next line,
  5760. ``` markdown
  5761. sit amet.
  5762. ```
  5763. is a "lazy continuation" of the open `paragraph`, so it gets added
  5764. to the paragraph's text:
  5765. ``` tree
  5766. -> document
  5767. -> block_quote
  5768. -> paragraph
  5769. "Lorem ipsum dolor\nsit amet."
  5770. ```
  5771. The third line,
  5772. ``` markdown
  5773. > - Qui *quodsi iracundia*
  5774. ```
  5775. causes the `paragraph` block to be closed, and a new `list` block
  5776. opened as a child of the `block_quote`. A `list_item` is also
  5777. added as a child of the `list`, and a `paragraph` as a child of
  5778. the `list_item`. The text is then added to the new `paragraph`:
  5779. ``` tree
  5780. -> document
  5781. -> block_quote
  5782. paragraph
  5783. "Lorem ipsum dolor\nsit amet."
  5784. -> list (type=bullet tight=true bullet_char=-)
  5785. -> list_item
  5786. -> paragraph
  5787. "Qui *quodsi iracundia*"
  5788. ```
  5789. The fourth line,
  5790. ``` markdown
  5791. > - aliquando id
  5792. ```
  5793. causes the `list_item` (and its child the `paragraph`) to be closed,
  5794. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5795. is added as a child of the new `list_item`, to contain the text.
  5796. We thus obtain the final tree:
  5797. ``` tree
  5798. -> document
  5799. -> block_quote
  5800. paragraph
  5801. "Lorem ipsum dolor\nsit amet."
  5802. -> list (type=bullet tight=true bullet_char=-)
  5803. list_item
  5804. paragraph
  5805. "Qui *quodsi iracundia*"
  5806. -> list_item
  5807. -> paragraph
  5808. "aliquando id"
  5809. ```
  5810. ## From block structure to the final document {-}
  5811. Once all of the input has been parsed, all open blocks are closed.
  5812. We then "walk the tree," visiting every node, and parse raw
  5813. string contents of paragraphs and headers as inlines. At this
  5814. point we have seen all the link reference definitions, so we can
  5815. resolve reference links as we go.
  5816. ``` tree
  5817. document
  5818. block_quote
  5819. paragraph
  5820. str "Lorem ipsum dolor"
  5821. softbreak
  5822. str "sit amet."
  5823. list (type=bullet tight=true bullet_char=-)
  5824. list_item
  5825. paragraph
  5826. str "Qui "
  5827. emph
  5828. str "quodsi iracundia"
  5829. list_item
  5830. paragraph
  5831. str "aliquando id"
  5832. ```
  5833. Notice how the [line ending] in the first paragraph has
  5834. been parsed as a `softbreak`, and the asterisks in the first list item
  5835. have become an `emph`.
  5836. The document can be rendered as HTML, or in any other format, given
  5837. an appropriate renderer.