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