aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: c18766cd85aeb080b7d4d21ddf397cd6e3119b04 (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 readibility.
  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. .
  1767. <pre language="haskell"><code>
  1768. import Text.HTML.TagSoup
  1769. main :: IO ()
  1770. main = print $ parseTags tags
  1771. </code></pre>
  1772. ````````````````````````````````
  1773. A script tag (type 1):
  1774. ```````````````````````````````` example
  1775. <script type="text/javascript">
  1776. // JavaScript example
  1777. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1778. </script>
  1779. .
  1780. <script type="text/javascript">
  1781. // JavaScript example
  1782. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1783. </script>
  1784. ````````````````````````````````
  1785. A style tag (type 1):
  1786. ```````````````````````````````` example
  1787. <style
  1788. type="text/css">
  1789. h1 {color:red;}
  1790. p {color:blue;}
  1791. </style>
  1792. .
  1793. <style
  1794. type="text/css">
  1795. h1 {color:red;}
  1796. p {color:blue;}
  1797. </style>
  1798. ````````````````````````````````
  1799. If there is no matching end tag, the block will end at the
  1800. end of the document (or the enclosing [block quote][block quotes]
  1801. or [list item][list items]):
  1802. ```````````````````````````````` example
  1803. <style
  1804. type="text/css">
  1805. foo
  1806. .
  1807. <style
  1808. type="text/css">
  1809. foo
  1810. ````````````````````````````````
  1811. ```````````````````````````````` example
  1812. > <div>
  1813. > foo
  1814. bar
  1815. .
  1816. <blockquote>
  1817. <div>
  1818. foo
  1819. </blockquote>
  1820. <p>bar</p>
  1821. ````````````````````````````````
  1822. ```````````````````````````````` example
  1823. - <div>
  1824. - foo
  1825. .
  1826. <ul>
  1827. <li>
  1828. <div>
  1829. </li>
  1830. <li>foo</li>
  1831. </ul>
  1832. ````````````````````````````````
  1833. The end tag can occur on the same line as the start tag:
  1834. ```````````````````````````````` example
  1835. <style>p{color:red;}</style>
  1836. *foo*
  1837. .
  1838. <style>p{color:red;}</style>
  1839. <p><em>foo</em></p>
  1840. ````````````````````````````````
  1841. ```````````````````````````````` example
  1842. <!-- foo -->*bar*
  1843. *baz*
  1844. .
  1845. <!-- foo -->*bar*
  1846. <p><em>baz</em></p>
  1847. ````````````````````````````````
  1848. Note that anything on the last line after the
  1849. end tag will be included in the [HTML block]:
  1850. ```````````````````````````````` example
  1851. <script>
  1852. foo
  1853. </script>1. *bar*
  1854. .
  1855. <script>
  1856. foo
  1857. </script>1. *bar*
  1858. ````````````````````````````````
  1859. A comment (type 2):
  1860. ```````````````````````````````` example
  1861. <!-- Foo
  1862. bar
  1863. baz -->
  1864. .
  1865. <!-- Foo
  1866. bar
  1867. baz -->
  1868. ````````````````````````````````
  1869. A processing instruction (type 3):
  1870. ```````````````````````````````` example
  1871. <?php
  1872. echo '>';
  1873. ?>
  1874. .
  1875. <?php
  1876. echo '>';
  1877. ?>
  1878. ````````````````````````````````
  1879. A declaration (type 4):
  1880. ```````````````````````````````` example
  1881. <!DOCTYPE html>
  1882. .
  1883. <!DOCTYPE html>
  1884. ````````````````````````````````
  1885. CDATA (type 5):
  1886. ```````````````````````````````` example
  1887. <![CDATA[
  1888. function matchwo(a,b)
  1889. {
  1890. if (a < b && a < 0) then {
  1891. return 1;
  1892. } else {
  1893. return 0;
  1894. }
  1895. }
  1896. ]]>
  1897. .
  1898. <![CDATA[
  1899. function matchwo(a,b)
  1900. {
  1901. if (a < b && a < 0) then {
  1902. return 1;
  1903. } else {
  1904. return 0;
  1905. }
  1906. }
  1907. ]]>
  1908. ````````````````````````````````
  1909. The opening tag can be indented 1-3 spaces, but not 4:
  1910. ```````````````````````````````` example
  1911. <!-- foo -->
  1912. <!-- foo -->
  1913. .
  1914. <!-- foo -->
  1915. <pre><code>&lt;!-- foo --&gt;
  1916. </code></pre>
  1917. ````````````````````````````````
  1918. ```````````````````````````````` example
  1919. <div>
  1920. <div>
  1921. .
  1922. <div>
  1923. <pre><code>&lt;div&gt;
  1924. </code></pre>
  1925. ````````````````````````````````
  1926. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1927. preceded by a blank line.
  1928. ```````````````````````````````` example
  1929. Foo
  1930. <div>
  1931. bar
  1932. </div>
  1933. .
  1934. <p>Foo</p>
  1935. <div>
  1936. bar
  1937. </div>
  1938. ````````````````````````````````
  1939. However, a following blank line is needed, except at the end of
  1940. a document, and except for blocks of types 1--5, above:
  1941. ```````````````````````````````` example
  1942. <div>
  1943. bar
  1944. </div>
  1945. *foo*
  1946. .
  1947. <div>
  1948. bar
  1949. </div>
  1950. *foo*
  1951. ````````````````````````````````
  1952. HTML blocks of type 7 cannot interrupt a paragraph:
  1953. ```````````````````````````````` example
  1954. Foo
  1955. <a href="bar">
  1956. baz
  1957. .
  1958. <p>Foo
  1959. <a href="bar">
  1960. baz</p>
  1961. ````````````````````````````````
  1962. This rule differs from John Gruber's original Markdown syntax
  1963. specification, which says:
  1964. > The only restrictions are that block-level HTML elements —
  1965. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1966. > surrounding content by blank lines, and the start and end tags of the
  1967. > block should not be indented with tabs or spaces.
  1968. In some ways Gruber's rule is more restrictive than the one given
  1969. here:
  1970. - It requires that an HTML block be preceded by a blank line.
  1971. - It does not allow the start tag to be indented.
  1972. - It requires a matching end tag, which it also does not allow to
  1973. be indented.
  1974. Most Markdown implementations (including some of Gruber's own) do not
  1975. respect all of these restrictions.
  1976. There is one respect, however, in which Gruber's rule is more liberal
  1977. than the one given here, since it allows blank lines to occur inside
  1978. an HTML block. There are two reasons for disallowing them here.
  1979. First, it removes the need to parse balanced tags, which is
  1980. expensive and can require backtracking from the end of the document
  1981. if no matching end tag is found. Second, it provides a very simple
  1982. and flexible way of including Markdown content inside HTML tags:
  1983. simply separate the Markdown from the HTML using blank lines:
  1984. Compare:
  1985. ```````````````````````````````` example
  1986. <div>
  1987. *Emphasized* text.
  1988. </div>
  1989. .
  1990. <div>
  1991. <p><em>Emphasized</em> text.</p>
  1992. </div>
  1993. ````````````````````````````````
  1994. ```````````````````````````````` example
  1995. <div>
  1996. *Emphasized* text.
  1997. </div>
  1998. .
  1999. <div>
  2000. *Emphasized* text.
  2001. </div>
  2002. ````````````````````````````````
  2003. Some Markdown implementations have adopted a convention of
  2004. interpreting content inside tags as text if the open tag has
  2005. the attribute `markdown=1`. The rule given above seems a simpler and
  2006. more elegant way of achieving the same expressive power, which is also
  2007. much simpler to parse.
  2008. The main potential drawback is that one can no longer paste HTML
  2009. blocks into Markdown documents with 100% reliability. However,
  2010. *in most cases* this will work fine, because the blank lines in
  2011. HTML are usually followed by HTML block tags. For example:
  2012. ```````````````````````````````` example
  2013. <table>
  2014. <tr>
  2015. <td>
  2016. Hi
  2017. </td>
  2018. </tr>
  2019. </table>
  2020. .
  2021. <table>
  2022. <tr>
  2023. <td>
  2024. Hi
  2025. </td>
  2026. </tr>
  2027. </table>
  2028. ````````````````````````````````
  2029. There are problems, however, if the inner tags are indented
  2030. *and* separated by spaces, as then they will be interpreted as
  2031. an indented code block:
  2032. ```````````````````````````````` example
  2033. <table>
  2034. <tr>
  2035. <td>
  2036. Hi
  2037. </td>
  2038. </tr>
  2039. </table>
  2040. .
  2041. <table>
  2042. <tr>
  2043. <pre><code>&lt;td&gt;
  2044. Hi
  2045. &lt;/td&gt;
  2046. </code></pre>
  2047. </tr>
  2048. </table>
  2049. ````````````````````````````````
  2050. Fortunately, blank lines are usually not necessary and can be
  2051. deleted. The exception is inside `<pre>` tags, but as described
  2052. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2053. lines.
  2054. ## Link reference definitions
  2055. A [link reference definition](@)
  2056. consists of a [link label], indented up to three spaces, followed
  2057. by a colon (`:`), optional [whitespace] (including up to one
  2058. [line ending]), a [link destination],
  2059. optional [whitespace] (including up to one
  2060. [line ending]), and an optional [link
  2061. title], which if it is present must be separated
  2062. from the [link destination] by [whitespace].
  2063. No further [non-whitespace characters] may occur on the line.
  2064. A [link reference definition]
  2065. does not correspond to a structural element of a document. Instead, it
  2066. defines a label which can be used in [reference links]
  2067. and reference-style [images] elsewhere in the document. [Link
  2068. reference definitions] can come either before or after the links that use
  2069. them.
  2070. ```````````````````````````````` example
  2071. [foo]: /url "title"
  2072. [foo]
  2073. .
  2074. <p><a href="/url" title="title">foo</a></p>
  2075. ````````````````````````````````
  2076. ```````````````````````````````` example
  2077. [foo]:
  2078. /url
  2079. 'the title'
  2080. [foo]
  2081. .
  2082. <p><a href="/url" title="the title">foo</a></p>
  2083. ````````````````````````````````
  2084. ```````````````````````````````` example
  2085. [Foo*bar\]]:my_(url) 'title (with parens)'
  2086. [Foo*bar\]]
  2087. .
  2088. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2089. ````````````````````````````````
  2090. ```````````````````````````````` example
  2091. [Foo bar]:
  2092. <my%20url>
  2093. 'title'
  2094. [Foo bar]
  2095. .
  2096. <p><a href="my%20url" title="title">Foo bar</a></p>
  2097. ````````````````````````````````
  2098. The title may extend over multiple lines:
  2099. ```````````````````````````````` example
  2100. [foo]: /url '
  2101. title
  2102. line1
  2103. line2
  2104. '
  2105. [foo]
  2106. .
  2107. <p><a href="/url" title="
  2108. title
  2109. line1
  2110. line2
  2111. ">foo</a></p>
  2112. ````````````````````````````````
  2113. However, it may not contain a [blank line]:
  2114. ```````````````````````````````` example
  2115. [foo]: /url 'title
  2116. with blank line'
  2117. [foo]
  2118. .
  2119. <p>[foo]: /url 'title</p>
  2120. <p>with blank line'</p>
  2121. <p>[foo]</p>
  2122. ````````````````````````````````
  2123. The title may be omitted:
  2124. ```````````````````````````````` example
  2125. [foo]:
  2126. /url
  2127. [foo]
  2128. .
  2129. <p><a href="/url">foo</a></p>
  2130. ````````````````````````````````
  2131. The link destination may not be omitted:
  2132. ```````````````````````````````` example
  2133. [foo]:
  2134. [foo]
  2135. .
  2136. <p>[foo]:</p>
  2137. <p>[foo]</p>
  2138. ````````````````````````````````
  2139. Both title and destination can contain backslash escapes
  2140. and literal backslashes:
  2141. ```````````````````````````````` example
  2142. [foo]: /url\bar\*baz "foo\"bar\baz"
  2143. [foo]
  2144. .
  2145. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2146. ````````````````````````````````
  2147. A link can come before its corresponding definition:
  2148. ```````````````````````````````` example
  2149. [foo]
  2150. [foo]: url
  2151. .
  2152. <p><a href="url">foo</a></p>
  2153. ````````````````````````````````
  2154. If there are several matching definitions, the first one takes
  2155. precedence:
  2156. ```````````````````````````````` example
  2157. [foo]
  2158. [foo]: first
  2159. [foo]: second
  2160. .
  2161. <p><a href="first">foo</a></p>
  2162. ````````````````````````````````
  2163. As noted in the section on [Links], matching of labels is
  2164. case-insensitive (see [matches]).
  2165. ```````````````````````````````` example
  2166. [FOO]: /url
  2167. [Foo]
  2168. .
  2169. <p><a href="/url">Foo</a></p>
  2170. ````````````````````````````````
  2171. ```````````````````````````````` example
  2172. [ΑΓΩ]: /φου
  2173. [αγω]
  2174. .
  2175. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2176. ````````````````````````````````
  2177. Here is a link reference definition with no corresponding link.
  2178. It contributes nothing to the document.
  2179. ```````````````````````````````` example
  2180. [foo]: /url
  2181. .
  2182. ````````````````````````````````
  2183. Here is another one:
  2184. ```````````````````````````````` example
  2185. [
  2186. foo
  2187. ]: /url
  2188. bar
  2189. .
  2190. <p>bar</p>
  2191. ````````````````````````````````
  2192. This is not a link reference definition, because there are
  2193. [non-whitespace characters] after the title:
  2194. ```````````````````````````````` example
  2195. [foo]: /url "title" ok
  2196. .
  2197. <p>[foo]: /url &quot;title&quot; ok</p>
  2198. ````````````````````````````````
  2199. This is a link reference definition, but it has no title:
  2200. ```````````````````````````````` example
  2201. [foo]: /url
  2202. "title" ok
  2203. .
  2204. <p>&quot;title&quot; ok</p>
  2205. ````````````````````````````````
  2206. This is not a link reference definition, because it is indented
  2207. four spaces:
  2208. ```````````````````````````````` example
  2209. [foo]: /url "title"
  2210. [foo]
  2211. .
  2212. <pre><code>[foo]: /url &quot;title&quot;
  2213. </code></pre>
  2214. <p>[foo]</p>
  2215. ````````````````````````````````
  2216. This is not a link reference definition, because it occurs inside
  2217. a code block:
  2218. ```````````````````````````````` example
  2219. ```
  2220. [foo]: /url
  2221. ```
  2222. [foo]
  2223. .
  2224. <pre><code>[foo]: /url
  2225. </code></pre>
  2226. <p>[foo]</p>
  2227. ````````````````````````````````
  2228. A [link reference definition] cannot interrupt a paragraph.
  2229. ```````````````````````````````` example
  2230. Foo
  2231. [bar]: /baz
  2232. [bar]
  2233. .
  2234. <p>Foo
  2235. [bar]: /baz</p>
  2236. <p>[bar]</p>
  2237. ````````````````````````````````
  2238. However, it can directly follow other block elements, such as headings
  2239. and thematic breaks, and it need not be followed by a blank line.
  2240. ```````````````````````````````` example
  2241. # [Foo]
  2242. [foo]: /url
  2243. > bar
  2244. .
  2245. <h1><a href="/url">Foo</a></h1>
  2246. <blockquote>
  2247. <p>bar</p>
  2248. </blockquote>
  2249. ````````````````````````````````
  2250. Several [link reference definitions]
  2251. can occur one after another, without intervening blank lines.
  2252. ```````````````````````````````` example
  2253. [foo]: /foo-url "foo"
  2254. [bar]: /bar-url
  2255. "bar"
  2256. [baz]: /baz-url
  2257. [foo],
  2258. [bar],
  2259. [baz]
  2260. .
  2261. <p><a href="/foo-url" title="foo">foo</a>,
  2262. <a href="/bar-url" title="bar">bar</a>,
  2263. <a href="/baz-url">baz</a></p>
  2264. ````````````````````````````````
  2265. [Link reference definitions] can occur
  2266. inside block containers, like lists and block quotations. They
  2267. affect the entire document, not just the container in which they
  2268. are defined:
  2269. ```````````````````````````````` example
  2270. [foo]
  2271. > [foo]: /url
  2272. .
  2273. <p><a href="/url">foo</a></p>
  2274. <blockquote>
  2275. </blockquote>
  2276. ````````````````````````````````
  2277. ## Paragraphs
  2278. A sequence of non-blank lines that cannot be interpreted as other
  2279. kinds of blocks forms a [paragraph](@).
  2280. The contents of the paragraph are the result of parsing the
  2281. paragraph's raw content as inlines. The paragraph's raw content
  2282. is formed by concatenating the lines and removing initial and final
  2283. [whitespace].
  2284. A simple example with two paragraphs:
  2285. ```````````````````````````````` example
  2286. aaa
  2287. bbb
  2288. .
  2289. <p>aaa</p>
  2290. <p>bbb</p>
  2291. ````````````````````````````````
  2292. Paragraphs can contain multiple lines, but no blank lines:
  2293. ```````````````````````````````` example
  2294. aaa
  2295. bbb
  2296. ccc
  2297. ddd
  2298. .
  2299. <p>aaa
  2300. bbb</p>
  2301. <p>ccc
  2302. ddd</p>
  2303. ````````````````````````````````
  2304. Multiple blank lines between paragraph have no effect:
  2305. ```````````````````````````````` example
  2306. aaa
  2307. bbb
  2308. .
  2309. <p>aaa</p>
  2310. <p>bbb</p>
  2311. ````````````````````````````````
  2312. Leading spaces are skipped:
  2313. ```````````````````````````````` example
  2314. aaa
  2315. bbb
  2316. .
  2317. <p>aaa
  2318. bbb</p>
  2319. ````````````````````````````````
  2320. Lines after the first may be indented any amount, since indented
  2321. code blocks cannot interrupt paragraphs.
  2322. ```````````````````````````````` example
  2323. aaa
  2324. bbb
  2325. ccc
  2326. .
  2327. <p>aaa
  2328. bbb
  2329. ccc</p>
  2330. ````````````````````````````````
  2331. However, the first line may be indented at most three spaces,
  2332. or an indented code block will be triggered:
  2333. ```````````````````````````````` example
  2334. aaa
  2335. bbb
  2336. .
  2337. <p>aaa
  2338. bbb</p>
  2339. ````````````````````````````````
  2340. ```````````````````````````````` example
  2341. aaa
  2342. bbb
  2343. .
  2344. <pre><code>aaa
  2345. </code></pre>
  2346. <p>bbb</p>
  2347. ````````````````````````````````
  2348. Final spaces are stripped before inline parsing, so a paragraph
  2349. that ends with two or more spaces will not end with a [hard line
  2350. break]:
  2351. ```````````````````````````````` example
  2352. aaa
  2353. bbb
  2354. .
  2355. <p>aaa<br />
  2356. bbb</p>
  2357. ````````````````````````````````
  2358. ## Blank lines
  2359. [Blank lines] between block-level elements are ignored,
  2360. except for the role they play in determining whether a [list]
  2361. is [tight] or [loose].
  2362. Blank lines at the beginning and end of the document are also ignored.
  2363. ```````````````````````````````` example
  2364. aaa
  2365. # aaa
  2366. .
  2367. <p>aaa</p>
  2368. <h1>aaa</h1>
  2369. ````````````````````````````````
  2370. # Container blocks
  2371. A [container block] is a block that has other
  2372. blocks as its contents. There are two basic kinds of container blocks:
  2373. [block quotes] and [list items].
  2374. [Lists] are meta-containers for [list items].
  2375. We define the syntax for container blocks recursively. The general
  2376. form of the definition is:
  2377. > If X is a sequence of blocks, then the result of
  2378. > transforming X in such-and-such a way is a container of type Y
  2379. > with these blocks as its content.
  2380. So, we explain what counts as a block quote or list item by explaining
  2381. how these can be *generated* from their contents. This should suffice
  2382. to define the syntax, although it does not give a recipe for *parsing*
  2383. these constructions. (A recipe is provided below in the section entitled
  2384. [A parsing strategy](#appendix-a-parsing-strategy).)
  2385. ## Block quotes
  2386. A [block quote marker](@)
  2387. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2388. with a following space, or (b) a single character `>` not followed by a space.
  2389. The following rules define [block quotes]:
  2390. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2391. of blocks *Bs*, then the result of prepending a [block quote
  2392. marker] to the beginning of each line in *Ls*
  2393. is a [block quote](#block-quotes) containing *Bs*.
  2394. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2395. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2396. the initial [block quote marker] from one or
  2397. more lines in which the next [non-whitespace character] after the [block
  2398. quote marker] is [paragraph continuation
  2399. text] is a block quote with *Bs* as its content.
  2400. [Paragraph continuation text](@) is text
  2401. that will be parsed as part of the content of a paragraph, but does
  2402. not occur at the beginning of the paragraph.
  2403. 3. **Consecutiveness.** A document cannot contain two [block
  2404. quotes] in a row unless there is a [blank line] between them.
  2405. Nothing else counts as a [block quote](#block-quotes).
  2406. Here is a simple example:
  2407. ```````````````````````````````` example
  2408. > # Foo
  2409. > bar
  2410. > baz
  2411. .
  2412. <blockquote>
  2413. <h1>Foo</h1>
  2414. <p>bar
  2415. baz</p>
  2416. </blockquote>
  2417. ````````````````````````````````
  2418. The spaces after the `>` characters can be omitted:
  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 `>` characters can be indented 1-3 spaces:
  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. Four spaces gives us a code block:
  2443. ```````````````````````````````` example
  2444. > # Foo
  2445. > bar
  2446. > baz
  2447. .
  2448. <pre><code>&gt; # Foo
  2449. &gt; bar
  2450. &gt; baz
  2451. </code></pre>
  2452. ````````````````````````````````
  2453. The Laziness clause allows us to omit the `>` before a
  2454. paragraph continuation line:
  2455. ```````````````````````````````` example
  2456. > # Foo
  2457. > bar
  2458. baz
  2459. .
  2460. <blockquote>
  2461. <h1>Foo</h1>
  2462. <p>bar
  2463. baz</p>
  2464. </blockquote>
  2465. ````````````````````````````````
  2466. A block quote can contain some lazy and some non-lazy
  2467. continuation lines:
  2468. ```````````````````````````````` example
  2469. > bar
  2470. baz
  2471. > foo
  2472. .
  2473. <blockquote>
  2474. <p>bar
  2475. baz
  2476. foo</p>
  2477. </blockquote>
  2478. ````````````````````````````````
  2479. Laziness only applies to lines that would have been continuations of
  2480. paragraphs had they been prepended with [block quote markers].
  2481. For example, the `> ` cannot be omitted in the second line of
  2482. ``` markdown
  2483. > foo
  2484. > ---
  2485. ```
  2486. without changing the meaning:
  2487. ```````````````````````````````` example
  2488. > foo
  2489. ---
  2490. .
  2491. <blockquote>
  2492. <p>foo</p>
  2493. </blockquote>
  2494. <hr />
  2495. ````````````````````````````````
  2496. Similarly, if we omit the `> ` in the second line of
  2497. ``` markdown
  2498. > - foo
  2499. > - bar
  2500. ```
  2501. then the block quote ends after the first line:
  2502. ```````````````````````````````` example
  2503. > - foo
  2504. - bar
  2505. .
  2506. <blockquote>
  2507. <ul>
  2508. <li>foo</li>
  2509. </ul>
  2510. </blockquote>
  2511. <ul>
  2512. <li>bar</li>
  2513. </ul>
  2514. ````````````````````````````````
  2515. For the same reason, we can't omit the `> ` in front of
  2516. subsequent lines of an indented or fenced code block:
  2517. ```````````````````````````````` example
  2518. > foo
  2519. bar
  2520. .
  2521. <blockquote>
  2522. <pre><code>foo
  2523. </code></pre>
  2524. </blockquote>
  2525. <pre><code>bar
  2526. </code></pre>
  2527. ````````````````````````````````
  2528. ```````````````````````````````` example
  2529. > ```
  2530. foo
  2531. ```
  2532. .
  2533. <blockquote>
  2534. <pre><code></code></pre>
  2535. </blockquote>
  2536. <p>foo</p>
  2537. <pre><code></code></pre>
  2538. ````````````````````````````````
  2539. Note that in the following case, we have a paragraph
  2540. continuation line:
  2541. ```````````````````````````````` example
  2542. > foo
  2543. - bar
  2544. .
  2545. <blockquote>
  2546. <p>foo
  2547. - bar</p>
  2548. </blockquote>
  2549. ````````````````````````````````
  2550. To see why, note that in
  2551. ```markdown
  2552. > foo
  2553. > - bar
  2554. ```
  2555. the `- bar` is indented too far to start a list, and can't
  2556. be an indented code block because indented code blocks cannot
  2557. interrupt paragraphs, so it is a [paragraph continuation line].
  2558. A block quote can be empty:
  2559. ```````````````````````````````` example
  2560. >
  2561. .
  2562. <blockquote>
  2563. </blockquote>
  2564. ````````````````````````````````
  2565. ```````````````````````````````` example
  2566. >
  2567. >
  2568. >
  2569. .
  2570. <blockquote>
  2571. </blockquote>
  2572. ````````````````````````````````
  2573. A block quote can have initial or final blank lines:
  2574. ```````````````````````````````` example
  2575. >
  2576. > foo
  2577. >
  2578. .
  2579. <blockquote>
  2580. <p>foo</p>
  2581. </blockquote>
  2582. ````````````````````````````````
  2583. A blank line always separates block quotes:
  2584. ```````````````````````````````` example
  2585. > foo
  2586. > bar
  2587. .
  2588. <blockquote>
  2589. <p>foo</p>
  2590. </blockquote>
  2591. <blockquote>
  2592. <p>bar</p>
  2593. </blockquote>
  2594. ````````````````````````````````
  2595. (Most current Markdown implementations, including John Gruber's
  2596. original `Markdown.pl`, will parse this example as a single block quote
  2597. with two paragraphs. But it seems better to allow the author to decide
  2598. whether two block quotes or one are wanted.)
  2599. Consecutiveness means that if we put these block quotes together,
  2600. we get a single block quote:
  2601. ```````````````````````````````` example
  2602. > foo
  2603. > bar
  2604. .
  2605. <blockquote>
  2606. <p>foo
  2607. bar</p>
  2608. </blockquote>
  2609. ````````````````````````````````
  2610. To get a block quote with two paragraphs, use:
  2611. ```````````````````````````````` example
  2612. > foo
  2613. >
  2614. > bar
  2615. .
  2616. <blockquote>
  2617. <p>foo</p>
  2618. <p>bar</p>
  2619. </blockquote>
  2620. ````````````````````````````````
  2621. Block quotes can interrupt paragraphs:
  2622. ```````````````````````````````` example
  2623. foo
  2624. > bar
  2625. .
  2626. <p>foo</p>
  2627. <blockquote>
  2628. <p>bar</p>
  2629. </blockquote>
  2630. ````````````````````````````````
  2631. In general, blank lines are not needed before or after block
  2632. quotes:
  2633. ```````````````````````````````` example
  2634. > aaa
  2635. ***
  2636. > bbb
  2637. .
  2638. <blockquote>
  2639. <p>aaa</p>
  2640. </blockquote>
  2641. <hr />
  2642. <blockquote>
  2643. <p>bbb</p>
  2644. </blockquote>
  2645. ````````````````````````````````
  2646. However, because of laziness, a blank line is needed between
  2647. a block quote and a following paragraph:
  2648. ```````````````````````````````` example
  2649. > bar
  2650. baz
  2651. .
  2652. <blockquote>
  2653. <p>bar
  2654. baz</p>
  2655. </blockquote>
  2656. ````````````````````````````````
  2657. ```````````````````````````````` example
  2658. > bar
  2659. baz
  2660. .
  2661. <blockquote>
  2662. <p>bar</p>
  2663. </blockquote>
  2664. <p>baz</p>
  2665. ````````````````````````````````
  2666. ```````````````````````````````` example
  2667. > bar
  2668. >
  2669. baz
  2670. .
  2671. <blockquote>
  2672. <p>bar</p>
  2673. </blockquote>
  2674. <p>baz</p>
  2675. ````````````````````````````````
  2676. It is a consequence of the Laziness rule that any number
  2677. of initial `>`s may be omitted on a continuation line of a
  2678. nested block quote:
  2679. ```````````````````````````````` example
  2680. > > > foo
  2681. bar
  2682. .
  2683. <blockquote>
  2684. <blockquote>
  2685. <blockquote>
  2686. <p>foo
  2687. bar</p>
  2688. </blockquote>
  2689. </blockquote>
  2690. </blockquote>
  2691. ````````````````````````````````
  2692. ```````````````````````````````` example
  2693. >>> foo
  2694. > bar
  2695. >>baz
  2696. .
  2697. <blockquote>
  2698. <blockquote>
  2699. <blockquote>
  2700. <p>foo
  2701. bar
  2702. baz</p>
  2703. </blockquote>
  2704. </blockquote>
  2705. </blockquote>
  2706. ````````````````````````````````
  2707. When including an indented code block in a block quote,
  2708. remember that the [block quote marker] includes
  2709. both the `>` and a following space. So *five spaces* are needed after
  2710. the `>`:
  2711. ```````````````````````````````` example
  2712. > code
  2713. > not code
  2714. .
  2715. <blockquote>
  2716. <pre><code>code
  2717. </code></pre>
  2718. </blockquote>
  2719. <blockquote>
  2720. <p>not code</p>
  2721. </blockquote>
  2722. ````````````````````````````````
  2723. ## List items
  2724. A [list marker](@) is a
  2725. [bullet list marker] or an [ordered list marker].
  2726. A [bullet list marker](@)
  2727. is a `-`, `+`, or `*` character.
  2728. An [ordered list marker](@)
  2729. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2730. `.` character or a `)` character. (The reason for the length
  2731. limit is that with 10 digits we start seeing integer overflows
  2732. in some browsers.)
  2733. The following rules define [list items]:
  2734. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2735. blocks *Bs* starting with a [non-whitespace character] and not separated
  2736. from each other by more than one blank line, and *M* is a list
  2737. marker of width *W* followed by 0 < *N* < 5 spaces, then the result
  2738. of prepending *M* and the following spaces to the first line of
  2739. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2740. list item with *Bs* as its contents. The type of the list item
  2741. (bullet or ordered) is determined by the type of its list marker.
  2742. If the list item is ordered, then it is also assigned a start
  2743. number, based on the ordered list marker.
  2744. For example, let *Ls* be the lines
  2745. ```````````````````````````````` example
  2746. A paragraph
  2747. with two lines.
  2748. indented code
  2749. > A block quote.
  2750. .
  2751. <p>A paragraph
  2752. with two lines.</p>
  2753. <pre><code>indented code
  2754. </code></pre>
  2755. <blockquote>
  2756. <p>A block quote.</p>
  2757. </blockquote>
  2758. ````````````````````````````````
  2759. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2760. that the following is an ordered list item with start number 1,
  2761. and the same contents as *Ls*:
  2762. ```````````````````````````````` example
  2763. 1. A paragraph
  2764. with two lines.
  2765. indented code
  2766. > A block quote.
  2767. .
  2768. <ol>
  2769. <li>
  2770. <p>A paragraph
  2771. with two lines.</p>
  2772. <pre><code>indented code
  2773. </code></pre>
  2774. <blockquote>
  2775. <p>A block quote.</p>
  2776. </blockquote>
  2777. </li>
  2778. </ol>
  2779. ````````````````````````````````
  2780. The most important thing to notice is that the position of
  2781. the text after the list marker determines how much indentation
  2782. is needed in subsequent blocks in the list item. If the list
  2783. marker takes up two spaces, and there are three spaces between
  2784. the list marker and the next [non-whitespace character], then blocks
  2785. must be indented five spaces in order to fall under the list
  2786. item.
  2787. Here are some examples showing how far content must be indented to be
  2788. put under the list item:
  2789. ```````````````````````````````` example
  2790. - one
  2791. two
  2792. .
  2793. <ul>
  2794. <li>one</li>
  2795. </ul>
  2796. <p>two</p>
  2797. ````````````````````````````````
  2798. ```````````````````````````````` example
  2799. - one
  2800. two
  2801. .
  2802. <ul>
  2803. <li>
  2804. <p>one</p>
  2805. <p>two</p>
  2806. </li>
  2807. </ul>
  2808. ````````````````````````````````
  2809. ```````````````````````````````` example
  2810. - one
  2811. two
  2812. .
  2813. <ul>
  2814. <li>one</li>
  2815. </ul>
  2816. <pre><code> two
  2817. </code></pre>
  2818. ````````````````````````````````
  2819. ```````````````````````````````` example
  2820. - one
  2821. two
  2822. .
  2823. <ul>
  2824. <li>
  2825. <p>one</p>
  2826. <p>two</p>
  2827. </li>
  2828. </ul>
  2829. ````````````````````````````````
  2830. It is tempting to think of this in terms of columns: the continuation
  2831. blocks must be indented at least to the column of the first
  2832. [non-whitespace character] after the list marker. However, that is not quite right.
  2833. The spaces after the list marker determine how much relative indentation
  2834. is needed. Which column this indentation reaches will depend on
  2835. how the list item is embedded in other constructions, as shown by
  2836. this example:
  2837. ```````````````````````````````` example
  2838. > > 1. one
  2839. >>
  2840. >> two
  2841. .
  2842. <blockquote>
  2843. <blockquote>
  2844. <ol>
  2845. <li>
  2846. <p>one</p>
  2847. <p>two</p>
  2848. </li>
  2849. </ol>
  2850. </blockquote>
  2851. </blockquote>
  2852. ````````````````````````````````
  2853. Here `two` occurs in the same column as the list marker `1.`,
  2854. but is actually contained in the list item, because there is
  2855. sufficient indentation after the last containing blockquote marker.
  2856. The converse is also possible. In the following example, the word `two`
  2857. occurs far to the right of the initial text of the list item, `one`, but
  2858. it is not considered part of the list item, because it is not indented
  2859. far enough past the blockquote marker:
  2860. ```````````````````````````````` example
  2861. >>- one
  2862. >>
  2863. > > two
  2864. .
  2865. <blockquote>
  2866. <blockquote>
  2867. <ul>
  2868. <li>one</li>
  2869. </ul>
  2870. <p>two</p>
  2871. </blockquote>
  2872. </blockquote>
  2873. ````````````````````````````````
  2874. Note that at least one space is needed between the list marker and
  2875. any following content, so these are not list items:
  2876. ```````````````````````````````` example
  2877. -one
  2878. 2.two
  2879. .
  2880. <p>-one</p>
  2881. <p>2.two</p>
  2882. ````````````````````````````````
  2883. A list item may not contain blocks that are separated by more than
  2884. one blank line. Thus, two blank lines will end a list, unless the
  2885. two blanks are contained in a [fenced code block].
  2886. ```````````````````````````````` example
  2887. - foo
  2888. bar
  2889. - foo
  2890. bar
  2891. - ```
  2892. foo
  2893. bar
  2894. ```
  2895. - baz
  2896. + ```
  2897. foo
  2898. bar
  2899. ```
  2900. .
  2901. <ul>
  2902. <li>
  2903. <p>foo</p>
  2904. <p>bar</p>
  2905. </li>
  2906. <li>
  2907. <p>foo</p>
  2908. </li>
  2909. </ul>
  2910. <p>bar</p>
  2911. <ul>
  2912. <li>
  2913. <pre><code>foo
  2914. bar
  2915. </code></pre>
  2916. </li>
  2917. <li>
  2918. <p>baz</p>
  2919. <ul>
  2920. <li>
  2921. <pre><code>foo
  2922. bar
  2923. </code></pre>
  2924. </li>
  2925. </ul>
  2926. </li>
  2927. </ul>
  2928. ````````````````````````````````
  2929. A list item may contain any kind of block:
  2930. ```````````````````````````````` example
  2931. 1. foo
  2932. ```
  2933. bar
  2934. ```
  2935. baz
  2936. > bam
  2937. .
  2938. <ol>
  2939. <li>
  2940. <p>foo</p>
  2941. <pre><code>bar
  2942. </code></pre>
  2943. <p>baz</p>
  2944. <blockquote>
  2945. <p>bam</p>
  2946. </blockquote>
  2947. </li>
  2948. </ol>
  2949. ````````````````````````````````
  2950. A list item that contains an indented code block will preserve
  2951. empty lines within the code block verbatim, unless there are two
  2952. or more empty lines in a row (since as described above, two
  2953. blank lines end the list):
  2954. ```````````````````````````````` example
  2955. - Foo
  2956. bar
  2957. baz
  2958. .
  2959. <ul>
  2960. <li>
  2961. <p>Foo</p>
  2962. <pre><code>bar
  2963. baz
  2964. </code></pre>
  2965. </li>
  2966. </ul>
  2967. ````````````````````````````````
  2968. ```````````````````````````````` example
  2969. - Foo
  2970. bar
  2971. baz
  2972. .
  2973. <ul>
  2974. <li>
  2975. <p>Foo</p>
  2976. <pre><code>bar
  2977. </code></pre>
  2978. </li>
  2979. </ul>
  2980. <pre><code> baz
  2981. </code></pre>
  2982. ````````````````````````````````
  2983. Note that ordered list start numbers must be nine digits or less:
  2984. ```````````````````````````````` example
  2985. 123456789. ok
  2986. .
  2987. <ol start="123456789">
  2988. <li>ok</li>
  2989. </ol>
  2990. ````````````````````````````````
  2991. ```````````````````````````````` example
  2992. 1234567890. not ok
  2993. .
  2994. <p>1234567890. not ok</p>
  2995. ````````````````````````````````
  2996. A start number may begin with 0s:
  2997. ```````````````````````````````` example
  2998. 0. ok
  2999. .
  3000. <ol start="0">
  3001. <li>ok</li>
  3002. </ol>
  3003. ````````````````````````````````
  3004. ```````````````````````````````` example
  3005. 003. ok
  3006. .
  3007. <ol start="3">
  3008. <li>ok</li>
  3009. </ol>
  3010. ````````````````````````````````
  3011. A start number may not be negative:
  3012. ```````````````````````````````` example
  3013. -1. not ok
  3014. .
  3015. <p>-1. not ok</p>
  3016. ````````````````````````````````
  3017. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3018. constitute a sequence of blocks *Bs* starting with an indented code
  3019. block and not separated from each other by more than one blank line,
  3020. and *M* is a list marker of width *W* followed by
  3021. one space, then the result of prepending *M* and the following
  3022. space to the first line of *Ls*, and indenting subsequent lines of
  3023. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3024. If a line is empty, then it need not be indented. The type of the
  3025. list item (bullet or ordered) is determined by the type of its list
  3026. marker. If the list item is ordered, then it is also assigned a
  3027. start number, based on the ordered list marker.
  3028. An indented code block will have to be indented four spaces beyond
  3029. the edge of the region where text will be included in the list item.
  3030. In the following case that is 6 spaces:
  3031. ```````````````````````````````` example
  3032. - foo
  3033. bar
  3034. .
  3035. <ul>
  3036. <li>
  3037. <p>foo</p>
  3038. <pre><code>bar
  3039. </code></pre>
  3040. </li>
  3041. </ul>
  3042. ````````````````````````````````
  3043. And in this case it is 11 spaces:
  3044. ```````````````````````````````` example
  3045. 10. foo
  3046. bar
  3047. .
  3048. <ol start="10">
  3049. <li>
  3050. <p>foo</p>
  3051. <pre><code>bar
  3052. </code></pre>
  3053. </li>
  3054. </ol>
  3055. ````````````````````````````````
  3056. If the *first* block in the list item is an indented code block,
  3057. then by rule #2, the contents must be indented *one* space after the
  3058. list marker:
  3059. ```````````````````````````````` example
  3060. indented code
  3061. paragraph
  3062. more code
  3063. .
  3064. <pre><code>indented code
  3065. </code></pre>
  3066. <p>paragraph</p>
  3067. <pre><code>more code
  3068. </code></pre>
  3069. ````````````````````````````````
  3070. ```````````````````````````````` example
  3071. 1. indented code
  3072. paragraph
  3073. more code
  3074. .
  3075. <ol>
  3076. <li>
  3077. <pre><code>indented code
  3078. </code></pre>
  3079. <p>paragraph</p>
  3080. <pre><code>more code
  3081. </code></pre>
  3082. </li>
  3083. </ol>
  3084. ````````````````````````````````
  3085. Note that an additional space indent is interpreted as space
  3086. inside the code block:
  3087. ```````````````````````````````` example
  3088. 1. indented code
  3089. paragraph
  3090. more code
  3091. .
  3092. <ol>
  3093. <li>
  3094. <pre><code> indented code
  3095. </code></pre>
  3096. <p>paragraph</p>
  3097. <pre><code>more code
  3098. </code></pre>
  3099. </li>
  3100. </ol>
  3101. ````````````````````````````````
  3102. Note that rules #1 and #2 only apply to two cases: (a) cases
  3103. in which the lines to be included in a list item begin with a
  3104. [non-whitespace character], and (b) cases in which
  3105. they begin with an indented code
  3106. block. In a case like the following, where the first block begins with
  3107. a three-space indent, the rules do not allow us to form a list item by
  3108. indenting the whole thing and prepending a list marker:
  3109. ```````````````````````````````` example
  3110. foo
  3111. bar
  3112. .
  3113. <p>foo</p>
  3114. <p>bar</p>
  3115. ````````````````````````````````
  3116. ```````````````````````````````` example
  3117. - foo
  3118. bar
  3119. .
  3120. <ul>
  3121. <li>foo</li>
  3122. </ul>
  3123. <p>bar</p>
  3124. ````````````````````````````````
  3125. This is not a significant restriction, because when a block begins
  3126. with 1-3 spaces indent, the indentation can always be removed without
  3127. a change in interpretation, allowing rule #1 to be applied. So, in
  3128. the above case:
  3129. ```````````````````````````````` example
  3130. - foo
  3131. bar
  3132. .
  3133. <ul>
  3134. <li>
  3135. <p>foo</p>
  3136. <p>bar</p>
  3137. </li>
  3138. </ul>
  3139. ````````````````````````````````
  3140. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3141. starting with a single [blank line] constitute a (possibly empty)
  3142. sequence of blocks *Bs*, not separated from each other by more than
  3143. one blank line, and *M* is a list marker of width *W*,
  3144. then the result of prepending *M* to the first line of *Ls*, and
  3145. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3146. item with *Bs* as its contents.
  3147. If a line is empty, then it need not be indented. The type of the
  3148. list item (bullet or ordered) is determined by the type of its list
  3149. marker. If the list item is ordered, then it is also assigned a
  3150. start number, based on the ordered list marker.
  3151. Here are some list items that start with a blank line but are not empty:
  3152. ```````````````````````````````` example
  3153. -
  3154. foo
  3155. -
  3156. ```
  3157. bar
  3158. ```
  3159. -
  3160. baz
  3161. .
  3162. <ul>
  3163. <li>foo</li>
  3164. <li>
  3165. <pre><code>bar
  3166. </code></pre>
  3167. </li>
  3168. <li>
  3169. <pre><code>baz
  3170. </code></pre>
  3171. </li>
  3172. </ul>
  3173. ````````````````````````````````
  3174. When the list item starts with a blank line, the number of spaces
  3175. following the list marker doesn't change the required indentation:
  3176. ```````````````````````````````` example
  3177. -
  3178. foo
  3179. .
  3180. <ul>
  3181. <li>foo</li>
  3182. </ul>
  3183. ````````````````````````````````
  3184. A list item can begin with at most one blank line.
  3185. In the following example, `foo` is not part of the list
  3186. item:
  3187. ```````````````````````````````` example
  3188. -
  3189. foo
  3190. .
  3191. <ul>
  3192. <li></li>
  3193. </ul>
  3194. <p>foo</p>
  3195. ````````````````````````````````
  3196. Here is an empty bullet list item:
  3197. ```````````````````````````````` example
  3198. - foo
  3199. -
  3200. - bar
  3201. .
  3202. <ul>
  3203. <li>foo</li>
  3204. <li></li>
  3205. <li>bar</li>
  3206. </ul>
  3207. ````````````````````````````````
  3208. It does not matter whether there are spaces following the [list marker]:
  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. Here is an empty ordered list item:
  3221. ```````````````````````````````` example
  3222. 1. foo
  3223. 2.
  3224. 3. bar
  3225. .
  3226. <ol>
  3227. <li>foo</li>
  3228. <li></li>
  3229. <li>bar</li>
  3230. </ol>
  3231. ````````````````````````````````
  3232. A list may start or end with an empty list item:
  3233. ```````````````````````````````` example
  3234. *
  3235. .
  3236. <ul>
  3237. <li></li>
  3238. </ul>
  3239. ````````````````````````````````
  3240. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3241. according to rule #1, #2, or #3, then the result of indenting each line
  3242. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3243. list item with the same contents and attributes. If a line is
  3244. empty, then it need not be indented.
  3245. Indented one space:
  3246. ```````````````````````````````` example
  3247. 1. A paragraph
  3248. with two lines.
  3249. indented code
  3250. > A block quote.
  3251. .
  3252. <ol>
  3253. <li>
  3254. <p>A paragraph
  3255. with two lines.</p>
  3256. <pre><code>indented code
  3257. </code></pre>
  3258. <blockquote>
  3259. <p>A block quote.</p>
  3260. </blockquote>
  3261. </li>
  3262. </ol>
  3263. ````````````````````````````````
  3264. Indented two spaces:
  3265. ```````````````````````````````` example
  3266. 1. A paragraph
  3267. with two lines.
  3268. indented code
  3269. > A block quote.
  3270. .
  3271. <ol>
  3272. <li>
  3273. <p>A paragraph
  3274. with two lines.</p>
  3275. <pre><code>indented code
  3276. </code></pre>
  3277. <blockquote>
  3278. <p>A block quote.</p>
  3279. </blockquote>
  3280. </li>
  3281. </ol>
  3282. ````````````````````````````````
  3283. Indented three spaces:
  3284. ```````````````````````````````` example
  3285. 1. A paragraph
  3286. with two lines.
  3287. indented code
  3288. > A block quote.
  3289. .
  3290. <ol>
  3291. <li>
  3292. <p>A paragraph
  3293. with two lines.</p>
  3294. <pre><code>indented code
  3295. </code></pre>
  3296. <blockquote>
  3297. <p>A block quote.</p>
  3298. </blockquote>
  3299. </li>
  3300. </ol>
  3301. ````````````````````````````````
  3302. Four spaces indent gives a code block:
  3303. ```````````````````````````````` example
  3304. 1. A paragraph
  3305. with two lines.
  3306. indented code
  3307. > A block quote.
  3308. .
  3309. <pre><code>1. A paragraph
  3310. with two lines.
  3311. indented code
  3312. &gt; A block quote.
  3313. </code></pre>
  3314. ````````````````````````````````
  3315. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3316. item](#list-items) with contents *Bs*, then the result of deleting
  3317. some or all of the indentation from one or more lines in which the
  3318. next [non-whitespace character] after the indentation is
  3319. [paragraph continuation text] is a
  3320. list item with the same contents and attributes. The unindented
  3321. lines are called
  3322. [lazy continuation line](@)s.
  3323. Here is an example with [lazy continuation lines]:
  3324. ```````````````````````````````` example
  3325. 1. A paragraph
  3326. with two lines.
  3327. indented code
  3328. > A block quote.
  3329. .
  3330. <ol>
  3331. <li>
  3332. <p>A paragraph
  3333. with two lines.</p>
  3334. <pre><code>indented code
  3335. </code></pre>
  3336. <blockquote>
  3337. <p>A block quote.</p>
  3338. </blockquote>
  3339. </li>
  3340. </ol>
  3341. ````````````````````````````````
  3342. Indentation can be partially deleted:
  3343. ```````````````````````````````` example
  3344. 1. A paragraph
  3345. with two lines.
  3346. .
  3347. <ol>
  3348. <li>A paragraph
  3349. with two lines.</li>
  3350. </ol>
  3351. ````````````````````````````````
  3352. These examples show how laziness can work in nested structures:
  3353. ```````````````````````````````` example
  3354. > 1. > Blockquote
  3355. continued here.
  3356. .
  3357. <blockquote>
  3358. <ol>
  3359. <li>
  3360. <blockquote>
  3361. <p>Blockquote
  3362. continued here.</p>
  3363. </blockquote>
  3364. </li>
  3365. </ol>
  3366. </blockquote>
  3367. ````````````````````````````````
  3368. ```````````````````````````````` example
  3369. > 1. > Blockquote
  3370. > continued here.
  3371. .
  3372. <blockquote>
  3373. <ol>
  3374. <li>
  3375. <blockquote>
  3376. <p>Blockquote
  3377. continued here.</p>
  3378. </blockquote>
  3379. </li>
  3380. </ol>
  3381. </blockquote>
  3382. ````````````````````````````````
  3383. 6. **That's all.** Nothing that is not counted as a list item by rules
  3384. #1--5 counts as a [list item](#list-items).
  3385. The rules for sublists follow from the general rules above. A sublist
  3386. must be indented the same number of spaces a paragraph would need to be
  3387. in order to be included in the list item.
  3388. So, in this case we need two spaces indent:
  3389. ```````````````````````````````` example
  3390. - foo
  3391. - bar
  3392. - baz
  3393. .
  3394. <ul>
  3395. <li>foo
  3396. <ul>
  3397. <li>bar
  3398. <ul>
  3399. <li>baz</li>
  3400. </ul>
  3401. </li>
  3402. </ul>
  3403. </li>
  3404. </ul>
  3405. ````````````````````````````````
  3406. One is not enough:
  3407. ```````````````````````````````` example
  3408. - foo
  3409. - bar
  3410. - baz
  3411. .
  3412. <ul>
  3413. <li>foo</li>
  3414. <li>bar</li>
  3415. <li>baz</li>
  3416. </ul>
  3417. ````````````````````````````````
  3418. Here we need four, because the list marker is wider:
  3419. ```````````````````````````````` example
  3420. 10) foo
  3421. - bar
  3422. .
  3423. <ol start="10">
  3424. <li>foo
  3425. <ul>
  3426. <li>bar</li>
  3427. </ul>
  3428. </li>
  3429. </ol>
  3430. ````````````````````````````````
  3431. Three is not enough:
  3432. ```````````````````````````````` example
  3433. 10) foo
  3434. - bar
  3435. .
  3436. <ol start="10">
  3437. <li>foo</li>
  3438. </ol>
  3439. <ul>
  3440. <li>bar</li>
  3441. </ul>
  3442. ````````````````````````````````
  3443. A list may be the first block in a list item:
  3444. ```````````````````````````````` example
  3445. - - foo
  3446. .
  3447. <ul>
  3448. <li>
  3449. <ul>
  3450. <li>foo</li>
  3451. </ul>
  3452. </li>
  3453. </ul>
  3454. ````````````````````````````````
  3455. ```````````````````````````````` example
  3456. 1. - 2. foo
  3457. .
  3458. <ol>
  3459. <li>
  3460. <ul>
  3461. <li>
  3462. <ol start="2">
  3463. <li>foo</li>
  3464. </ol>
  3465. </li>
  3466. </ul>
  3467. </li>
  3468. </ol>
  3469. ````````````````````````````````
  3470. A list item can contain a heading:
  3471. ```````````````````````````````` example
  3472. - # Foo
  3473. - Bar
  3474. ---
  3475. baz
  3476. .
  3477. <ul>
  3478. <li>
  3479. <h1>Foo</h1>
  3480. </li>
  3481. <li>
  3482. <h2>Bar</h2>
  3483. baz</li>
  3484. </ul>
  3485. ````````````````````````````````
  3486. ### Motivation
  3487. John Gruber's Markdown spec says the following about list items:
  3488. 1. "List markers typically start at the left margin, but may be indented
  3489. by up to three spaces. List markers must be followed by one or more
  3490. spaces or a tab."
  3491. 2. "To make lists look nice, you can wrap items with hanging indents....
  3492. But if you don't want to, you don't have to."
  3493. 3. "List items may consist of multiple paragraphs. Each subsequent
  3494. paragraph in a list item must be indented by either 4 spaces or one
  3495. tab."
  3496. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3497. but here again, Markdown will allow you to be lazy."
  3498. 5. "To put a blockquote within a list item, the blockquote's `>`
  3499. delimiters need to be indented."
  3500. 6. "To put a code block within a list item, the code block needs to be
  3501. indented twice — 8 spaces or two tabs."
  3502. These rules specify that a paragraph under a list item must be indented
  3503. four spaces (presumably, from the left margin, rather than the start of
  3504. the list marker, but this is not said), and that code under a list item
  3505. must be indented eight spaces instead of the usual four. They also say
  3506. that a block quote must be indented, but not by how much; however, the
  3507. example given has four spaces indentation. Although nothing is said
  3508. about other kinds of block-level content, it is certainly reasonable to
  3509. infer that *all* block elements under a list item, including other
  3510. lists, must be indented four spaces. This principle has been called the
  3511. *four-space rule*.
  3512. The four-space rule is clear and principled, and if the reference
  3513. implementation `Markdown.pl` had followed it, it probably would have
  3514. become the standard. However, `Markdown.pl` allowed paragraphs and
  3515. sublists to start with only two spaces indentation, at least on the
  3516. outer level. Worse, its behavior was inconsistent: a sublist of an
  3517. outer-level list needed two spaces indentation, but a sublist of this
  3518. sublist needed three spaces. It is not surprising, then, that different
  3519. implementations of Markdown have developed very different rules for
  3520. determining what comes under a list item. (Pandoc and python-Markdown,
  3521. for example, stuck with Gruber's syntax description and the four-space
  3522. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3523. followed `Markdown.pl`'s behavior more closely.)
  3524. Unfortunately, given the divergences between implementations, there
  3525. is no way to give a spec for list items that will be guaranteed not
  3526. to break any existing documents. However, the spec given here should
  3527. correctly handle lists formatted with either the four-space rule or
  3528. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3529. in a way that is natural for a human to read.
  3530. The strategy here is to let the width and indentation of the list marker
  3531. determine the indentation necessary for blocks to fall under the list
  3532. item, rather than having a fixed and arbitrary number. The writer can
  3533. think of the body of the list item as a unit which gets indented to the
  3534. right enough to fit the list marker (and any indentation on the list
  3535. marker). (The laziness rule, #5, then allows continuation lines to be
  3536. unindented if needed.)
  3537. This rule is superior, we claim, to any rule requiring a fixed level of
  3538. indentation from the margin. The four-space rule is clear but
  3539. unnatural. It is quite unintuitive that
  3540. ``` markdown
  3541. - foo
  3542. bar
  3543. - baz
  3544. ```
  3545. should be parsed as two lists with an intervening paragraph,
  3546. ``` html
  3547. <ul>
  3548. <li>foo</li>
  3549. </ul>
  3550. <p>bar</p>
  3551. <ul>
  3552. <li>baz</li>
  3553. </ul>
  3554. ```
  3555. as the four-space rule demands, rather than a single list,
  3556. ``` html
  3557. <ul>
  3558. <li>
  3559. <p>foo</p>
  3560. <p>bar</p>
  3561. <ul>
  3562. <li>baz</li>
  3563. </ul>
  3564. </li>
  3565. </ul>
  3566. ```
  3567. The choice of four spaces is arbitrary. It can be learned, but it is
  3568. not likely to be guessed, and it trips up beginners regularly.
  3569. Would it help to adopt a two-space rule? The problem is that such
  3570. a rule, together with the rule allowing 1--3 spaces indentation of the
  3571. initial list marker, allows text that is indented *less than* the
  3572. original list marker to be included in the list item. For example,
  3573. `Markdown.pl` parses
  3574. ``` markdown
  3575. - one
  3576. two
  3577. ```
  3578. as a single list item, with `two` a continuation paragraph:
  3579. ``` html
  3580. <ul>
  3581. <li>
  3582. <p>one</p>
  3583. <p>two</p>
  3584. </li>
  3585. </ul>
  3586. ```
  3587. and similarly
  3588. ``` markdown
  3589. > - one
  3590. >
  3591. > two
  3592. ```
  3593. as
  3594. ``` html
  3595. <blockquote>
  3596. <ul>
  3597. <li>
  3598. <p>one</p>
  3599. <p>two</p>
  3600. </li>
  3601. </ul>
  3602. </blockquote>
  3603. ```
  3604. This is extremely unintuitive.
  3605. Rather than requiring a fixed indent from the margin, we could require
  3606. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3607. may itself be indented). This proposal would remove the last anomaly
  3608. discussed. Unlike the spec presented above, it would count the following
  3609. as a list item with a subparagraph, even though the paragraph `bar`
  3610. is not indented as far as the first paragraph `foo`:
  3611. ``` markdown
  3612. 10. foo
  3613. bar
  3614. ```
  3615. Arguably this text does read like a list item with `bar` as a subparagraph,
  3616. which may count in favor of the proposal. However, on this proposal indented
  3617. code would have to be indented six spaces after the list marker. And this
  3618. would break a lot of existing Markdown, which has the pattern:
  3619. ``` markdown
  3620. 1. foo
  3621. indented code
  3622. ```
  3623. where the code is indented eight spaces. The spec above, by contrast, will
  3624. parse this text as expected, since the code block's indentation is measured
  3625. from the beginning of `foo`.
  3626. The one case that needs special treatment is a list item that *starts*
  3627. with indented code. How much indentation is required in that case, since
  3628. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3629. that in such cases, we require one space indentation from the list marker
  3630. (and then the normal four spaces for the indented code). This will match the
  3631. four-space rule in cases where the list marker plus its initial indentation
  3632. takes four spaces (a common case), but diverge in other cases.
  3633. ## Lists
  3634. A [list](@) is a sequence of one or more
  3635. list items [of the same type]. The list items
  3636. may be separated by single [blank lines], but two
  3637. blank lines end all containing lists.
  3638. Two list items are [of the same type](@)
  3639. if they begin with a [list marker] of the same type.
  3640. Two list markers are of the
  3641. same type if (a) they are bullet list markers using the same character
  3642. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3643. delimiter (either `.` or `)`).
  3644. A list is an [ordered list](@)
  3645. if its constituent list items begin with
  3646. [ordered list markers], and a
  3647. [bullet list](@) if its constituent list
  3648. items begin with [bullet list markers].
  3649. The [start number](@)
  3650. of an [ordered list] is determined by the list number of
  3651. its initial list item. The numbers of subsequent list items are
  3652. disregarded.
  3653. A list is [loose](@) if any of its constituent
  3654. list items are separated by blank lines, or if any of its constituent
  3655. list items directly contain two block-level elements with a blank line
  3656. between them. Otherwise a list is [tight](@).
  3657. (The difference in HTML output is that paragraphs in a loose list are
  3658. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3659. Changing the bullet or ordered list delimiter starts a new list:
  3660. ```````````````````````````````` example
  3661. - foo
  3662. - bar
  3663. + baz
  3664. .
  3665. <ul>
  3666. <li>foo</li>
  3667. <li>bar</li>
  3668. </ul>
  3669. <ul>
  3670. <li>baz</li>
  3671. </ul>
  3672. ````````````````````````````````
  3673. ```````````````````````````````` example
  3674. 1. foo
  3675. 2. bar
  3676. 3) baz
  3677. .
  3678. <ol>
  3679. <li>foo</li>
  3680. <li>bar</li>
  3681. </ol>
  3682. <ol start="3">
  3683. <li>baz</li>
  3684. </ol>
  3685. ````````````````````````````````
  3686. In CommonMark, a list can interrupt a paragraph. That is,
  3687. no blank line is needed to separate a paragraph from a following
  3688. list:
  3689. ```````````````````````````````` example
  3690. Foo
  3691. - bar
  3692. - baz
  3693. .
  3694. <p>Foo</p>
  3695. <ul>
  3696. <li>bar</li>
  3697. <li>baz</li>
  3698. </ul>
  3699. ````````````````````````````````
  3700. `Markdown.pl` does not allow this, through fear of triggering a list
  3701. via a numeral in a hard-wrapped line:
  3702. ```````````````````````````````` example
  3703. The number of windows in my house is
  3704. 14. The number of doors is 6.
  3705. .
  3706. <p>The number of windows in my house is</p>
  3707. <ol start="14">
  3708. <li>The number of doors is 6.</li>
  3709. </ol>
  3710. ````````````````````````````````
  3711. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  3712. even though the same considerations might apply. We think that the two
  3713. cases should be treated the same. Here are two reasons for allowing
  3714. lists to interrupt paragraphs:
  3715. First, it is natural and not uncommon for people to start lists without
  3716. blank lines:
  3717. I need to buy
  3718. - new shoes
  3719. - a coat
  3720. - a plane ticket
  3721. Second, we are attracted to a
  3722. > [principle of uniformity](@):
  3723. > if a chunk of text has a certain
  3724. > meaning, it will continue to have the same meaning when put into a
  3725. > container block (such as a list item or blockquote).
  3726. (Indeed, the spec for [list items] and [block quotes] presupposes
  3727. this principle.) This principle implies that if
  3728. * I need to buy
  3729. - new shoes
  3730. - a coat
  3731. - a plane ticket
  3732. is a list item containing a paragraph followed by a nested sublist,
  3733. as all Markdown implementations agree it is (though the paragraph
  3734. may be rendered without `<p>` tags, since the list is "tight"),
  3735. then
  3736. I need to buy
  3737. - new shoes
  3738. - a coat
  3739. - a plane ticket
  3740. by itself should be a paragraph followed by a nested sublist.
  3741. Our adherence to the [principle of uniformity]
  3742. thus inclines us to think that there are two coherent packages:
  3743. 1. Require blank lines before *all* lists and blockquotes,
  3744. including lists that occur as sublists inside other list items.
  3745. 2. Require blank lines in none of these places.
  3746. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  3747. the first approach, for which there is much to be said. But the second
  3748. seems more consistent with established practice with Markdown.
  3749. There can be blank lines between items, but two blank lines end
  3750. a list:
  3751. ```````````````````````````````` example
  3752. - foo
  3753. - bar
  3754. - baz
  3755. .
  3756. <ul>
  3757. <li>
  3758. <p>foo</p>
  3759. </li>
  3760. <li>
  3761. <p>bar</p>
  3762. </li>
  3763. </ul>
  3764. <ul>
  3765. <li>baz</li>
  3766. </ul>
  3767. ````````````````````````````````
  3768. As illustrated above in the section on [list items],
  3769. two blank lines between blocks *within* a list item will also end a
  3770. list:
  3771. ```````````````````````````````` example
  3772. - foo
  3773. bar
  3774. - baz
  3775. .
  3776. <ul>
  3777. <li>foo</li>
  3778. </ul>
  3779. <p>bar</p>
  3780. <ul>
  3781. <li>baz</li>
  3782. </ul>
  3783. ````````````````````````````````
  3784. Indeed, two blank lines will end *all* containing lists:
  3785. ```````````````````````````````` example
  3786. - foo
  3787. - bar
  3788. - baz
  3789. bim
  3790. .
  3791. <ul>
  3792. <li>foo
  3793. <ul>
  3794. <li>bar
  3795. <ul>
  3796. <li>baz</li>
  3797. </ul>
  3798. </li>
  3799. </ul>
  3800. </li>
  3801. </ul>
  3802. <pre><code> bim
  3803. </code></pre>
  3804. ````````````````````````````````
  3805. Thus, two blank lines can be used to separate consecutive lists of
  3806. the same type, or to separate a list from an indented code block
  3807. that would otherwise be parsed as a subparagraph of the final list
  3808. item:
  3809. ```````````````````````````````` example
  3810. - foo
  3811. - bar
  3812. - baz
  3813. - bim
  3814. .
  3815. <ul>
  3816. <li>foo</li>
  3817. <li>bar</li>
  3818. </ul>
  3819. <ul>
  3820. <li>baz</li>
  3821. <li>bim</li>
  3822. </ul>
  3823. ````````````````````````````````
  3824. ```````````````````````````````` example
  3825. - foo
  3826. notcode
  3827. - foo
  3828. code
  3829. .
  3830. <ul>
  3831. <li>
  3832. <p>foo</p>
  3833. <p>notcode</p>
  3834. </li>
  3835. <li>
  3836. <p>foo</p>
  3837. </li>
  3838. </ul>
  3839. <pre><code>code
  3840. </code></pre>
  3841. ````````````````````````````````
  3842. List items need not be indented to the same level. The following
  3843. list items will be treated as items at the same list level,
  3844. since none is indented enough to belong to the previous list
  3845. item:
  3846. ```````````````````````````````` example
  3847. - a
  3848. - b
  3849. - c
  3850. - d
  3851. - e
  3852. - f
  3853. - g
  3854. - h
  3855. - i
  3856. .
  3857. <ul>
  3858. <li>a</li>
  3859. <li>b</li>
  3860. <li>c</li>
  3861. <li>d</li>
  3862. <li>e</li>
  3863. <li>f</li>
  3864. <li>g</li>
  3865. <li>h</li>
  3866. <li>i</li>
  3867. </ul>
  3868. ````````````````````````````````
  3869. ```````````````````````````````` example
  3870. 1. a
  3871. 2. b
  3872. 3. c
  3873. .
  3874. <ol>
  3875. <li>
  3876. <p>a</p>
  3877. </li>
  3878. <li>
  3879. <p>b</p>
  3880. </li>
  3881. <li>
  3882. <p>c</p>
  3883. </li>
  3884. </ol>
  3885. ````````````````````````````````
  3886. This is a loose list, because there is a blank line between
  3887. two of the list items:
  3888. ```````````````````````````````` example
  3889. - a
  3890. - b
  3891. - c
  3892. .
  3893. <ul>
  3894. <li>
  3895. <p>a</p>
  3896. </li>
  3897. <li>
  3898. <p>b</p>
  3899. </li>
  3900. <li>
  3901. <p>c</p>
  3902. </li>
  3903. </ul>
  3904. ````````````````````````````````
  3905. So is this, with a empty second item:
  3906. ```````````````````````````````` example
  3907. * a
  3908. *
  3909. * c
  3910. .
  3911. <ul>
  3912. <li>
  3913. <p>a</p>
  3914. </li>
  3915. <li></li>
  3916. <li>
  3917. <p>c</p>
  3918. </li>
  3919. </ul>
  3920. ````````````````````````````````
  3921. These are loose lists, even though there is no space between the items,
  3922. because one of the items directly contains two block-level elements
  3923. with a blank line between them:
  3924. ```````````````````````````````` example
  3925. - a
  3926. - b
  3927. c
  3928. - d
  3929. .
  3930. <ul>
  3931. <li>
  3932. <p>a</p>
  3933. </li>
  3934. <li>
  3935. <p>b</p>
  3936. <p>c</p>
  3937. </li>
  3938. <li>
  3939. <p>d</p>
  3940. </li>
  3941. </ul>
  3942. ````````````````````````````````
  3943. ```````````````````````````````` example
  3944. - a
  3945. - b
  3946. [ref]: /url
  3947. - d
  3948. .
  3949. <ul>
  3950. <li>
  3951. <p>a</p>
  3952. </li>
  3953. <li>
  3954. <p>b</p>
  3955. </li>
  3956. <li>
  3957. <p>d</p>
  3958. </li>
  3959. </ul>
  3960. ````````````````````````````````
  3961. This is a tight list, because the blank lines are in a code block:
  3962. ```````````````````````````````` example
  3963. - a
  3964. - ```
  3965. b
  3966. ```
  3967. - c
  3968. .
  3969. <ul>
  3970. <li>a</li>
  3971. <li>
  3972. <pre><code>b
  3973. </code></pre>
  3974. </li>
  3975. <li>c</li>
  3976. </ul>
  3977. ````````````````````````````````
  3978. This is a tight list, because the blank line is between two
  3979. paragraphs of a sublist. So the sublist is loose while
  3980. the outer list is tight:
  3981. ```````````````````````````````` example
  3982. - a
  3983. - b
  3984. c
  3985. - d
  3986. .
  3987. <ul>
  3988. <li>a
  3989. <ul>
  3990. <li>
  3991. <p>b</p>
  3992. <p>c</p>
  3993. </li>
  3994. </ul>
  3995. </li>
  3996. <li>d</li>
  3997. </ul>
  3998. ````````````````````````````````
  3999. This is a tight list, because the blank line is inside the
  4000. block quote:
  4001. ```````````````````````````````` example
  4002. * a
  4003. > b
  4004. >
  4005. * c
  4006. .
  4007. <ul>
  4008. <li>a
  4009. <blockquote>
  4010. <p>b</p>
  4011. </blockquote>
  4012. </li>
  4013. <li>c</li>
  4014. </ul>
  4015. ````````````````````````````````
  4016. This list is tight, because the consecutive block elements
  4017. are not separated by blank lines:
  4018. ```````````````````````````````` example
  4019. - a
  4020. > b
  4021. ```
  4022. c
  4023. ```
  4024. - d
  4025. .
  4026. <ul>
  4027. <li>a
  4028. <blockquote>
  4029. <p>b</p>
  4030. </blockquote>
  4031. <pre><code>c
  4032. </code></pre>
  4033. </li>
  4034. <li>d</li>
  4035. </ul>
  4036. ````````````````````````````````
  4037. A single-paragraph list is tight:
  4038. ```````````````````````````````` example
  4039. - a
  4040. .
  4041. <ul>
  4042. <li>a</li>
  4043. </ul>
  4044. ````````````````````````````````
  4045. ```````````````````````````````` example
  4046. - a
  4047. - b
  4048. .
  4049. <ul>
  4050. <li>a
  4051. <ul>
  4052. <li>b</li>
  4053. </ul>
  4054. </li>
  4055. </ul>
  4056. ````````````````````````````````
  4057. This list is loose, because of the blank line between the
  4058. two block elements in the list item:
  4059. ```````````````````````````````` example
  4060. 1. ```
  4061. foo
  4062. ```
  4063. bar
  4064. .
  4065. <ol>
  4066. <li>
  4067. <pre><code>foo
  4068. </code></pre>
  4069. <p>bar</p>
  4070. </li>
  4071. </ol>
  4072. ````````````````````````````````
  4073. Here the outer list is loose, the inner list tight:
  4074. ```````````````````````````````` example
  4075. * foo
  4076. * bar
  4077. baz
  4078. .
  4079. <ul>
  4080. <li>
  4081. <p>foo</p>
  4082. <ul>
  4083. <li>bar</li>
  4084. </ul>
  4085. <p>baz</p>
  4086. </li>
  4087. </ul>
  4088. ````````````````````````````````
  4089. ```````````````````````````````` example
  4090. - a
  4091. - b
  4092. - c
  4093. - d
  4094. - e
  4095. - f
  4096. .
  4097. <ul>
  4098. <li>
  4099. <p>a</p>
  4100. <ul>
  4101. <li>b</li>
  4102. <li>c</li>
  4103. </ul>
  4104. </li>
  4105. <li>
  4106. <p>d</p>
  4107. <ul>
  4108. <li>e</li>
  4109. <li>f</li>
  4110. </ul>
  4111. </li>
  4112. </ul>
  4113. ````````````````````````````````
  4114. # Inlines
  4115. Inlines are parsed sequentially from the beginning of the character
  4116. stream to the end (left to right, in left-to-right languages).
  4117. Thus, for example, in
  4118. ```````````````````````````````` example
  4119. `hi`lo`
  4120. .
  4121. <p><code>hi</code>lo`</p>
  4122. ````````````````````````````````
  4123. `hi` is parsed as code, leaving the backtick at the end as a literal
  4124. backtick.
  4125. ## Backslash escapes
  4126. Any ASCII punctuation character may be backslash-escaped:
  4127. ```````````````````````````````` example
  4128. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4129. .
  4130. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4131. ````````````````````````````````
  4132. Backslashes before other characters are treated as literal
  4133. backslashes:
  4134. ```````````````````````````````` example
  4135. \→\A\a\ \3\φ\«
  4136. .
  4137. <p>\→\A\a\ \3\φ\«</p>
  4138. ````````````````````````````````
  4139. Escaped characters are treated as regular characters and do
  4140. not have their usual Markdown meanings:
  4141. ```````````````````````````````` example
  4142. \*not emphasized*
  4143. \<br/> not a tag
  4144. \[not a link](/foo)
  4145. \`not code`
  4146. 1\. not a list
  4147. \* not a list
  4148. \# not a heading
  4149. \[foo]: /url "not a reference"
  4150. .
  4151. <p>*not emphasized*
  4152. &lt;br/&gt; not a tag
  4153. [not a link](/foo)
  4154. `not code`
  4155. 1. not a list
  4156. * not a list
  4157. # not a heading
  4158. [foo]: /url &quot;not a reference&quot;</p>
  4159. ````````````````````````````````
  4160. If a backslash is itself escaped, the following character is not:
  4161. ```````````````````````````````` example
  4162. \\*emphasis*
  4163. .
  4164. <p>\<em>emphasis</em></p>
  4165. ````````````````````````````````
  4166. A backslash at the end of the line is a [hard line break]:
  4167. ```````````````````````````````` example
  4168. foo\
  4169. bar
  4170. .
  4171. <p>foo<br />
  4172. bar</p>
  4173. ````````````````````````````````
  4174. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4175. raw HTML:
  4176. ```````````````````````````````` example
  4177. `` \[\` ``
  4178. .
  4179. <p><code>\[\`</code></p>
  4180. ````````````````````````````````
  4181. ```````````````````````````````` example
  4182. \[\]
  4183. .
  4184. <pre><code>\[\]
  4185. </code></pre>
  4186. ````````````````````````````````
  4187. ```````````````````````````````` example
  4188. ~~~
  4189. \[\]
  4190. ~~~
  4191. .
  4192. <pre><code>\[\]
  4193. </code></pre>
  4194. ````````````````````````````````
  4195. ```````````````````````````````` example
  4196. <http://example.com?find=\*>
  4197. .
  4198. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4199. ````````````````````````````````
  4200. ```````````````````````````````` example
  4201. <a href="/bar\/)">
  4202. .
  4203. <a href="/bar\/)">
  4204. ````````````````````````````````
  4205. But they work in all other contexts, including URLs and link titles,
  4206. link references, and [info strings] in [fenced code blocks]:
  4207. ```````````````````````````````` example
  4208. [foo](/bar\* "ti\*tle")
  4209. .
  4210. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4211. ````````````````````````````````
  4212. ```````````````````````````````` example
  4213. [foo]
  4214. [foo]: /bar\* "ti\*tle"
  4215. .
  4216. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4217. ````````````````````````````````
  4218. ```````````````````````````````` example
  4219. ``` foo\+bar
  4220. foo
  4221. ```
  4222. .
  4223. <pre><code class="language-foo+bar">foo
  4224. </code></pre>
  4225. ````````````````````````````````
  4226. ## Entity and numeric character references
  4227. All valid HTML entity references and numeric character
  4228. references, except those occuring in code blocks and code spans,
  4229. are recognized as such and treated as equivalent to the
  4230. corresponding Unicode characters. Conforming CommonMark parsers
  4231. need not store information about whether a particular character
  4232. was represented in the source using a Unicode character or
  4233. an entity reference.
  4234. [Entity references](@) consist of `&` + any of the valid
  4235. HTML5 entity names + `;`. The
  4236. document <https://html.spec.whatwg.org/multipage/entities.json>
  4237. is used as an authoritative source for the valid entity
  4238. references and their corresponding code points.
  4239. ```````````````````````````````` example
  4240. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4241. &frac34; &HilbertSpace; &DifferentialD;
  4242. &ClockwiseContourIntegral; &ngE;
  4243. .
  4244. <p>  &amp; © Æ Ď
  4245. ¾ ℋ ⅆ
  4246. ∲ ≧̸</p>
  4247. ````````````````````````````````
  4248. [Decimal numeric character
  4249. references](@)
  4250. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4251. numeric character reference is parsed as the corresponding
  4252. Unicode character. Invalid Unicode code points will be replaced by
  4253. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4254. the code point `U+0000` will also be replaced by `U+FFFD`.
  4255. ```````````````````````````````` example
  4256. &#35; &#1234; &#992; &#98765432; &#0;
  4257. .
  4258. <p># Ӓ Ϡ � �</p>
  4259. ````````````````````````````````
  4260. [Hexadecimal numeric character
  4261. references](@) consist of `&#` +
  4262. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4263. They too are parsed as the corresponding Unicode character (this
  4264. time specified with a hexadecimal numeral instead of decimal).
  4265. ```````````````````````````````` example
  4266. &#X22; &#XD06; &#xcab;
  4267. .
  4268. <p>&quot; ആ ಫ</p>
  4269. ````````````````````````````````
  4270. Here are some nonentities:
  4271. ```````````````````````````````` example
  4272. &nbsp &x; &#; &#x;
  4273. &ThisIsNotDefined; &hi?;
  4274. .
  4275. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4276. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4277. ````````````````````````````````
  4278. Although HTML5 does accept some entity references
  4279. without a trailing semicolon (such as `&copy`), these are not
  4280. recognized here, because it makes the grammar too ambiguous:
  4281. ```````````````````````````````` example
  4282. &copy
  4283. .
  4284. <p>&amp;copy</p>
  4285. ````````````````````````````````
  4286. Strings that are not on the list of HTML5 named entities are not
  4287. recognized as entity references either:
  4288. ```````````````````````````````` example
  4289. &MadeUpEntity;
  4290. .
  4291. <p>&amp;MadeUpEntity;</p>
  4292. ````````````````````````````````
  4293. Entity and numeric character references are recognized in any
  4294. context besides code spans or code blocks, including
  4295. URLs, [link titles], and [fenced code block][] [info strings]:
  4296. ```````````````````````````````` example
  4297. <a href="&ouml;&ouml;.html">
  4298. .
  4299. <a href="&ouml;&ouml;.html">
  4300. ````````````````````````````````
  4301. ```````````````````````````````` example
  4302. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4303. .
  4304. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4305. ````````````````````````````````
  4306. ```````````````````````````````` example
  4307. [foo]
  4308. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4309. .
  4310. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4311. ````````````````````````````````
  4312. ```````````````````````````````` example
  4313. ``` f&ouml;&ouml;
  4314. foo
  4315. ```
  4316. .
  4317. <pre><code class="language-föö">foo
  4318. </code></pre>
  4319. ````````````````````````````````
  4320. Entity and numeric character references are treated as literal
  4321. text in code spans and code blocks:
  4322. ```````````````````````````````` example
  4323. `f&ouml;&ouml;`
  4324. .
  4325. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4326. ````````````````````````````````
  4327. ```````````````````````````````` example
  4328. f&ouml;f&ouml;
  4329. .
  4330. <pre><code>f&amp;ouml;f&amp;ouml;
  4331. </code></pre>
  4332. ````````````````````````````````
  4333. ## Code spans
  4334. A [backtick string](@)
  4335. is a string of one or more backtick characters (`` ` ``) that is neither
  4336. preceded nor followed by a backtick.
  4337. A [code span](@) begins with a backtick string and ends with
  4338. a backtick string of equal length. The contents of the code span are
  4339. the characters between the two backtick strings, with leading and
  4340. trailing spaces and [line endings] removed, and
  4341. [whitespace] collapsed to single spaces.
  4342. This is a simple code span:
  4343. ```````````````````````````````` example
  4344. `foo`
  4345. .
  4346. <p><code>foo</code></p>
  4347. ````````````````````````````````
  4348. Here two backticks are used, because the code contains a backtick.
  4349. This example also illustrates stripping of leading and trailing spaces:
  4350. ```````````````````````````````` example
  4351. `` foo ` bar ``
  4352. .
  4353. <p><code>foo ` bar</code></p>
  4354. ````````````````````````````````
  4355. This example shows the motivation for stripping leading and trailing
  4356. spaces:
  4357. ```````````````````````````````` example
  4358. ` `` `
  4359. .
  4360. <p><code>``</code></p>
  4361. ````````````````````````````````
  4362. [Line endings] are treated like spaces:
  4363. ```````````````````````````````` example
  4364. ``
  4365. foo
  4366. ``
  4367. .
  4368. <p><code>foo</code></p>
  4369. ````````````````````````````````
  4370. Interior spaces and [line endings] are collapsed into
  4371. single spaces, just as they would be by a browser:
  4372. ```````````````````````````````` example
  4373. `foo bar
  4374. baz`
  4375. .
  4376. <p><code>foo bar baz</code></p>
  4377. ````````````````````````````````
  4378. Q: Why not just leave the spaces, since browsers will collapse them
  4379. anyway? A: Because we might be targeting a non-HTML format, and we
  4380. shouldn't rely on HTML-specific rendering assumptions.
  4381. (Existing implementations differ in their treatment of internal
  4382. spaces and [line endings]. Some, including `Markdown.pl` and
  4383. `showdown`, convert an internal [line ending] into a
  4384. `<br />` tag. But this makes things difficult for those who like to
  4385. hard-wrap their paragraphs, since a line break in the midst of a code
  4386. span will cause an unintended line break in the output. Others just
  4387. leave internal spaces as they are, which is fine if only HTML is being
  4388. targeted.)
  4389. ```````````````````````````````` example
  4390. `foo `` bar`
  4391. .
  4392. <p><code>foo `` bar</code></p>
  4393. ````````````````````````````````
  4394. Note that backslash escapes do not work in code spans. All backslashes
  4395. are treated literally:
  4396. ```````````````````````````````` example
  4397. `foo\`bar`
  4398. .
  4399. <p><code>foo\</code>bar`</p>
  4400. ````````````````````````````````
  4401. Backslash escapes are never needed, because one can always choose a
  4402. string of *n* backtick characters as delimiters, where the code does
  4403. not contain any strings of exactly *n* backtick characters.
  4404. Code span backticks have higher precedence than any other inline
  4405. constructs except HTML tags and autolinks. Thus, for example, this is
  4406. not parsed as emphasized text, since the second `*` is part of a code
  4407. span:
  4408. ```````````````````````````````` example
  4409. *foo`*`
  4410. .
  4411. <p>*foo<code>*</code></p>
  4412. ````````````````````````````````
  4413. And this is not parsed as a link:
  4414. ```````````````````````````````` example
  4415. [not a `link](/foo`)
  4416. .
  4417. <p>[not a <code>link](/foo</code>)</p>
  4418. ````````````````````````````````
  4419. Code spans, HTML tags, and autolinks have the same precedence.
  4420. Thus, this is code:
  4421. ```````````````````````````````` example
  4422. `<a href="`">`
  4423. .
  4424. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4425. ````````````````````````````````
  4426. But this is an HTML tag:
  4427. ```````````````````````````````` example
  4428. <a href="`">`
  4429. .
  4430. <p><a href="`">`</p>
  4431. ````````````````````````````````
  4432. And this is code:
  4433. ```````````````````````````````` example
  4434. `<http://foo.bar.`baz>`
  4435. .
  4436. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4437. ````````````````````````````````
  4438. But this is an autolink:
  4439. ```````````````````````````````` example
  4440. <http://foo.bar.`baz>`
  4441. .
  4442. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4443. ````````````````````````````````
  4444. When a backtick string is not closed by a matching backtick string,
  4445. we just have literal backticks:
  4446. ```````````````````````````````` example
  4447. ```foo``
  4448. .
  4449. <p>```foo``</p>
  4450. ````````````````````````````````
  4451. ```````````````````````````````` example
  4452. `foo
  4453. .
  4454. <p>`foo</p>
  4455. ````````````````````````````````
  4456. ## Emphasis and strong emphasis
  4457. John Gruber's original [Markdown syntax
  4458. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4459. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4460. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4461. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4462. > tag.
  4463. This is enough for most users, but these rules leave much undecided,
  4464. especially when it comes to nested emphasis. The original
  4465. `Markdown.pl` test suite makes it clear that triple `***` and
  4466. `___` delimiters can be used for strong emphasis, and most
  4467. implementations have also allowed the following patterns:
  4468. ``` markdown
  4469. ***strong emph***
  4470. ***strong** in emph*
  4471. ***emph* in strong**
  4472. **in strong *emph***
  4473. *in emph **strong***
  4474. ```
  4475. The following patterns are less widely supported, but the intent
  4476. is clear and they are useful (especially in contexts like bibliography
  4477. entries):
  4478. ``` markdown
  4479. *emph *with emph* in it*
  4480. **strong **with strong** in it**
  4481. ```
  4482. Many implementations have also restricted intraword emphasis to
  4483. the `*` forms, to avoid unwanted emphasis in words containing
  4484. internal underscores. (It is best practice to put these in code
  4485. spans, but users often do not.)
  4486. ``` markdown
  4487. internal emphasis: foo*bar*baz
  4488. no emphasis: foo_bar_baz
  4489. ```
  4490. The rules given below capture all of these patterns, while allowing
  4491. for efficient parsing strategies that do not backtrack.
  4492. First, some definitions. A [delimiter run](@) is either
  4493. a sequence of one or more `*` characters that is not preceded or
  4494. followed by a `*` character, or a sequence of one or more `_`
  4495. characters that is not preceded or followed by a `_` character.
  4496. A [left-flanking delimiter run](@) is
  4497. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4498. and (b) either not followed by a [punctuation character], or
  4499. preceded by [Unicode whitespace] or a [punctuation character].
  4500. For purposes of this definition, the beginning and the end of
  4501. the line count as Unicode whitespace.
  4502. A [right-flanking delimiter run](@) is
  4503. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4504. and (b) either not preceded by a [punctuation character], or
  4505. followed by [Unicode whitespace] or a [punctuation character].
  4506. For purposes of this definition, the beginning and the end of
  4507. the line count as Unicode whitespace.
  4508. Here are some examples of delimiter runs.
  4509. - left-flanking but not right-flanking:
  4510. ```
  4511. ***abc
  4512. _abc
  4513. **"abc"
  4514. _"abc"
  4515. ```
  4516. - right-flanking but not left-flanking:
  4517. ```
  4518. abc***
  4519. abc_
  4520. "abc"**
  4521. "abc"_
  4522. ```
  4523. - Both left and right-flanking:
  4524. ```
  4525. abc***def
  4526. "abc"_"def"
  4527. ```
  4528. - Neither left nor right-flanking:
  4529. ```
  4530. abc *** def
  4531. a _ b
  4532. ```
  4533. (The idea of distinguishing left-flanking and right-flanking
  4534. delimiter runs based on the character before and the character
  4535. after comes from Roopesh Chander's
  4536. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4537. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4538. run," and its rules for distinguishing left- and right-flanking runs
  4539. are a bit more complex than the ones given here.)
  4540. The following rules define emphasis and strong emphasis:
  4541. 1. A single `*` character [can open emphasis](@)
  4542. iff (if and only if) it is part of a [left-flanking delimiter run].
  4543. 2. A single `_` character [can open emphasis] iff
  4544. it is part of a [left-flanking delimiter run]
  4545. and either (a) not part of a [right-flanking delimiter run]
  4546. or (b) part of a [right-flanking delimiter run]
  4547. preceded by punctuation.
  4548. 3. A single `*` character [can close emphasis](@)
  4549. iff it is part of a [right-flanking delimiter run].
  4550. 4. A single `_` character [can close emphasis] iff
  4551. it is part of a [right-flanking delimiter run]
  4552. and either (a) not part of a [left-flanking delimiter run]
  4553. or (b) part of a [left-flanking delimiter run]
  4554. followed by punctuation.
  4555. 5. A double `**` [can open strong emphasis](@)
  4556. iff it is part of a [left-flanking delimiter run].
  4557. 6. A double `__` [can open strong emphasis] iff
  4558. it is part of a [left-flanking delimiter run]
  4559. and either (a) not part of a [right-flanking delimiter run]
  4560. or (b) part of a [right-flanking delimiter run]
  4561. preceded by punctuation.
  4562. 7. A double `**` [can close strong emphasis](@)
  4563. iff it is part of a [right-flanking delimiter run].
  4564. 8. A double `__` [can close strong emphasis]
  4565. it is part of a [right-flanking delimiter run]
  4566. and either (a) not part of a [left-flanking delimiter run]
  4567. or (b) part of a [left-flanking delimiter run]
  4568. followed by punctuation.
  4569. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4570. with a delimiter that [can close emphasis], and that uses the same
  4571. character (`_` or `*`) as the opening delimiter. There must
  4572. be a nonempty sequence of inlines between the open delimiter
  4573. and the closing delimiter; these form the contents of the emphasis
  4574. inline.
  4575. 10. Strong emphasis begins with a delimiter that
  4576. [can open strong emphasis] and ends with a delimiter that
  4577. [can close strong emphasis], and that uses the same character
  4578. (`_` or `*`) as the opening delimiter.
  4579. There must be a nonempty sequence of inlines between the open
  4580. delimiter and the closing delimiter; these form the contents of
  4581. the strong emphasis inline.
  4582. 11. A literal `*` character cannot occur at the beginning or end of
  4583. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4584. is backslash-escaped.
  4585. 12. A literal `_` character cannot occur at the beginning or end of
  4586. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4587. is backslash-escaped.
  4588. Where rules 1--12 above are compatible with multiple parsings,
  4589. the following principles resolve ambiguity:
  4590. 13. The number of nestings should be minimized. Thus, for example,
  4591. an interpretation `<strong>...</strong>` is always preferred to
  4592. `<em><em>...</em></em>`.
  4593. 14. An interpretation `<strong><em>...</em></strong>` is always
  4594. preferred to `<em><strong>..</strong></em>`.
  4595. 15. When two potential emphasis or strong emphasis spans overlap,
  4596. so that the second begins before the first ends and ends after
  4597. the first ends, the first takes precedence. Thus, for example,
  4598. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4599. than `*foo <em>bar* baz</em>`. For the same reason,
  4600. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  4601. rather than `<strong>foo*bar</strong>`.
  4602. 16. When there are two potential emphasis or strong emphasis spans
  4603. with the same closing delimiter, the shorter one (the one that
  4604. opens later) takes precedence. Thus, for example,
  4605. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4606. rather than `<strong>foo **bar baz</strong>`.
  4607. 17. Inline code spans, links, images, and HTML tags group more tightly
  4608. than emphasis. So, when there is a choice between an interpretation
  4609. that contains one of these elements and one that does not, the
  4610. former always wins. Thus, for example, `*[foo*](bar)` is
  4611. parsed as `*<a href="bar">foo*</a>` rather than as
  4612. `<em>[foo</em>](bar)`.
  4613. These rules can be illustrated through a series of examples.
  4614. Rule 1:
  4615. ```````````````````````````````` example
  4616. *foo bar*
  4617. .
  4618. <p><em>foo bar</em></p>
  4619. ````````````````````````````````
  4620. This is not emphasis, because the opening `*` is followed by
  4621. whitespace, and hence not part of a [left-flanking delimiter run]:
  4622. ```````````````````````````````` example
  4623. a * foo bar*
  4624. .
  4625. <p>a * foo bar*</p>
  4626. ````````````````````````````````
  4627. This is not emphasis, because the opening `*` is preceded
  4628. by an alphanumeric and followed by punctuation, and hence
  4629. not part of a [left-flanking delimiter run]:
  4630. ```````````````````````````````` example
  4631. a*"foo"*
  4632. .
  4633. <p>a*&quot;foo&quot;*</p>
  4634. ````````````````````````````````
  4635. Unicode nonbreaking spaces count as whitespace, too:
  4636. ```````````````````````````````` example
  4637. * a *
  4638. .
  4639. <p>* a *</p>
  4640. ````````````````````````````````
  4641. Intraword emphasis with `*` is permitted:
  4642. ```````````````````````````````` example
  4643. foo*bar*
  4644. .
  4645. <p>foo<em>bar</em></p>
  4646. ````````````````````````````````
  4647. ```````````````````````````````` example
  4648. 5*6*78
  4649. .
  4650. <p>5<em>6</em>78</p>
  4651. ````````````````````````````````
  4652. Rule 2:
  4653. ```````````````````````````````` example
  4654. _foo bar_
  4655. .
  4656. <p><em>foo bar</em></p>
  4657. ````````````````````````````````
  4658. This is not emphasis, because the opening `_` is followed by
  4659. whitespace:
  4660. ```````````````````````````````` example
  4661. _ foo bar_
  4662. .
  4663. <p>_ foo bar_</p>
  4664. ````````````````````````````````
  4665. This is not emphasis, because the opening `_` is preceded
  4666. by an alphanumeric and followed by punctuation:
  4667. ```````````````````````````````` example
  4668. a_"foo"_
  4669. .
  4670. <p>a_&quot;foo&quot;_</p>
  4671. ````````````````````````````````
  4672. Emphasis with `_` is not allowed inside words:
  4673. ```````````````````````````````` example
  4674. foo_bar_
  4675. .
  4676. <p>foo_bar_</p>
  4677. ````````````````````````````````
  4678. ```````````````````````````````` example
  4679. 5_6_78
  4680. .
  4681. <p>5_6_78</p>
  4682. ````````````````````````````````
  4683. ```````````````````````````````` example
  4684. пристаням_стремятся_
  4685. .
  4686. <p>пристаням_стремятся_</p>
  4687. ````````````````````````````````
  4688. Here `_` does not generate emphasis, because the first delimiter run
  4689. is right-flanking and the second left-flanking:
  4690. ```````````````````````````````` example
  4691. aa_"bb"_cc
  4692. .
  4693. <p>aa_&quot;bb&quot;_cc</p>
  4694. ````````````````````````````````
  4695. This is emphasis, even though the opening delimiter is
  4696. both left- and right-flanking, because it is preceded by
  4697. punctuation:
  4698. ```````````````````````````````` example
  4699. foo-_(bar)_
  4700. .
  4701. <p>foo-<em>(bar)</em></p>
  4702. ````````````````````````````````
  4703. Rule 3:
  4704. This is not emphasis, because the closing delimiter does
  4705. not match the opening delimiter:
  4706. ```````````````````````````````` example
  4707. _foo*
  4708. .
  4709. <p>_foo*</p>
  4710. ````````````````````````````````
  4711. This is not emphasis, because the closing `*` is preceded by
  4712. whitespace:
  4713. ```````````````````````````````` example
  4714. *foo bar *
  4715. .
  4716. <p>*foo bar *</p>
  4717. ````````````````````````````````
  4718. A newline also counts as whitespace:
  4719. ```````````````````````````````` example
  4720. *foo bar
  4721. *
  4722. .
  4723. <p>*foo bar</p>
  4724. <ul>
  4725. <li></li>
  4726. </ul>
  4727. ````````````````````````````````
  4728. This is not emphasis, because the second `*` is
  4729. preceded by punctuation and followed by an alphanumeric
  4730. (hence it is not part of a [right-flanking delimiter run]:
  4731. ```````````````````````````````` example
  4732. *(*foo)
  4733. .
  4734. <p>*(*foo)</p>
  4735. ````````````````````````````````
  4736. The point of this restriction is more easily appreciated
  4737. with this example:
  4738. ```````````````````````````````` example
  4739. *(*foo*)*
  4740. .
  4741. <p><em>(<em>foo</em>)</em></p>
  4742. ````````````````````````````````
  4743. Intraword emphasis with `*` is allowed:
  4744. ```````````````````````````````` example
  4745. *foo*bar
  4746. .
  4747. <p><em>foo</em>bar</p>
  4748. ````````````````````````````````
  4749. Rule 4:
  4750. This is not emphasis, because the closing `_` is preceded by
  4751. whitespace:
  4752. ```````````````````````````````` example
  4753. _foo bar _
  4754. .
  4755. <p>_foo bar _</p>
  4756. ````````````````````````````````
  4757. This is not emphasis, because the second `_` is
  4758. preceded by punctuation and followed by an alphanumeric:
  4759. ```````````````````````````````` example
  4760. _(_foo)
  4761. .
  4762. <p>_(_foo)</p>
  4763. ````````````````````````````````
  4764. This is emphasis within emphasis:
  4765. ```````````````````````````````` example
  4766. _(_foo_)_
  4767. .
  4768. <p><em>(<em>foo</em>)</em></p>
  4769. ````````````````````````````````
  4770. Intraword emphasis is disallowed for `_`:
  4771. ```````````````````````````````` example
  4772. _foo_bar
  4773. .
  4774. <p>_foo_bar</p>
  4775. ````````````````````````````````
  4776. ```````````````````````````````` example
  4777. _пристаням_стремятся
  4778. .
  4779. <p>_пристаням_стремятся</p>
  4780. ````````````````````````````````
  4781. ```````````````````````````````` example
  4782. _foo_bar_baz_
  4783. .
  4784. <p><em>foo_bar_baz</em></p>
  4785. ````````````````````````````````
  4786. This is emphasis, even though the closing delimiter is
  4787. both left- and right-flanking, because it is followed by
  4788. punctuation:
  4789. ```````````````````````````````` example
  4790. _(bar)_.
  4791. .
  4792. <p><em>(bar)</em>.</p>
  4793. ````````````````````````````````
  4794. Rule 5:
  4795. ```````````````````````````````` example
  4796. **foo bar**
  4797. .
  4798. <p><strong>foo bar</strong></p>
  4799. ````````````````````````````````
  4800. This is not strong emphasis, because the opening delimiter is
  4801. followed by whitespace:
  4802. ```````````````````````````````` example
  4803. ** foo bar**
  4804. .
  4805. <p>** foo bar**</p>
  4806. ````````````````````````````````
  4807. This is not strong emphasis, because the opening `**` is preceded
  4808. by an alphanumeric and followed by punctuation, and hence
  4809. not part of a [left-flanking delimiter run]:
  4810. ```````````````````````````````` example
  4811. a**"foo"**
  4812. .
  4813. <p>a**&quot;foo&quot;**</p>
  4814. ````````````````````````````````
  4815. Intraword strong emphasis with `**` is permitted:
  4816. ```````````````````````````````` example
  4817. foo**bar**
  4818. .
  4819. <p>foo<strong>bar</strong></p>
  4820. ````````````````````````````````
  4821. Rule 6:
  4822. ```````````````````````````````` example
  4823. __foo bar__
  4824. .
  4825. <p><strong>foo bar</strong></p>
  4826. ````````````````````````````````
  4827. This is not strong emphasis, because the opening delimiter is
  4828. followed by whitespace:
  4829. ```````````````````````````````` example
  4830. __ foo bar__
  4831. .
  4832. <p>__ foo bar__</p>
  4833. ````````````````````````````````
  4834. A newline counts as whitespace:
  4835. ```````````````````````````````` example
  4836. __
  4837. foo bar__
  4838. .
  4839. <p>__
  4840. foo bar__</p>
  4841. ````````````````````````````````
  4842. This is not strong emphasis, because the opening `__` is preceded
  4843. by an alphanumeric and followed by punctuation:
  4844. ```````````````````````````````` example
  4845. a__"foo"__
  4846. .
  4847. <p>a__&quot;foo&quot;__</p>
  4848. ````````````````````````````````
  4849. Intraword strong emphasis is forbidden with `__`:
  4850. ```````````````````````````````` example
  4851. foo__bar__
  4852. .
  4853. <p>foo__bar__</p>
  4854. ````````````````````````````````
  4855. ```````````````````````````````` example
  4856. 5__6__78
  4857. .
  4858. <p>5__6__78</p>
  4859. ````````````````````````````````
  4860. ```````````````````````````````` example
  4861. пристаням__стремятся__
  4862. .
  4863. <p>пристаням__стремятся__</p>
  4864. ````````````````````````````````
  4865. ```````````````````````````````` example
  4866. __foo, __bar__, baz__
  4867. .
  4868. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4869. ````````````````````````````````
  4870. This is strong emphasis, even though the opening delimiter is
  4871. both left- and right-flanking, because it is preceded by
  4872. punctuation:
  4873. ```````````````````````````````` example
  4874. foo-__(bar)__
  4875. .
  4876. <p>foo-<strong>(bar)</strong></p>
  4877. ````````````````````````````````
  4878. Rule 7:
  4879. This is not strong emphasis, because the closing delimiter is preceded
  4880. by whitespace:
  4881. ```````````````````````````````` example
  4882. **foo bar **
  4883. .
  4884. <p>**foo bar **</p>
  4885. ````````````````````````````````
  4886. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4887. Rule 11.)
  4888. This is not strong emphasis, because the second `**` is
  4889. preceded by punctuation and followed by an alphanumeric:
  4890. ```````````````````````````````` example
  4891. **(**foo)
  4892. .
  4893. <p>**(**foo)</p>
  4894. ````````````````````````````````
  4895. The point of this restriction is more easily appreciated
  4896. with these examples:
  4897. ```````````````````````````````` example
  4898. *(**foo**)*
  4899. .
  4900. <p><em>(<strong>foo</strong>)</em></p>
  4901. ````````````````````````````````
  4902. ```````````````````````````````` example
  4903. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4904. *Asclepias physocarpa*)**
  4905. .
  4906. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4907. <em>Asclepias physocarpa</em>)</strong></p>
  4908. ````````````````````````````````
  4909. ```````````````````````````````` example
  4910. **foo "*bar*" foo**
  4911. .
  4912. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4913. ````````````````````````````````
  4914. Intraword emphasis:
  4915. ```````````````````````````````` example
  4916. **foo**bar
  4917. .
  4918. <p><strong>foo</strong>bar</p>
  4919. ````````````````````````````````
  4920. Rule 8:
  4921. This is not strong emphasis, because the closing delimiter is
  4922. preceded by whitespace:
  4923. ```````````````````````````````` example
  4924. __foo bar __
  4925. .
  4926. <p>__foo bar __</p>
  4927. ````````````````````````````````
  4928. This is not strong emphasis, because the second `__` is
  4929. preceded by punctuation and followed by an alphanumeric:
  4930. ```````````````````````````````` example
  4931. __(__foo)
  4932. .
  4933. <p>__(__foo)</p>
  4934. ````````````````````````````````
  4935. The point of this restriction is more easily appreciated
  4936. with this example:
  4937. ```````````````````````````````` example
  4938. _(__foo__)_
  4939. .
  4940. <p><em>(<strong>foo</strong>)</em></p>
  4941. ````````````````````````````````
  4942. Intraword strong emphasis is forbidden with `__`:
  4943. ```````````````````````````````` example
  4944. __foo__bar
  4945. .
  4946. <p>__foo__bar</p>
  4947. ````````````````````````````````
  4948. ```````````````````````````````` example
  4949. __пристаням__стремятся
  4950. .
  4951. <p>__пристаням__стремятся</p>
  4952. ````````````````````````````````
  4953. ```````````````````````````````` example
  4954. __foo__bar__baz__
  4955. .
  4956. <p><strong>foo__bar__baz</strong></p>
  4957. ````````````````````````````````
  4958. This is strong emphasis, even though the closing delimiter is
  4959. both left- and right-flanking, because it is followed by
  4960. punctuation:
  4961. ```````````````````````````````` example
  4962. __(bar)__.
  4963. .
  4964. <p><strong>(bar)</strong>.</p>
  4965. ````````````````````````````````
  4966. Rule 9:
  4967. Any nonempty sequence of inline elements can be the contents of an
  4968. emphasized span.
  4969. ```````````````````````````````` example
  4970. *foo [bar](/url)*
  4971. .
  4972. <p><em>foo <a href="/url">bar</a></em></p>
  4973. ````````````````````````````````
  4974. ```````````````````````````````` example
  4975. *foo
  4976. bar*
  4977. .
  4978. <p><em>foo
  4979. bar</em></p>
  4980. ````````````````````````````````
  4981. In particular, emphasis and strong emphasis can be nested
  4982. inside emphasis:
  4983. ```````````````````````````````` example
  4984. _foo __bar__ baz_
  4985. .
  4986. <p><em>foo <strong>bar</strong> baz</em></p>
  4987. ````````````````````````````````
  4988. ```````````````````````````````` example
  4989. _foo _bar_ baz_
  4990. .
  4991. <p><em>foo <em>bar</em> baz</em></p>
  4992. ````````````````````````````````
  4993. ```````````````````````````````` example
  4994. __foo_ bar_
  4995. .
  4996. <p><em><em>foo</em> bar</em></p>
  4997. ````````````````````````````````
  4998. ```````````````````````````````` example
  4999. *foo *bar**
  5000. .
  5001. <p><em>foo <em>bar</em></em></p>
  5002. ````````````````````````````````
  5003. ```````````````````````````````` example
  5004. *foo **bar** baz*
  5005. .
  5006. <p><em>foo <strong>bar</strong> baz</em></p>
  5007. ````````````````````````````````
  5008. But note:
  5009. ```````````````````````````````` example
  5010. *foo**bar**baz*
  5011. .
  5012. <p><em>foo</em><em>bar</em><em>baz</em></p>
  5013. ````````````````````````````````
  5014. The difference is that in the preceding case, the internal delimiters
  5015. [can close emphasis], while in the cases with spaces, they cannot.
  5016. ```````````````````````````````` example
  5017. ***foo** bar*
  5018. .
  5019. <p><em><strong>foo</strong> bar</em></p>
  5020. ````````````````````````````````
  5021. ```````````````````````````````` example
  5022. *foo **bar***
  5023. .
  5024. <p><em>foo <strong>bar</strong></em></p>
  5025. ````````````````````````````````
  5026. Note, however, that in the following case we get no strong
  5027. emphasis, because the opening delimiter is closed by the first
  5028. `*` before `bar`:
  5029. ```````````````````````````````` example
  5030. *foo**bar***
  5031. .
  5032. <p><em>foo</em><em>bar</em>**</p>
  5033. ````````````````````````````````
  5034. Indefinite levels of nesting are possible:
  5035. ```````````````````````````````` example
  5036. *foo **bar *baz* bim** bop*
  5037. .
  5038. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5039. ````````````````````````````````
  5040. ```````````````````````````````` example
  5041. *foo [*bar*](/url)*
  5042. .
  5043. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5044. ````````````````````````````````
  5045. There can be no empty emphasis or strong emphasis:
  5046. ```````````````````````````````` example
  5047. ** is not an empty emphasis
  5048. .
  5049. <p>** is not an empty emphasis</p>
  5050. ````````````````````````````````
  5051. ```````````````````````````````` example
  5052. **** is not an empty strong emphasis
  5053. .
  5054. <p>**** is not an empty strong emphasis</p>
  5055. ````````````````````````````````
  5056. Rule 10:
  5057. Any nonempty sequence of inline elements can be the contents of an
  5058. strongly emphasized span.
  5059. ```````````````````````````````` example
  5060. **foo [bar](/url)**
  5061. .
  5062. <p><strong>foo <a href="/url">bar</a></strong></p>
  5063. ````````````````````````````````
  5064. ```````````````````````````````` example
  5065. **foo
  5066. bar**
  5067. .
  5068. <p><strong>foo
  5069. bar</strong></p>
  5070. ````````````````````````````````
  5071. In particular, emphasis and strong emphasis can be nested
  5072. inside strong emphasis:
  5073. ```````````````````````````````` example
  5074. __foo _bar_ baz__
  5075. .
  5076. <p><strong>foo <em>bar</em> baz</strong></p>
  5077. ````````````````````````````````
  5078. ```````````````````````````````` example
  5079. __foo __bar__ baz__
  5080. .
  5081. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5082. ````````````````````````````````
  5083. ```````````````````````````````` example
  5084. ____foo__ bar__
  5085. .
  5086. <p><strong><strong>foo</strong> bar</strong></p>
  5087. ````````````````````````````````
  5088. ```````````````````````````````` example
  5089. **foo **bar****
  5090. .
  5091. <p><strong>foo <strong>bar</strong></strong></p>
  5092. ````````````````````````````````
  5093. ```````````````````````````````` example
  5094. **foo *bar* baz**
  5095. .
  5096. <p><strong>foo <em>bar</em> baz</strong></p>
  5097. ````````````````````````````````
  5098. But note:
  5099. ```````````````````````````````` example
  5100. **foo*bar*baz**
  5101. .
  5102. <p><em><em>foo</em>bar</em>baz**</p>
  5103. ````````````````````````````````
  5104. The difference is that in the preceding case, the internal delimiters
  5105. [can close emphasis], while in the cases with spaces, they cannot.
  5106. ```````````````````````````````` example
  5107. ***foo* bar**
  5108. .
  5109. <p><strong><em>foo</em> bar</strong></p>
  5110. ````````````````````````````````
  5111. ```````````````````````````````` example
  5112. **foo *bar***
  5113. .
  5114. <p><strong>foo <em>bar</em></strong></p>
  5115. ````````````````````````````````
  5116. Indefinite levels of nesting are possible:
  5117. ```````````````````````````````` example
  5118. **foo *bar **baz**
  5119. bim* bop**
  5120. .
  5121. <p><strong>foo <em>bar <strong>baz</strong>
  5122. bim</em> bop</strong></p>
  5123. ````````````````````````````````
  5124. ```````````````````````````````` example
  5125. **foo [*bar*](/url)**
  5126. .
  5127. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5128. ````````````````````````````````
  5129. There can be no empty emphasis or strong emphasis:
  5130. ```````````````````````````````` example
  5131. __ is not an empty emphasis
  5132. .
  5133. <p>__ is not an empty emphasis</p>
  5134. ````````````````````````````````
  5135. ```````````````````````````````` example
  5136. ____ is not an empty strong emphasis
  5137. .
  5138. <p>____ is not an empty strong emphasis</p>
  5139. ````````````````````````````````
  5140. Rule 11:
  5141. ```````````````````````````````` example
  5142. foo ***
  5143. .
  5144. <p>foo ***</p>
  5145. ````````````````````````````````
  5146. ```````````````````````````````` example
  5147. foo *\**
  5148. .
  5149. <p>foo <em>*</em></p>
  5150. ````````````````````````````````
  5151. ```````````````````````````````` example
  5152. foo *_*
  5153. .
  5154. <p>foo <em>_</em></p>
  5155. ````````````````````````````````
  5156. ```````````````````````````````` example
  5157. foo *****
  5158. .
  5159. <p>foo *****</p>
  5160. ````````````````````````````````
  5161. ```````````````````````````````` example
  5162. foo **\***
  5163. .
  5164. <p>foo <strong>*</strong></p>
  5165. ````````````````````````````````
  5166. ```````````````````````````````` example
  5167. foo **_**
  5168. .
  5169. <p>foo <strong>_</strong></p>
  5170. ````````````````````````````````
  5171. Note that when delimiters do not match evenly, Rule 11 determines
  5172. that the excess literal `*` characters will appear outside of the
  5173. emphasis, rather than inside it:
  5174. ```````````````````````````````` example
  5175. **foo*
  5176. .
  5177. <p>*<em>foo</em></p>
  5178. ````````````````````````````````
  5179. ```````````````````````````````` example
  5180. *foo**
  5181. .
  5182. <p><em>foo</em>*</p>
  5183. ````````````````````````````````
  5184. ```````````````````````````````` example
  5185. ***foo**
  5186. .
  5187. <p>*<strong>foo</strong></p>
  5188. ````````````````````````````````
  5189. ```````````````````````````````` example
  5190. ****foo*
  5191. .
  5192. <p>***<em>foo</em></p>
  5193. ````````````````````````````````
  5194. ```````````````````````````````` example
  5195. **foo***
  5196. .
  5197. <p><strong>foo</strong>*</p>
  5198. ````````````````````````````````
  5199. ```````````````````````````````` example
  5200. *foo****
  5201. .
  5202. <p><em>foo</em>***</p>
  5203. ````````````````````````````````
  5204. Rule 12:
  5205. ```````````````````````````````` example
  5206. foo ___
  5207. .
  5208. <p>foo ___</p>
  5209. ````````````````````````````````
  5210. ```````````````````````````````` example
  5211. foo _\__
  5212. .
  5213. <p>foo <em>_</em></p>
  5214. ````````````````````````````````
  5215. ```````````````````````````````` example
  5216. foo _*_
  5217. .
  5218. <p>foo <em>*</em></p>
  5219. ````````````````````````````````
  5220. ```````````````````````````````` example
  5221. foo _____
  5222. .
  5223. <p>foo _____</p>
  5224. ````````````````````````````````
  5225. ```````````````````````````````` example
  5226. foo __\___
  5227. .
  5228. <p>foo <strong>_</strong></p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. foo __*__
  5232. .
  5233. <p>foo <strong>*</strong></p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. __foo_
  5237. .
  5238. <p>_<em>foo</em></p>
  5239. ````````````````````````````````
  5240. Note that when delimiters do not match evenly, Rule 12 determines
  5241. that the excess literal `_` characters will appear outside of the
  5242. emphasis, rather than inside it:
  5243. ```````````````````````````````` example
  5244. _foo__
  5245. .
  5246. <p><em>foo</em>_</p>
  5247. ````````````````````````````````
  5248. ```````````````````````````````` example
  5249. ___foo__
  5250. .
  5251. <p>_<strong>foo</strong></p>
  5252. ````````````````````````````````
  5253. ```````````````````````````````` example
  5254. ____foo_
  5255. .
  5256. <p>___<em>foo</em></p>
  5257. ````````````````````````````````
  5258. ```````````````````````````````` example
  5259. __foo___
  5260. .
  5261. <p><strong>foo</strong>_</p>
  5262. ````````````````````````````````
  5263. ```````````````````````````````` example
  5264. _foo____
  5265. .
  5266. <p><em>foo</em>___</p>
  5267. ````````````````````````````````
  5268. Rule 13 implies that if you want emphasis nested directly inside
  5269. emphasis, you must use different delimiters:
  5270. ```````````````````````````````` example
  5271. **foo**
  5272. .
  5273. <p><strong>foo</strong></p>
  5274. ````````````````````````````````
  5275. ```````````````````````````````` example
  5276. *_foo_*
  5277. .
  5278. <p><em><em>foo</em></em></p>
  5279. ````````````````````````````````
  5280. ```````````````````````````````` example
  5281. __foo__
  5282. .
  5283. <p><strong>foo</strong></p>
  5284. ````````````````````````````````
  5285. ```````````````````````````````` example
  5286. _*foo*_
  5287. .
  5288. <p><em><em>foo</em></em></p>
  5289. ````````````````````````````````
  5290. However, strong emphasis within strong emphasis is possible without
  5291. switching delimiters:
  5292. ```````````````````````````````` example
  5293. ****foo****
  5294. .
  5295. <p><strong><strong>foo</strong></strong></p>
  5296. ````````````````````````````````
  5297. ```````````````````````````````` example
  5298. ____foo____
  5299. .
  5300. <p><strong><strong>foo</strong></strong></p>
  5301. ````````````````````````````````
  5302. Rule 13 can be applied to arbitrarily long sequences of
  5303. delimiters:
  5304. ```````````````````````````````` example
  5305. ******foo******
  5306. .
  5307. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5308. ````````````````````````````````
  5309. Rule 14:
  5310. ```````````````````````````````` example
  5311. ***foo***
  5312. .
  5313. <p><strong><em>foo</em></strong></p>
  5314. ````````````````````````````````
  5315. ```````````````````````````````` example
  5316. _____foo_____
  5317. .
  5318. <p><strong><strong><em>foo</em></strong></strong></p>
  5319. ````````````````````````````````
  5320. Rule 15:
  5321. ```````````````````````````````` example
  5322. *foo _bar* baz_
  5323. .
  5324. <p><em>foo _bar</em> baz_</p>
  5325. ````````````````````````````````
  5326. ```````````````````````````````` example
  5327. **foo*bar**
  5328. .
  5329. <p><em><em>foo</em>bar</em>*</p>
  5330. ````````````````````````````````
  5331. ```````````````````````````````` example
  5332. *foo __bar *baz bim__ bam*
  5333. .
  5334. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5335. ````````````````````````````````
  5336. Rule 16:
  5337. ```````````````````````````````` example
  5338. **foo **bar baz**
  5339. .
  5340. <p>**foo <strong>bar baz</strong></p>
  5341. ````````````````````````````````
  5342. ```````````````````````````````` example
  5343. *foo *bar baz*
  5344. .
  5345. <p>*foo <em>bar baz</em></p>
  5346. ````````````````````````````````
  5347. Rule 17:
  5348. ```````````````````````````````` example
  5349. *[bar*](/url)
  5350. .
  5351. <p>*<a href="/url">bar*</a></p>
  5352. ````````````````````````````````
  5353. ```````````````````````````````` example
  5354. _foo [bar_](/url)
  5355. .
  5356. <p>_foo <a href="/url">bar_</a></p>
  5357. ````````````````````````````````
  5358. ```````````````````````````````` example
  5359. *<img src="foo" title="*"/>
  5360. .
  5361. <p>*<img src="foo" title="*"/></p>
  5362. ````````````````````````````````
  5363. ```````````````````````````````` example
  5364. **<a href="**">
  5365. .
  5366. <p>**<a href="**"></p>
  5367. ````````````````````````````````
  5368. ```````````````````````````````` example
  5369. __<a href="__">
  5370. .
  5371. <p>__<a href="__"></p>
  5372. ````````````````````````````````
  5373. ```````````````````````````````` example
  5374. *a `*`*
  5375. .
  5376. <p><em>a <code>*</code></em></p>
  5377. ````````````````````````````````
  5378. ```````````````````````````````` example
  5379. _a `_`_
  5380. .
  5381. <p><em>a <code>_</code></em></p>
  5382. ````````````````````````````````
  5383. ```````````````````````````````` example
  5384. **a<http://foo.bar/?q=**>
  5385. .
  5386. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5387. ````````````````````````````````
  5388. ```````````````````````````````` example
  5389. __a<http://foo.bar/?q=__>
  5390. .
  5391. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5392. ````````````````````````````````
  5393. ## Links
  5394. A link contains [link text] (the visible text), a [link destination]
  5395. (the URI that is the link destination), and optionally a [link title].
  5396. There are two basic kinds of links in Markdown. In [inline links] the
  5397. destination and title are given immediately after the link text. In
  5398. [reference links] the destination and title are defined elsewhere in
  5399. the document.
  5400. A [link text](@) consists of a sequence of zero or more
  5401. inline elements enclosed by square brackets (`[` and `]`). The
  5402. following rules apply:
  5403. - Links may not contain other links, at any level of nesting. If
  5404. multiple otherwise valid link definitions appear nested inside each
  5405. other, the inner-most definition is used.
  5406. - Brackets are allowed in the [link text] only if (a) they
  5407. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5408. with an open bracket `[`, a sequence of zero or more inlines, and
  5409. a close bracket `]`.
  5410. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5411. than the brackets in link text. Thus, for example,
  5412. `` [foo`]` `` could not be a link text, since the second `]`
  5413. is part of a code span.
  5414. - The brackets in link text bind more tightly than markers for
  5415. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5416. A [link destination](@) consists of either
  5417. - a sequence of zero or more characters between an opening `<` and a
  5418. closing `>` that contains no spaces, line breaks, or unescaped
  5419. `<` or `>` characters, or
  5420. - a nonempty sequence of characters that does not include
  5421. ASCII space or control characters, and includes parentheses
  5422. only if (a) they are backslash-escaped or (b) they are part of
  5423. a balanced pair of unescaped parentheses that is not itself
  5424. inside a balanced pair of unescaped parentheses.
  5425. A [link title](@) consists of either
  5426. - a sequence of zero or more characters between straight double-quote
  5427. characters (`"`), including a `"` character only if it is
  5428. backslash-escaped, or
  5429. - a sequence of zero or more characters between straight single-quote
  5430. characters (`'`), including a `'` character only if it is
  5431. backslash-escaped, or
  5432. - a sequence of zero or more characters between matching parentheses
  5433. (`(...)`), including a `)` character only if it is backslash-escaped.
  5434. Although [link titles] may span multiple lines, they may not contain
  5435. a [blank line].
  5436. An [inline link](@) consists of a [link text] followed immediately
  5437. by a left parenthesis `(`, optional [whitespace], an optional
  5438. [link destination], an optional [link title] separated from the link
  5439. destination by [whitespace], optional [whitespace], and a right
  5440. parenthesis `)`. The link's text consists of the inlines contained
  5441. in the [link text] (excluding the enclosing square brackets).
  5442. The link's URI consists of the link destination, excluding enclosing
  5443. `<...>` if present, with backslash-escapes in effect as described
  5444. above. The link's title consists of the link title, excluding its
  5445. enclosing delimiters, with backslash-escapes in effect as described
  5446. above.
  5447. Here is a simple inline link:
  5448. ```````````````````````````````` example
  5449. [link](/uri "title")
  5450. .
  5451. <p><a href="/uri" title="title">link</a></p>
  5452. ````````````````````````````````
  5453. The title may be omitted:
  5454. ```````````````````````````````` example
  5455. [link](/uri)
  5456. .
  5457. <p><a href="/uri">link</a></p>
  5458. ````````````````````````````````
  5459. Both the title and the destination may be omitted:
  5460. ```````````````````````````````` example
  5461. [link]()
  5462. .
  5463. <p><a href="">link</a></p>
  5464. ````````````````````````````````
  5465. ```````````````````````````````` example
  5466. [link](<>)
  5467. .
  5468. <p><a href="">link</a></p>
  5469. ````````````````````````````````
  5470. The destination cannot contain spaces or line breaks,
  5471. even if enclosed in pointy brackets:
  5472. ```````````````````````````````` example
  5473. [link](/my uri)
  5474. .
  5475. <p>[link](/my uri)</p>
  5476. ````````````````````````````````
  5477. ```````````````````````````````` example
  5478. [link](</my uri>)
  5479. .
  5480. <p>[link](&lt;/my uri&gt;)</p>
  5481. ````````````````````````````````
  5482. ```````````````````````````````` example
  5483. [link](foo
  5484. bar)
  5485. .
  5486. <p>[link](foo
  5487. bar)</p>
  5488. ````````````````````````````````
  5489. ```````````````````````````````` example
  5490. [link](<foo
  5491. bar>)
  5492. .
  5493. <p>[link](<foo
  5494. bar>)</p>
  5495. ````````````````````````````````
  5496. Parentheses inside the link destination may be escaped:
  5497. ```````````````````````````````` example
  5498. [link](\(foo\))
  5499. .
  5500. <p><a href="(foo)">link</a></p>
  5501. ````````````````````````````````
  5502. One level of balanced parentheses is allowed without escaping:
  5503. ```````````````````````````````` example
  5504. [link]((foo)and(bar))
  5505. .
  5506. <p><a href="(foo)and(bar)">link</a></p>
  5507. ````````````````````````````````
  5508. However, if you have parentheses within parentheses, you need to escape
  5509. or use the `<...>` form:
  5510. ```````````````````````````````` example
  5511. [link](foo(and(bar)))
  5512. .
  5513. <p>[link](foo(and(bar)))</p>
  5514. ````````````````````````````````
  5515. ```````````````````````````````` example
  5516. [link](foo(and\(bar\)))
  5517. .
  5518. <p><a href="foo(and(bar))">link</a></p>
  5519. ````````````````````````````````
  5520. ```````````````````````````````` example
  5521. [link](<foo(and(bar))>)
  5522. .
  5523. <p><a href="foo(and(bar))">link</a></p>
  5524. ````````````````````````````````
  5525. Parentheses and other symbols can also be escaped, as usual
  5526. in Markdown:
  5527. ```````````````````````````````` example
  5528. [link](foo\)\:)
  5529. .
  5530. <p><a href="foo):">link</a></p>
  5531. ````````````````````````````````
  5532. A link can contain fragment identifiers and queries:
  5533. ```````````````````````````````` example
  5534. [link](#fragment)
  5535. [link](http://example.com#fragment)
  5536. [link](http://example.com?foo=3#frag)
  5537. .
  5538. <p><a href="#fragment">link</a></p>
  5539. <p><a href="http://example.com#fragment">link</a></p>
  5540. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5541. ````````````````````````````````
  5542. Note that a backslash before a non-escapable character is
  5543. just a backslash:
  5544. ```````````````````````````````` example
  5545. [link](foo\bar)
  5546. .
  5547. <p><a href="foo%5Cbar">link</a></p>
  5548. ````````````````````````````````
  5549. URL-escaping should be left alone inside the destination, as all
  5550. URL-escaped characters are also valid URL characters. Entity and
  5551. numerical character references in the destination will be parsed
  5552. into the corresponding Unicode code points, as usual. These may
  5553. be optionally URL-escaped when written as HTML, but this spec
  5554. does not enforce any particular policy for rendering URLs in
  5555. HTML or other formats. Renderers may make different decisions
  5556. about how to escape or normalize URLs in the output.
  5557. ```````````````````````````````` example
  5558. [link](foo%20b&auml;)
  5559. .
  5560. <p><a href="foo%20b%C3%A4">link</a></p>
  5561. ````````````````````````````````
  5562. Note that, because titles can often be parsed as destinations,
  5563. if you try to omit the destination and keep the title, you'll
  5564. get unexpected results:
  5565. ```````````````````````````````` example
  5566. [link]("title")
  5567. .
  5568. <p><a href="%22title%22">link</a></p>
  5569. ````````````````````````````````
  5570. Titles may be in single quotes, double quotes, or parentheses:
  5571. ```````````````````````````````` example
  5572. [link](/url "title")
  5573. [link](/url 'title')
  5574. [link](/url (title))
  5575. .
  5576. <p><a href="/url" title="title">link</a>
  5577. <a href="/url" title="title">link</a>
  5578. <a href="/url" title="title">link</a></p>
  5579. ````````````````````````````````
  5580. Backslash escapes and entity and numeric character references
  5581. may be used in titles:
  5582. ```````````````````````````````` example
  5583. [link](/url "title \"&quot;")
  5584. .
  5585. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5586. ````````````````````````````````
  5587. Nested balanced quotes are not allowed without escaping:
  5588. ```````````````````````````````` example
  5589. [link](/url "title "and" title")
  5590. .
  5591. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5592. ````````````````````````````````
  5593. But it is easy to work around this by using a different quote type:
  5594. ```````````````````````````````` example
  5595. [link](/url 'title "and" title')
  5596. .
  5597. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5598. ````````````````````````````````
  5599. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5600. title, and its test suite included a test demonstrating this.
  5601. But it is hard to see a good rationale for the extra complexity this
  5602. brings, since there are already many ways---backslash escaping,
  5603. entity and numeric character references, or using a different
  5604. quote type for the enclosing title---to write titles containing
  5605. double quotes. `Markdown.pl`'s handling of titles has a number
  5606. of other strange features. For example, it allows single-quoted
  5607. titles in inline links, but not reference links. And, in
  5608. reference links but not inline links, it allows a title to begin
  5609. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5610. titles with no closing quotation mark, though 1.0.2b8 does not.
  5611. It seems preferable to adopt a simple, rational rule that works
  5612. the same way in inline links and link reference definitions.)
  5613. [Whitespace] is allowed around the destination and title:
  5614. ```````````````````````````````` example
  5615. [link]( /uri
  5616. "title" )
  5617. .
  5618. <p><a href="/uri" title="title">link</a></p>
  5619. ````````````````````````````````
  5620. But it is not allowed between the link text and the
  5621. following parenthesis:
  5622. ```````````````````````````````` example
  5623. [link] (/uri)
  5624. .
  5625. <p>[link] (/uri)</p>
  5626. ````````````````````````````````
  5627. The link text may contain balanced brackets, but not unbalanced ones,
  5628. unless they are escaped:
  5629. ```````````````````````````````` example
  5630. [link [foo [bar]]](/uri)
  5631. .
  5632. <p><a href="/uri">link [foo [bar]]</a></p>
  5633. ````````````````````````````````
  5634. ```````````````````````````````` example
  5635. [link] bar](/uri)
  5636. .
  5637. <p>[link] bar](/uri)</p>
  5638. ````````````````````````````````
  5639. ```````````````````````````````` example
  5640. [link [bar](/uri)
  5641. .
  5642. <p>[link <a href="/uri">bar</a></p>
  5643. ````````````````````````````````
  5644. ```````````````````````````````` example
  5645. [link \[bar](/uri)
  5646. .
  5647. <p><a href="/uri">link [bar</a></p>
  5648. ````````````````````````````````
  5649. The link text may contain inline content:
  5650. ```````````````````````````````` example
  5651. [link *foo **bar** `#`*](/uri)
  5652. .
  5653. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5654. ````````````````````````````````
  5655. ```````````````````````````````` example
  5656. [![moon](moon.jpg)](/uri)
  5657. .
  5658. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5659. ````````````````````````````````
  5660. However, links may not contain other links, at any level of nesting.
  5661. ```````````````````````````````` example
  5662. [foo [bar](/uri)](/uri)
  5663. .
  5664. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5665. ````````````````````````````````
  5666. ```````````````````````````````` example
  5667. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5668. .
  5669. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5670. ````````````````````````````````
  5671. ```````````````````````````````` example
  5672. ![[[foo](uri1)](uri2)](uri3)
  5673. .
  5674. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5675. ````````````````````````````````
  5676. These cases illustrate the precedence of link text grouping over
  5677. emphasis grouping:
  5678. ```````````````````````````````` example
  5679. *[foo*](/uri)
  5680. .
  5681. <p>*<a href="/uri">foo*</a></p>
  5682. ````````````````````````````````
  5683. ```````````````````````````````` example
  5684. [foo *bar](baz*)
  5685. .
  5686. <p><a href="baz*">foo *bar</a></p>
  5687. ````````````````````````````````
  5688. Note that brackets that *aren't* part of links do not take
  5689. precedence:
  5690. ```````````````````````````````` example
  5691. *foo [bar* baz]
  5692. .
  5693. <p><em>foo [bar</em> baz]</p>
  5694. ````````````````````````````````
  5695. These cases illustrate the precedence of HTML tags, code spans,
  5696. and autolinks over link grouping:
  5697. ```````````````````````````````` example
  5698. [foo <bar attr="](baz)">
  5699. .
  5700. <p>[foo <bar attr="](baz)"></p>
  5701. ````````````````````````````````
  5702. ```````````````````````````````` example
  5703. [foo`](/uri)`
  5704. .
  5705. <p>[foo<code>](/uri)</code></p>
  5706. ````````````````````````````````
  5707. ```````````````````````````````` example
  5708. [foo<http://example.com/?search=](uri)>
  5709. .
  5710. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5711. ````````````````````````````````
  5712. There are three kinds of [reference link](@)s:
  5713. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5714. and [shortcut](#shortcut-reference-link).
  5715. A [full reference link](@)
  5716. consists of a [link text] immediately followed by a [link label]
  5717. that [matches] a [link reference definition] elsewhere in the document.
  5718. A [link label](@) begins with a left bracket (`[`) and ends
  5719. with the first right bracket (`]`) that is not backslash-escaped.
  5720. Between these brackets there must be at least one [non-whitespace character].
  5721. Unescaped square bracket characters are not allowed in
  5722. [link labels]. A link label can have at most 999
  5723. characters inside the square brackets.
  5724. One label [matches](@)
  5725. another just in case their normalized forms are equal. To normalize a
  5726. label, perform the *Unicode case fold* and collapse consecutive internal
  5727. [whitespace] to a single space. If there are multiple
  5728. matching reference link definitions, the one that comes first in the
  5729. document is used. (It is desirable in such cases to emit a warning.)
  5730. The contents of the first link label are parsed as inlines, which are
  5731. used as the link's text. The link's URI and title are provided by the
  5732. matching [link reference definition].
  5733. Here is a simple example:
  5734. ```````````````````````````````` example
  5735. [foo][bar]
  5736. [bar]: /url "title"
  5737. .
  5738. <p><a href="/url" title="title">foo</a></p>
  5739. ````````````````````````````````
  5740. The rules for the [link text] are the same as with
  5741. [inline links]. Thus:
  5742. The link text may contain balanced brackets, but not unbalanced ones,
  5743. unless they are escaped:
  5744. ```````````````````````````````` example
  5745. [link [foo [bar]]][ref]
  5746. [ref]: /uri
  5747. .
  5748. <p><a href="/uri">link [foo [bar]]</a></p>
  5749. ````````````````````````````````
  5750. ```````````````````````````````` example
  5751. [link \[bar][ref]
  5752. [ref]: /uri
  5753. .
  5754. <p><a href="/uri">link [bar</a></p>
  5755. ````````````````````````````````
  5756. The link text may contain inline content:
  5757. ```````````````````````````````` example
  5758. [link *foo **bar** `#`*][ref]
  5759. [ref]: /uri
  5760. .
  5761. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5762. ````````````````````````````````
  5763. ```````````````````````````````` example
  5764. [![moon](moon.jpg)][ref]
  5765. [ref]: /uri
  5766. .
  5767. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5768. ````````````````````````````````
  5769. However, links may not contain other links, at any level of nesting.
  5770. ```````````````````````````````` example
  5771. [foo [bar](/uri)][ref]
  5772. [ref]: /uri
  5773. .
  5774. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5775. ````````````````````````````````
  5776. ```````````````````````````````` example
  5777. [foo *bar [baz][ref]*][ref]
  5778. [ref]: /uri
  5779. .
  5780. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5781. ````````````````````````````````
  5782. (In the examples above, we have two [shortcut reference links]
  5783. instead of one [full reference link].)
  5784. The following cases illustrate the precedence of link text grouping over
  5785. emphasis grouping:
  5786. ```````````````````````````````` example
  5787. *[foo*][ref]
  5788. [ref]: /uri
  5789. .
  5790. <p>*<a href="/uri">foo*</a></p>
  5791. ````````````````````````````````
  5792. ```````````````````````````````` example
  5793. [foo *bar][ref]
  5794. [ref]: /uri
  5795. .
  5796. <p><a href="/uri">foo *bar</a></p>
  5797. ````````````````````````````````
  5798. These cases illustrate the precedence of HTML tags, code spans,
  5799. and autolinks over link grouping:
  5800. ```````````````````````````````` example
  5801. [foo <bar attr="][ref]">
  5802. [ref]: /uri
  5803. .
  5804. <p>[foo <bar attr="][ref]"></p>
  5805. ````````````````````````````````
  5806. ```````````````````````````````` example
  5807. [foo`][ref]`
  5808. [ref]: /uri
  5809. .
  5810. <p>[foo<code>][ref]</code></p>
  5811. ````````````````````````````````
  5812. ```````````````````````````````` example
  5813. [foo<http://example.com/?search=][ref]>
  5814. [ref]: /uri
  5815. .
  5816. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5817. ````````````````````````````````
  5818. Matching is case-insensitive:
  5819. ```````````````````````````````` example
  5820. [foo][BaR]
  5821. [bar]: /url "title"
  5822. .
  5823. <p><a href="/url" title="title">foo</a></p>
  5824. ````````````````````````````````
  5825. Unicode case fold is used:
  5826. ```````````````````````````````` example
  5827. [Толпой][Толпой] is a Russian word.
  5828. [ТОЛПОЙ]: /url
  5829. .
  5830. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5831. ````````````````````````````````
  5832. Consecutive internal [whitespace] is treated as one space for
  5833. purposes of determining matching:
  5834. ```````````````````````````````` example
  5835. [Foo
  5836. bar]: /url
  5837. [Baz][Foo bar]
  5838. .
  5839. <p><a href="/url">Baz</a></p>
  5840. ````````````````````````````````
  5841. No [whitespace] is allowed between the [link text] and the
  5842. [link label]:
  5843. ```````````````````````````````` example
  5844. [foo] [bar]
  5845. [bar]: /url "title"
  5846. .
  5847. <p>[foo] <a href="/url" title="title">bar</a></p>
  5848. ````````````````````````````````
  5849. ```````````````````````````````` example
  5850. [foo]
  5851. [bar]
  5852. [bar]: /url "title"
  5853. .
  5854. <p>[foo]
  5855. <a href="/url" title="title">bar</a></p>
  5856. ````````````````````````````````
  5857. This is a departure from John Gruber's original Markdown syntax
  5858. description, which explicitly allows whitespace between the link
  5859. text and the link label. It brings reference links in line with
  5860. [inline links], which (according to both original Markdown and
  5861. this spec) cannot have whitespace after the link text. More
  5862. importantly, it prevents inadvertent capture of consecutive
  5863. [shortcut reference links]. If whitespace is allowed between the
  5864. link text and the link label, then in the following we will have
  5865. a single reference link, not two shortcut reference links, as
  5866. intended:
  5867. ``` markdown
  5868. [foo]
  5869. [bar]
  5870. [foo]: /url1
  5871. [bar]: /url2
  5872. ```
  5873. (Note that [shortcut reference links] were introduced by Gruber
  5874. himself in a beta version of `Markdown.pl`, but never included
  5875. in the official syntax description. Without shortcut reference
  5876. links, it is harmless to allow space between the link text and
  5877. link label; but once shortcut references are introduced, it is
  5878. too dangerous to allow this, as it frequently leads to
  5879. unintended results.)
  5880. When there are multiple matching [link reference definitions],
  5881. the first is used:
  5882. ```````````````````````````````` example
  5883. [foo]: /url1
  5884. [foo]: /url2
  5885. [bar][foo]
  5886. .
  5887. <p><a href="/url1">bar</a></p>
  5888. ````````````````````````````````
  5889. Note that matching is performed on normalized strings, not parsed
  5890. inline content. So the following does not match, even though the
  5891. labels define equivalent inline content:
  5892. ```````````````````````````````` example
  5893. [bar][foo\!]
  5894. [foo!]: /url
  5895. .
  5896. <p>[bar][foo!]</p>
  5897. ````````````````````````````````
  5898. [Link labels] cannot contain brackets, unless they are
  5899. backslash-escaped:
  5900. ```````````````````````````````` example
  5901. [foo][ref[]
  5902. [ref[]: /uri
  5903. .
  5904. <p>[foo][ref[]</p>
  5905. <p>[ref[]: /uri</p>
  5906. ````````````````````````````````
  5907. ```````````````````````````````` example
  5908. [foo][ref[bar]]
  5909. [ref[bar]]: /uri
  5910. .
  5911. <p>[foo][ref[bar]]</p>
  5912. <p>[ref[bar]]: /uri</p>
  5913. ````````````````````````````````
  5914. ```````````````````````````````` example
  5915. [[[foo]]]
  5916. [[[foo]]]: /url
  5917. .
  5918. <p>[[[foo]]]</p>
  5919. <p>[[[foo]]]: /url</p>
  5920. ````````````````````````````````
  5921. ```````````````````````````````` example
  5922. [foo][ref\[]
  5923. [ref\[]: /uri
  5924. .
  5925. <p><a href="/uri">foo</a></p>
  5926. ````````````````````````````````
  5927. Note that in this example `]` is not backslash-escaped:
  5928. ```````````````````````````````` example
  5929. [bar\\]: /uri
  5930. [bar\\]
  5931. .
  5932. <p><a href="/uri">bar\</a></p>
  5933. ````````````````````````````````
  5934. A [link label] must contain at least one [non-whitespace character]:
  5935. ```````````````````````````````` example
  5936. []
  5937. []: /uri
  5938. .
  5939. <p>[]</p>
  5940. <p>[]: /uri</p>
  5941. ````````````````````````````````
  5942. ```````````````````````````````` example
  5943. [
  5944. ]
  5945. [
  5946. ]: /uri
  5947. .
  5948. <p>[
  5949. ]</p>
  5950. <p>[
  5951. ]: /uri</p>
  5952. ````````````````````````````````
  5953. A [collapsed reference link](@)
  5954. consists of a [link label] that [matches] a
  5955. [link reference definition] elsewhere in the
  5956. document, followed by the string `[]`.
  5957. The contents of the first link label are parsed as inlines,
  5958. which are used as the link's text. The link's URI and title are
  5959. provided by the matching reference link definition. Thus,
  5960. `[foo][]` is equivalent to `[foo][foo]`.
  5961. ```````````````````````````````` example
  5962. [foo][]
  5963. [foo]: /url "title"
  5964. .
  5965. <p><a href="/url" title="title">foo</a></p>
  5966. ````````````````````````````````
  5967. ```````````````````````````````` example
  5968. [*foo* bar][]
  5969. [*foo* bar]: /url "title"
  5970. .
  5971. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5972. ````````````````````````````````
  5973. The link labels are case-insensitive:
  5974. ```````````````````````````````` example
  5975. [Foo][]
  5976. [foo]: /url "title"
  5977. .
  5978. <p><a href="/url" title="title">Foo</a></p>
  5979. ````````````````````````````````
  5980. As with full reference links, [whitespace] is not
  5981. allowed between the two sets of brackets:
  5982. ```````````````````````````````` example
  5983. [foo]
  5984. []
  5985. [foo]: /url "title"
  5986. .
  5987. <p><a href="/url" title="title">foo</a>
  5988. []</p>
  5989. ````````````````````````````````
  5990. A [shortcut reference link](@)
  5991. consists of a [link label] that [matches] a
  5992. [link reference definition] elsewhere in the
  5993. document and is not followed by `[]` or a link label.
  5994. The contents of the first link label are parsed as inlines,
  5995. which are used as the link's text. the link's URI and title
  5996. are provided by the matching link reference definition.
  5997. Thus, `[foo]` is equivalent to `[foo][]`.
  5998. ```````````````````````````````` example
  5999. [foo]
  6000. [foo]: /url "title"
  6001. .
  6002. <p><a href="/url" title="title">foo</a></p>
  6003. ````````````````````````````````
  6004. ```````````````````````````````` example
  6005. [*foo* bar]
  6006. [*foo* bar]: /url "title"
  6007. .
  6008. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6009. ````````````````````````````````
  6010. ```````````````````````````````` example
  6011. [[*foo* bar]]
  6012. [*foo* bar]: /url "title"
  6013. .
  6014. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6015. ````````````````````````````````
  6016. ```````````````````````````````` example
  6017. [[bar [foo]
  6018. [foo]: /url
  6019. .
  6020. <p>[[bar <a href="/url">foo</a></p>
  6021. ````````````````````````````````
  6022. The link labels are case-insensitive:
  6023. ```````````````````````````````` example
  6024. [Foo]
  6025. [foo]: /url "title"
  6026. .
  6027. <p><a href="/url" title="title">Foo</a></p>
  6028. ````````````````````````````````
  6029. A space after the link text should be preserved:
  6030. ```````````````````````````````` example
  6031. [foo] bar
  6032. [foo]: /url
  6033. .
  6034. <p><a href="/url">foo</a> bar</p>
  6035. ````````````````````````````````
  6036. If you just want bracketed text, you can backslash-escape the
  6037. opening bracket to avoid links:
  6038. ```````````````````````````````` example
  6039. \[foo]
  6040. [foo]: /url "title"
  6041. .
  6042. <p>[foo]</p>
  6043. ````````````````````````````````
  6044. Note that this is a link, because a link label ends with the first
  6045. following closing bracket:
  6046. ```````````````````````````````` example
  6047. [foo*]: /url
  6048. *[foo*]
  6049. .
  6050. <p>*<a href="/url">foo*</a></p>
  6051. ````````````````````````````````
  6052. Full references take precedence over shortcut references:
  6053. ```````````````````````````````` example
  6054. [foo][bar]
  6055. [foo]: /url1
  6056. [bar]: /url2
  6057. .
  6058. <p><a href="/url2">foo</a></p>
  6059. ````````````````````````````````
  6060. In the following case `[bar][baz]` is parsed as a reference,
  6061. `[foo]` as normal text:
  6062. ```````````````````````````````` example
  6063. [foo][bar][baz]
  6064. [baz]: /url
  6065. .
  6066. <p>[foo]<a href="/url">bar</a></p>
  6067. ````````````````````````````````
  6068. Here, though, `[foo][bar]` is parsed as a reference, since
  6069. `[bar]` is defined:
  6070. ```````````````````````````````` example
  6071. [foo][bar][baz]
  6072. [baz]: /url1
  6073. [bar]: /url2
  6074. .
  6075. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6076. ````````````````````````````````
  6077. Here `[foo]` is not parsed as a shortcut reference, because it
  6078. is followed by a link label (even though `[bar]` is not defined):
  6079. ```````````````````````````````` example
  6080. [foo][bar][baz]
  6081. [baz]: /url1
  6082. [foo]: /url2
  6083. .
  6084. <p>[foo]<a href="/url1">bar</a></p>
  6085. ````````````````````````````````
  6086. ## Images
  6087. Syntax for images is like the syntax for links, with one
  6088. difference. Instead of [link text], we have an
  6089. [image description](@). The rules for this are the
  6090. same as for [link text], except that (a) an
  6091. image description starts with `![` rather than `[`, and
  6092. (b) an image description may contain links.
  6093. An image description has inline elements
  6094. as its contents. When an image is rendered to HTML,
  6095. this is standardly used as the image's `alt` attribute.
  6096. ```````````````````````````````` example
  6097. ![foo](/url "title")
  6098. .
  6099. <p><img src="/url" alt="foo" title="title" /></p>
  6100. ````````````````````````````````
  6101. ```````````````````````````````` example
  6102. ![foo *bar*]
  6103. [foo *bar*]: train.jpg "train & tracks"
  6104. .
  6105. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6106. ````````````````````````````````
  6107. ```````````````````````````````` example
  6108. ![foo ![bar](/url)](/url2)
  6109. .
  6110. <p><img src="/url2" alt="foo bar" /></p>
  6111. ````````````````````````````````
  6112. ```````````````````````````````` example
  6113. ![foo [bar](/url)](/url2)
  6114. .
  6115. <p><img src="/url2" alt="foo bar" /></p>
  6116. ````````````````````````````````
  6117. Though this spec is concerned with parsing, not rendering, it is
  6118. recommended that in rendering to HTML, only the plain string content
  6119. of the [image description] be used. Note that in
  6120. the above example, the alt attribute's value is `foo bar`, not `foo
  6121. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6122. content is rendered, without formatting.
  6123. ```````````````````````````````` example
  6124. ![foo *bar*][]
  6125. [foo *bar*]: train.jpg "train & tracks"
  6126. .
  6127. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6128. ````````````````````````````````
  6129. ```````````````````````````````` example
  6130. ![foo *bar*][foobar]
  6131. [FOOBAR]: train.jpg "train & tracks"
  6132. .
  6133. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6134. ````````````````````````````````
  6135. ```````````````````````````````` example
  6136. ![foo](train.jpg)
  6137. .
  6138. <p><img src="train.jpg" alt="foo" /></p>
  6139. ````````````````````````````````
  6140. ```````````````````````````````` example
  6141. My ![foo bar](/path/to/train.jpg "title" )
  6142. .
  6143. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6144. ````````````````````````````````
  6145. ```````````````````````````````` example
  6146. ![foo](<url>)
  6147. .
  6148. <p><img src="url" alt="foo" /></p>
  6149. ````````````````````````````````
  6150. ```````````````````````````````` example
  6151. ![](/url)
  6152. .
  6153. <p><img src="/url" alt="" /></p>
  6154. ````````````````````````````````
  6155. Reference-style:
  6156. ```````````````````````````````` example
  6157. ![foo][bar]
  6158. [bar]: /url
  6159. .
  6160. <p><img src="/url" alt="foo" /></p>
  6161. ````````````````````````````````
  6162. ```````````````````````````````` example
  6163. ![foo][bar]
  6164. [BAR]: /url
  6165. .
  6166. <p><img src="/url" alt="foo" /></p>
  6167. ````````````````````````````````
  6168. Collapsed:
  6169. ```````````````````````````````` example
  6170. ![foo][]
  6171. [foo]: /url "title"
  6172. .
  6173. <p><img src="/url" alt="foo" title="title" /></p>
  6174. ````````````````````````````````
  6175. ```````````````````````````````` example
  6176. ![*foo* bar][]
  6177. [*foo* bar]: /url "title"
  6178. .
  6179. <p><img src="/url" alt="foo bar" title="title" /></p>
  6180. ````````````````````````````````
  6181. The labels are case-insensitive:
  6182. ```````````````````````````````` example
  6183. ![Foo][]
  6184. [foo]: /url "title"
  6185. .
  6186. <p><img src="/url" alt="Foo" title="title" /></p>
  6187. ````````````````````````````````
  6188. As with reference links, [whitespace] is not allowed
  6189. between the two sets of brackets:
  6190. ```````````````````````````````` example
  6191. ![foo]
  6192. []
  6193. [foo]: /url "title"
  6194. .
  6195. <p><img src="/url" alt="foo" title="title" />
  6196. []</p>
  6197. ````````````````````````````````
  6198. Shortcut:
  6199. ```````````````````````````````` example
  6200. ![foo]
  6201. [foo]: /url "title"
  6202. .
  6203. <p><img src="/url" alt="foo" title="title" /></p>
  6204. ````````````````````````````````
  6205. ```````````````````````````````` example
  6206. ![*foo* bar]
  6207. [*foo* bar]: /url "title"
  6208. .
  6209. <p><img src="/url" alt="foo bar" title="title" /></p>
  6210. ````````````````````````````````
  6211. Note that link labels cannot contain unescaped brackets:
  6212. ```````````````````````````````` example
  6213. ![[foo]]
  6214. [[foo]]: /url "title"
  6215. .
  6216. <p>![[foo]]</p>
  6217. <p>[[foo]]: /url &quot;title&quot;</p>
  6218. ````````````````````````````````
  6219. The link labels are case-insensitive:
  6220. ```````````````````````````````` example
  6221. ![Foo]
  6222. [foo]: /url "title"
  6223. .
  6224. <p><img src="/url" alt="Foo" title="title" /></p>
  6225. ````````````````````````````````
  6226. If you just want bracketed text, you can backslash-escape the
  6227. opening `!` and `[`:
  6228. ```````````````````````````````` example
  6229. \!\[foo]
  6230. [foo]: /url "title"
  6231. .
  6232. <p>![foo]</p>
  6233. ````````````````````````````````
  6234. If you want a link after a literal `!`, backslash-escape the
  6235. `!`:
  6236. ```````````````````````````````` example
  6237. \![foo]
  6238. [foo]: /url "title"
  6239. .
  6240. <p>!<a href="/url" title="title">foo</a></p>
  6241. ````````````````````````````````
  6242. ## Autolinks
  6243. [Autolink](@)s are absolute URIs and email addresses inside
  6244. `<` and `>`. They are parsed as links, with the URL or email address
  6245. as the link label.
  6246. A [URI autolink](@) consists of `<`, followed by an
  6247. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6248. a link to the URI, with the URI as the link's label.
  6249. An [absolute URI](@),
  6250. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6251. followed by zero or more characters other than ASCII
  6252. [whitespace] and control characters, `<`, and `>`. If
  6253. the URI includes these characters, they must be percent-encoded
  6254. (e.g. `%20` for a space).
  6255. For purposes of this spec, a [scheme](@) is any sequence
  6256. of 2--32 characters beginning with an ASCII letter and followed
  6257. by any combination of ASCII letters, digits, or the symbols plus
  6258. ("+"), period ("."), or hyphen ("-").
  6259. Here are some valid autolinks:
  6260. ```````````````````````````````` example
  6261. <http://foo.bar.baz>
  6262. .
  6263. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6264. ````````````````````````````````
  6265. ```````````````````````````````` example
  6266. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6267. .
  6268. <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>
  6269. ````````````````````````````````
  6270. ```````````````````````````````` example
  6271. <irc://foo.bar:2233/baz>
  6272. .
  6273. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6274. ````````````````````````````````
  6275. Uppercase is also fine:
  6276. ```````````````````````````````` example
  6277. <MAILTO:FOO@BAR.BAZ>
  6278. .
  6279. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6280. ````````````````````````````````
  6281. Note that many strings that count as [absolute URIs] for
  6282. purposes of this spec are not valid URIs, because their
  6283. schemes are not registered or because of other problems
  6284. with their syntax:
  6285. ```````````````````````````````` example
  6286. <a+b+c:d>
  6287. .
  6288. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6289. ````````````````````````````````
  6290. ```````````````````````````````` example
  6291. <made-up-scheme://foo,bar>
  6292. .
  6293. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6294. ````````````````````````````````
  6295. ```````````````````````````````` example
  6296. <http://../>
  6297. .
  6298. <p><a href="http://../">http://../</a></p>
  6299. ````````````````````````````````
  6300. ```````````````````````````````` example
  6301. <localhost:5001/foo>
  6302. .
  6303. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6304. ````````````````````````````````
  6305. Spaces are not allowed in autolinks:
  6306. ```````````````````````````````` example
  6307. <http://foo.bar/baz bim>
  6308. .
  6309. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6310. ````````````````````````````````
  6311. Backslash-escapes do not work inside autolinks:
  6312. ```````````````````````````````` example
  6313. <http://example.com/\[\>
  6314. .
  6315. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6316. ````````````````````````````````
  6317. An [email autolink](@)
  6318. consists of `<`, followed by an [email address],
  6319. followed by `>`. The link's label is the email address,
  6320. and the URL is `mailto:` followed by the email address.
  6321. An [email address](@),
  6322. for these purposes, is anything that matches
  6323. the [non-normative regex from the HTML5
  6324. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6325. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6326. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6327. Examples of email autolinks:
  6328. ```````````````````````````````` example
  6329. <foo@bar.example.com>
  6330. .
  6331. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6332. ````````````````````````````````
  6333. ```````````````````````````````` example
  6334. <foo+special@Bar.baz-bar0.com>
  6335. .
  6336. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6337. ````````````````````````````````
  6338. Backslash-escapes do not work inside email autolinks:
  6339. ```````````````````````````````` example
  6340. <foo\+@bar.example.com>
  6341. .
  6342. <p>&lt;foo+@bar.example.com&gt;</p>
  6343. ````````````````````````````````
  6344. These are not autolinks:
  6345. ```````````````````````````````` example
  6346. <>
  6347. .
  6348. <p>&lt;&gt;</p>
  6349. ````````````````````````````````
  6350. ```````````````````````````````` example
  6351. < http://foo.bar >
  6352. .
  6353. <p>&lt; http://foo.bar &gt;</p>
  6354. ````````````````````````````````
  6355. ```````````````````````````````` example
  6356. <m:abc>
  6357. .
  6358. <p>&lt;m:abc&gt;</p>
  6359. ````````````````````````````````
  6360. ```````````````````````````````` example
  6361. <foo.bar.baz>
  6362. .
  6363. <p>&lt;foo.bar.baz&gt;</p>
  6364. ````````````````````````````````
  6365. ```````````````````````````````` example
  6366. http://example.com
  6367. .
  6368. <p>http://example.com</p>
  6369. ````````````````````````````````
  6370. ```````````````````````````````` example
  6371. foo@bar.example.com
  6372. .
  6373. <p>foo@bar.example.com</p>
  6374. ````````````````````````````````
  6375. ## Raw HTML
  6376. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6377. raw HTML tag and will be rendered in HTML without escaping.
  6378. Tag and attribute names are not limited to current HTML tags,
  6379. so custom tags (and even, say, DocBook tags) may be used.
  6380. Here is the grammar for tags:
  6381. A [tag name](@) consists of an ASCII letter
  6382. followed by zero or more ASCII letters, digits, or
  6383. hyphens (`-`).
  6384. An [attribute](@) consists of [whitespace],
  6385. an [attribute name], and an optional
  6386. [attribute value specification].
  6387. An [attribute name](@)
  6388. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6389. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6390. specification restricted to ASCII. HTML5 is laxer.)
  6391. An [attribute value specification](@)
  6392. consists of optional [whitespace],
  6393. a `=` character, optional [whitespace], and an [attribute
  6394. value].
  6395. An [attribute value](@)
  6396. consists of an [unquoted attribute value],
  6397. a [single-quoted attribute value], or a [double-quoted attribute value].
  6398. An [unquoted attribute value](@)
  6399. is a nonempty string of characters not
  6400. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6401. A [single-quoted attribute value](@)
  6402. consists of `'`, zero or more
  6403. characters not including `'`, and a final `'`.
  6404. A [double-quoted attribute value](@)
  6405. consists of `"`, zero or more
  6406. characters not including `"`, and a final `"`.
  6407. An [open tag](@) consists of a `<` character, a [tag name],
  6408. zero or more [attributes], optional [whitespace], an optional `/`
  6409. character, and a `>` character.
  6410. A [closing tag](@) consists of the string `</`, a
  6411. [tag name], optional [whitespace], and the character `>`.
  6412. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6413. where *text* does not start with `>` or `->`, does not end with `-`,
  6414. and does not contain `--`. (See the
  6415. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6416. A [processing instruction](@)
  6417. consists of the string `<?`, a string
  6418. of characters not including the string `?>`, and the string
  6419. `?>`.
  6420. A [declaration](@) consists of the
  6421. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6422. [whitespace], a string of characters not including the
  6423. character `>`, and the character `>`.
  6424. A [CDATA section](@) consists of
  6425. the string `<![CDATA[`, a string of characters not including the string
  6426. `]]>`, and the string `]]>`.
  6427. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6428. an [HTML comment], a [processing instruction], a [declaration],
  6429. or a [CDATA section].
  6430. Here are some simple open tags:
  6431. ```````````````````````````````` example
  6432. <a><bab><c2c>
  6433. .
  6434. <p><a><bab><c2c></p>
  6435. ````````````````````````````````
  6436. Empty elements:
  6437. ```````````````````````````````` example
  6438. <a/><b2/>
  6439. .
  6440. <p><a/><b2/></p>
  6441. ````````````````````````````````
  6442. [Whitespace] is allowed:
  6443. ```````````````````````````````` example
  6444. <a /><b2
  6445. data="foo" >
  6446. .
  6447. <p><a /><b2
  6448. data="foo" ></p>
  6449. ````````````````````````````````
  6450. With attributes:
  6451. ```````````````````````````````` example
  6452. <a foo="bar" bam = 'baz <em>"</em>'
  6453. _boolean zoop:33=zoop:33 />
  6454. .
  6455. <p><a foo="bar" bam = 'baz <em>"</em>'
  6456. _boolean zoop:33=zoop:33 /></p>
  6457. ````````````````````````````````
  6458. Custom tag names can be used:
  6459. ```````````````````````````````` example
  6460. Foo <responsive-image src="foo.jpg" />
  6461. .
  6462. <p>Foo <responsive-image src="foo.jpg" /></p>
  6463. ````````````````````````````````
  6464. Illegal tag names, not parsed as HTML:
  6465. ```````````````````````````````` example
  6466. <33> <__>
  6467. .
  6468. <p>&lt;33&gt; &lt;__&gt;</p>
  6469. ````````````````````````````````
  6470. Illegal attribute names:
  6471. ```````````````````````````````` example
  6472. <a h*#ref="hi">
  6473. .
  6474. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6475. ````````````````````````````````
  6476. Illegal attribute values:
  6477. ```````````````````````````````` example
  6478. <a href="hi'> <a href=hi'>
  6479. .
  6480. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6481. ````````````````````````````````
  6482. Illegal [whitespace]:
  6483. ```````````````````````````````` example
  6484. < a><
  6485. foo><bar/ >
  6486. .
  6487. <p>&lt; a&gt;&lt;
  6488. foo&gt;&lt;bar/ &gt;</p>
  6489. ````````````````````````````````
  6490. Missing [whitespace]:
  6491. ```````````````````````````````` example
  6492. <a href='bar'title=title>
  6493. .
  6494. <p>&lt;a href='bar'title=title&gt;</p>
  6495. ````````````````````````````````
  6496. Closing tags:
  6497. ```````````````````````````````` example
  6498. </a></foo >
  6499. .
  6500. <p></a></foo ></p>
  6501. ````````````````````````````````
  6502. Illegal attributes in closing tag:
  6503. ```````````````````````````````` example
  6504. </a href="foo">
  6505. .
  6506. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6507. ````````````````````````````````
  6508. Comments:
  6509. ```````````````````````````````` example
  6510. foo <!-- this is a
  6511. comment - with hyphen -->
  6512. .
  6513. <p>foo <!-- this is a
  6514. comment - with hyphen --></p>
  6515. ````````````````````````````````
  6516. ```````````````````````````````` example
  6517. foo <!-- not a comment -- two hyphens -->
  6518. .
  6519. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6520. ````````````````````````````````
  6521. Not comments:
  6522. ```````````````````````````````` example
  6523. foo <!--> foo -->
  6524. foo <!-- foo--->
  6525. .
  6526. <p>foo &lt;!--&gt; foo --&gt;</p>
  6527. <p>foo &lt;!-- foo---&gt;</p>
  6528. ````````````````````````````````
  6529. Processing instructions:
  6530. ```````````````````````````````` example
  6531. foo <?php echo $a; ?>
  6532. .
  6533. <p>foo <?php echo $a; ?></p>
  6534. ````````````````````````````````
  6535. Declarations:
  6536. ```````````````````````````````` example
  6537. foo <!ELEMENT br EMPTY>
  6538. .
  6539. <p>foo <!ELEMENT br EMPTY></p>
  6540. ````````````````````````````````
  6541. CDATA sections:
  6542. ```````````````````````````````` example
  6543. foo <![CDATA[>&<]]>
  6544. .
  6545. <p>foo <![CDATA[>&<]]></p>
  6546. ````````````````````````````````
  6547. Entity and numeric character references are preserved in HTML
  6548. attributes:
  6549. ```````````````````````````````` example
  6550. foo <a href="&ouml;">
  6551. .
  6552. <p>foo <a href="&ouml;"></p>
  6553. ````````````````````````````````
  6554. Backslash escapes do not work in HTML attributes:
  6555. ```````````````````````````````` example
  6556. foo <a href="\*">
  6557. .
  6558. <p>foo <a href="\*"></p>
  6559. ````````````````````````````````
  6560. ```````````````````````````````` example
  6561. <a href="\"">
  6562. .
  6563. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6564. ````````````````````````````````
  6565. ## Hard line breaks
  6566. A line break (not in a code span or HTML tag) that is preceded
  6567. by two or more spaces and does not occur at the end of a block
  6568. is parsed as a [hard line break](@) (rendered
  6569. in HTML as a `<br />` tag):
  6570. ```````````````````````````````` example
  6571. foo
  6572. baz
  6573. .
  6574. <p>foo<br />
  6575. baz</p>
  6576. ````````````````````````````````
  6577. For a more visible alternative, a backslash before the
  6578. [line ending] may be used instead of two spaces:
  6579. ```````````````````````````````` example
  6580. foo\
  6581. baz
  6582. .
  6583. <p>foo<br />
  6584. baz</p>
  6585. ````````````````````````````````
  6586. More than two spaces can be used:
  6587. ```````````````````````````````` example
  6588. foo
  6589. baz
  6590. .
  6591. <p>foo<br />
  6592. baz</p>
  6593. ````````````````````````````````
  6594. Leading spaces at the beginning of the next line are ignored:
  6595. ```````````````````````````````` example
  6596. foo
  6597. bar
  6598. .
  6599. <p>foo<br />
  6600. bar</p>
  6601. ````````````````````````````````
  6602. ```````````````````````````````` example
  6603. foo\
  6604. bar
  6605. .
  6606. <p>foo<br />
  6607. bar</p>
  6608. ````````````````````````````````
  6609. Line breaks can occur inside emphasis, links, and other constructs
  6610. that allow inline content:
  6611. ```````````````````````````````` example
  6612. *foo
  6613. bar*
  6614. .
  6615. <p><em>foo<br />
  6616. bar</em></p>
  6617. ````````````````````````````````
  6618. ```````````````````````````````` example
  6619. *foo\
  6620. bar*
  6621. .
  6622. <p><em>foo<br />
  6623. bar</em></p>
  6624. ````````````````````````````````
  6625. Line breaks do not occur inside code spans
  6626. ```````````````````````````````` example
  6627. `code
  6628. span`
  6629. .
  6630. <p><code>code span</code></p>
  6631. ````````````````````````````````
  6632. ```````````````````````````````` example
  6633. `code\
  6634. span`
  6635. .
  6636. <p><code>code\ span</code></p>
  6637. ````````````````````````````````
  6638. or HTML tags:
  6639. ```````````````````````````````` example
  6640. <a href="foo
  6641. bar">
  6642. .
  6643. <p><a href="foo
  6644. bar"></p>
  6645. ````````````````````````````````
  6646. ```````````````````````````````` example
  6647. <a href="foo\
  6648. bar">
  6649. .
  6650. <p><a href="foo\
  6651. bar"></p>
  6652. ````````````````````````````````
  6653. Hard line breaks are for separating inline content within a block.
  6654. Neither syntax for hard line breaks works at the end of a paragraph or
  6655. other block element:
  6656. ```````````````````````````````` example
  6657. foo\
  6658. .
  6659. <p>foo\</p>
  6660. ````````````````````````````````
  6661. ```````````````````````````````` example
  6662. foo
  6663. .
  6664. <p>foo</p>
  6665. ````````````````````````````````
  6666. ```````````````````````````````` example
  6667. ### foo\
  6668. .
  6669. <h3>foo\</h3>
  6670. ````````````````````````````````
  6671. ```````````````````````````````` example
  6672. ### foo
  6673. .
  6674. <h3>foo</h3>
  6675. ````````````````````````````````
  6676. ## Soft line breaks
  6677. A regular line break (not in a code span or HTML tag) that is not
  6678. preceded by two or more spaces or a backslash is parsed as a
  6679. softbreak. (A softbreak may be rendered in HTML either as a
  6680. [line ending] or as a space. The result will be the same in
  6681. browsers. In the examples here, a [line ending] will be used.)
  6682. ```````````````````````````````` example
  6683. foo
  6684. baz
  6685. .
  6686. <p>foo
  6687. baz</p>
  6688. ````````````````````````````````
  6689. Spaces at the end of the line and beginning of the next line are
  6690. removed:
  6691. ```````````````````````````````` example
  6692. foo
  6693. baz
  6694. .
  6695. <p>foo
  6696. baz</p>
  6697. ````````````````````````````````
  6698. A conforming parser may render a soft line break in HTML either as a
  6699. line break or as a space.
  6700. A renderer may also provide an option to render soft line breaks
  6701. as hard line breaks.
  6702. ## Textual content
  6703. Any characters not given an interpretation by the above rules will
  6704. be parsed as plain textual content.
  6705. ```````````````````````````````` example
  6706. hello $.;'there
  6707. .
  6708. <p>hello $.;'there</p>
  6709. ````````````````````````````````
  6710. ```````````````````````````````` example
  6711. Foo χρῆν
  6712. .
  6713. <p>Foo χρῆν</p>
  6714. ````````````````````````````````
  6715. Internal spaces are preserved verbatim:
  6716. ```````````````````````````````` example
  6717. Multiple spaces
  6718. .
  6719. <p>Multiple spaces</p>
  6720. ````````````````````````````````
  6721. <!-- END TESTS -->
  6722. # Appendix: A parsing strategy
  6723. In this appendix we describe some features of the parsing strategy
  6724. used in the CommonMark reference implementations.
  6725. ## Overview
  6726. Parsing has two phases:
  6727. 1. In the first phase, lines of input are consumed and the block
  6728. structure of the document---its division into paragraphs, block quotes,
  6729. list items, and so on---is constructed. Text is assigned to these
  6730. blocks but not parsed. Link reference definitions are parsed and a
  6731. map of links is constructed.
  6732. 2. In the second phase, the raw text contents of paragraphs and headings
  6733. are parsed into sequences of Markdown inline elements (strings,
  6734. code spans, links, emphasis, and so on), using the map of link
  6735. references constructed in phase 1.
  6736. At each point in processing, the document is represented as a tree of
  6737. **blocks**. The root of the tree is a `document` block. The `document`
  6738. may have any number of other blocks as **children**. These children
  6739. may, in turn, have other blocks as children. The last child of a block
  6740. is normally considered **open**, meaning that subsequent lines of input
  6741. can alter its contents. (Blocks that are not open are **closed**.)
  6742. Here, for example, is a possible document tree, with the open blocks
  6743. marked by arrows:
  6744. ``` tree
  6745. -> document
  6746. -> block_quote
  6747. paragraph
  6748. "Lorem ipsum dolor\nsit amet."
  6749. -> list (type=bullet tight=true bullet_char=-)
  6750. list_item
  6751. paragraph
  6752. "Qui *quodsi iracundia*"
  6753. -> list_item
  6754. -> paragraph
  6755. "aliquando id"
  6756. ```
  6757. ## Phase 1: block structure
  6758. Each line that is processed has an effect on this tree. The line is
  6759. analyzed and, depending on its contents, the document may be altered
  6760. in one or more of the following ways:
  6761. 1. One or more open blocks may be closed.
  6762. 2. One or more new blocks may be created as children of the
  6763. last open block.
  6764. 3. Text may be added to the last (deepest) open block remaining
  6765. on the tree.
  6766. Once a line has been incorporated into the tree in this way,
  6767. it can be discarded, so input can be read in a stream.
  6768. For each line, we follow this procedure:
  6769. 1. First we iterate through the open blocks, starting with the
  6770. root document, and descending through last children down to the last
  6771. open block. Each block imposes a condition that the line must satisfy
  6772. if the block is to remain open. For example, a block quote requires a
  6773. `>` character. A paragraph requires a non-blank line.
  6774. In this phase we may match all or just some of the open
  6775. blocks. But we cannot close unmatched blocks yet, because we may have a
  6776. [lazy continuation line].
  6777. 2. Next, after consuming the continuation markers for existing
  6778. blocks, we look for new block starts (e.g. `>` for a block quote.
  6779. If we encounter a new block start, we close any blocks unmatched
  6780. in step 1 before creating the new block as a child of the last
  6781. matched block.
  6782. 3. Finally, we look at the remainder of the line (after block
  6783. markers like `>`, list markers, and indentation have been consumed).
  6784. This is text that can be incorporated into the last open
  6785. block (a paragraph, code block, heading, or raw HTML).
  6786. Setext headings are formed when we see a line of a paragraph
  6787. that is a [setext heading underline].
  6788. Reference link definitions are detected when a paragraph is closed;
  6789. the accumulated text lines are parsed to see if they begin with
  6790. one or more reference link definitions. Any remainder becomes a
  6791. normal paragraph.
  6792. We can see how this works by considering how the tree above is
  6793. generated by four lines of Markdown:
  6794. ``` markdown
  6795. > Lorem ipsum dolor
  6796. sit amet.
  6797. > - Qui *quodsi iracundia*
  6798. > - aliquando id
  6799. ```
  6800. At the outset, our document model is just
  6801. ``` tree
  6802. -> document
  6803. ```
  6804. The first line of our text,
  6805. ``` markdown
  6806. > Lorem ipsum dolor
  6807. ```
  6808. causes a `block_quote` block to be created as a child of our
  6809. open `document` block, and a `paragraph` block as a child of
  6810. the `block_quote`. Then the text is added to the last open
  6811. block, the `paragraph`:
  6812. ``` tree
  6813. -> document
  6814. -> block_quote
  6815. -> paragraph
  6816. "Lorem ipsum dolor"
  6817. ```
  6818. The next line,
  6819. ``` markdown
  6820. sit amet.
  6821. ```
  6822. is a "lazy continuation" of the open `paragraph`, so it gets added
  6823. to the paragraph's text:
  6824. ``` tree
  6825. -> document
  6826. -> block_quote
  6827. -> paragraph
  6828. "Lorem ipsum dolor\nsit amet."
  6829. ```
  6830. The third line,
  6831. ``` markdown
  6832. > - Qui *quodsi iracundia*
  6833. ```
  6834. causes the `paragraph` block to be closed, and a new `list` block
  6835. opened as a child of the `block_quote`. A `list_item` is also
  6836. added as a child of the `list`, and a `paragraph` as a child of
  6837. the `list_item`. The text is then added to the new `paragraph`:
  6838. ``` tree
  6839. -> document
  6840. -> block_quote
  6841. paragraph
  6842. "Lorem ipsum dolor\nsit amet."
  6843. -> list (type=bullet tight=true bullet_char=-)
  6844. -> list_item
  6845. -> paragraph
  6846. "Qui *quodsi iracundia*"
  6847. ```
  6848. The fourth line,
  6849. ``` markdown
  6850. > - aliquando id
  6851. ```
  6852. causes the `list_item` (and its child the `paragraph`) to be closed,
  6853. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6854. is added as a child of the new `list_item`, to contain the text.
  6855. We thus obtain the final tree:
  6856. ``` tree
  6857. -> document
  6858. -> block_quote
  6859. paragraph
  6860. "Lorem ipsum dolor\nsit amet."
  6861. -> list (type=bullet tight=true bullet_char=-)
  6862. list_item
  6863. paragraph
  6864. "Qui *quodsi iracundia*"
  6865. -> list_item
  6866. -> paragraph
  6867. "aliquando id"
  6868. ```
  6869. ## Phase 2: inline structure
  6870. Once all of the input has been parsed, all open blocks are closed.
  6871. We then "walk the tree," visiting every node, and parse raw
  6872. string contents of paragraphs and headings as inlines. At this
  6873. point we have seen all the link reference definitions, so we can
  6874. resolve reference links as we go.
  6875. ``` tree
  6876. document
  6877. block_quote
  6878. paragraph
  6879. str "Lorem ipsum dolor"
  6880. softbreak
  6881. str "sit amet."
  6882. list (type=bullet tight=true bullet_char=-)
  6883. list_item
  6884. paragraph
  6885. str "Qui "
  6886. emph
  6887. str "quodsi iracundia"
  6888. list_item
  6889. paragraph
  6890. str "aliquando id"
  6891. ```
  6892. Notice how the [line ending] in the first paragraph has
  6893. been parsed as a `softbreak`, and the asterisks in the first list item
  6894. have become an `emph`.
  6895. ### An algorithm for parsing nested emphasis and links
  6896. By far the trickiest part of inline parsing is handling emphasis,
  6897. strong emphasis, links, and images. This is done using the following
  6898. algorithm.
  6899. When we're parsing inlines and we hit either
  6900. - a run of `*` or `_` characters, or
  6901. - a `[` or `![`
  6902. we insert a text node with these symbols as its literal content, and we
  6903. add a pointer to this text node to the [delimiter stack](@).
  6904. The [delimiter stack] is a doubly linked list. Each
  6905. element contains a pointer to a text node, plus information about
  6906. - the type of delimiter (`[`, `![`, `*`, `_`)
  6907. - the number of delimiters,
  6908. - whether the delimiter is "active" (all are active to start), and
  6909. - whether the delimiter is a potential opener, a potential closer,
  6910. or both (which depends on what sort of characters precede
  6911. and follow the delimiters).
  6912. When we hit a `]` character, we call the *look for link or image*
  6913. procedure (see below).
  6914. When we hit the end of the input, we call the *process emphasis*
  6915. procedure (see below), with `stack_bottom` = NULL.
  6916. #### *look for link or image*
  6917. Starting at the top of the delimiter stack, we look backwards
  6918. through the stack for an opening `[` or `![` delimiter.
  6919. - If we don't find one, we return a literal text node `]`.
  6920. - If we do find one, but it's not *active*, we remove the inactive
  6921. delimiter from the stack, and return a literal text node `]`.
  6922. - If we find one and it's active, then we parse ahead to see if
  6923. we have an inline link/image, reference link/image, compact reference
  6924. link/image, or shortcut reference link/image.
  6925. + If we don't, then we remove the opening delimiter from the
  6926. delimiter stack and return a literal text node `]`.
  6927. + If we do, then
  6928. * We return a link or image node whose children are the inlines
  6929. after the text node pointed to by the opening delimiter.
  6930. * We run *process emphasis* on these inlines, with the `[` opener
  6931. as `stack_bottom`.
  6932. * We remove the opening delimiter.
  6933. * If we have a link (and not an image), we also set all
  6934. `[` delimiters before the opening delimiter to *inactive*. (This
  6935. will prevent us from getting links within links.)
  6936. #### *process emphasis*
  6937. Parameter `stack_bottom` sets a lower bound to how far we
  6938. descend in the [delimiter stack]. If it is NULL, we can
  6939. go all the way to the bottom. Otherwise, we stop before
  6940. visiting `stack_bottom`.
  6941. Let `current_position` point to the element on the [delimiter stack]
  6942. just above `stack_bottom` (or the first element if `stack_bottom`
  6943. is NULL).
  6944. We keep track of the `openers_bottom` for each delimiter
  6945. type (`*`, `_`). Initialize this to `stack_bottom`.
  6946. Then we repeat the following until we run out of potential
  6947. closers:
  6948. - Move `current_position` forward in the delimiter stack (if needed)
  6949. until we find the first potential closer with delimiter `*` or `_`.
  6950. (This will be the potential closer closest
  6951. to the beginning of the input -- the first one in parse order.)
  6952. - Now, look back in the stack (staying above `stack_bottom` and
  6953. the `openers_bottom` for this delimiter type) for the
  6954. first matching potential opener ("matching" means same delimiter).
  6955. - If one is found:
  6956. + Figure out whether we have emphasis or strong emphasis:
  6957. if both closer and opener spans have length >= 2, we have
  6958. strong, otherwise regular.
  6959. + Insert an emph or strong emph node accordingly, after
  6960. the text node corresponding to the opener.
  6961. + Remove any delimiters between the opener and closer from
  6962. the delimiter stack.
  6963. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  6964. from the opening and closing text nodes. If they become empty
  6965. as a result, remove them and remove the corresponding element
  6966. of the delimiter stack. If the closing node is removed, reset
  6967. `current_position` to the next element in the stack.
  6968. - If none in found:
  6969. + Set `openers_bottom` to the element before `current_position`.
  6970. (We know that there are no openers for this kind of closer up to and
  6971. including this point, so this puts a lower bound on future searches.)
  6972. + If the closer at `current_position` is not a potential opener,
  6973. remove it from the delimiter stack (since we know it can't
  6974. be a closer either).
  6975. + Advance `current_position` to the next element in the stack.
  6976. After we're done, we remove all delimiters above `stack_bottom` from the
  6977. delimiter stack.