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