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