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