aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 958ca6491ab9f068c7376e757d3398981f9b7e14 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  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 for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a GitHub wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  252. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  253. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  254. `{`, `|`, `}`, or `~` (U+007B–007E).
  255. A [punctuation character](@) is an [ASCII
  256. punctuation character] or anything in
  257. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  258. ## Tabs
  259. Tabs in lines are not expanded to [spaces]. However,
  260. in contexts where whitespace helps to define block structure,
  261. tabs behave as if they were replaced by spaces with a tab stop
  262. of 4 characters.
  263. Thus, for example, a tab can be used instead of four spaces
  264. in an indented code block. (Note, however, that internal
  265. tabs are passed through as literal tabs, not expanded to
  266. spaces.)
  267. ```````````````````````````````` example
  268. →foo→baz→→bim
  269. .
  270. <pre><code>foo→baz→→bim
  271. </code></pre>
  272. ````````````````````````````````
  273. ```````````````````````````````` example
  274. →foo→baz→→bim
  275. .
  276. <pre><code>foo→baz→→bim
  277. </code></pre>
  278. ````````````````````````````````
  279. ```````````````````````````````` example
  280. a→a
  281. ὐ→a
  282. .
  283. <pre><code>a→a
  284. ὐ→a
  285. </code></pre>
  286. ````````````````````````````````
  287. In the following example, a continuation paragraph of a list
  288. item is indented with a tab; this has exactly the same effect
  289. as indentation with four spaces would:
  290. ```````````````````````````````` example
  291. - foo
  292. →bar
  293. .
  294. <ul>
  295. <li>
  296. <p>foo</p>
  297. <p>bar</p>
  298. </li>
  299. </ul>
  300. ````````````````````````````````
  301. ```````````````````````````````` example
  302. - foo
  303. →→bar
  304. .
  305. <ul>
  306. <li>
  307. <p>foo</p>
  308. <pre><code> bar
  309. </code></pre>
  310. </li>
  311. </ul>
  312. ````````````````````````````````
  313. Normally the `>` that begins a block quote may be followed
  314. optionally by a space, which is not considered part of the
  315. content. In the following case `>` is followed by a tab,
  316. which is treated as if it were expanded into three spaces.
  317. Since one of these spaces is considered part of the
  318. delimiter, `foo` is considered to be indented six spaces
  319. inside the block quote context, so we get an indented
  320. code block starting with two spaces.
  321. ```````````````````````````````` example
  322. >→→foo
  323. .
  324. <blockquote>
  325. <pre><code> foo
  326. </code></pre>
  327. </blockquote>
  328. ````````````````````````````````
  329. ```````````````````````````````` example
  330. -→→foo
  331. .
  332. <ul>
  333. <li>
  334. <pre><code> foo
  335. </code></pre>
  336. </li>
  337. </ul>
  338. ````````````````````````````````
  339. ```````````````````````````````` example
  340. foo
  341. →bar
  342. .
  343. <pre><code>foo
  344. bar
  345. </code></pre>
  346. ````````````````````````````````
  347. ```````````````````````````````` example
  348. - foo
  349. - bar
  350. → - baz
  351. .
  352. <ul>
  353. <li>foo
  354. <ul>
  355. <li>bar
  356. <ul>
  357. <li>baz</li>
  358. </ul>
  359. </li>
  360. </ul>
  361. </li>
  362. </ul>
  363. ````````````````````````````````
  364. ```````````````````````````````` example
  365. #→Foo
  366. .
  367. <h1>Foo</h1>
  368. ````````````````````````````````
  369. ```````````````````````````````` example
  370. *→*→*→
  371. .
  372. <hr />
  373. ````````````````````````````````
  374. ## Insecure characters
  375. For security reasons, the Unicode character `U+0000` must be replaced
  376. with the REPLACEMENT CHARACTER (`U+FFFD`).
  377. # Blocks and inlines
  378. We can think of a document as a sequence of
  379. [blocks](@)---structural elements like paragraphs, block
  380. quotations, lists, headings, rules, and code blocks. Some blocks (like
  381. block quotes and list items) contain other blocks; others (like
  382. headings and paragraphs) contain [inline](@) content---text,
  383. links, emphasized text, images, code spans, and so on.
  384. ## Precedence
  385. Indicators of block structure always take precedence over indicators
  386. of inline structure. So, for example, the following is a list with
  387. two items, not a list with one item containing a code span:
  388. ```````````````````````````````` example
  389. - `one
  390. - two`
  391. .
  392. <ul>
  393. <li>`one</li>
  394. <li>two`</li>
  395. </ul>
  396. ````````````````````````````````
  397. This means that parsing can proceed in two steps: first, the block
  398. structure of the document can be discerned; second, text lines inside
  399. paragraphs, headings, and other block constructs can be parsed for inline
  400. structure. The second step requires information about link reference
  401. definitions that will be available only at the end of the first
  402. step. Note that the first step requires processing lines in sequence,
  403. but the second can be parallelized, since the inline parsing of
  404. one block element does not affect the inline parsing of any other.
  405. ## Container blocks and leaf blocks
  406. We can divide blocks into two types:
  407. [container blocks](@),
  408. which can contain other blocks, and [leaf blocks](@),
  409. which cannot.
  410. # Leaf blocks
  411. This section describes the different kinds of leaf block that make up a
  412. Markdown document.
  413. ## Thematic breaks
  414. A line consisting of 0-3 spaces of indentation, followed by a sequence
  415. of three or more matching `-`, `_`, or `*` characters, each followed
  416. optionally by any number of spaces or tabs, forms a
  417. [thematic break](@).
  418. ```````````````````````````````` example
  419. ***
  420. ---
  421. ___
  422. .
  423. <hr />
  424. <hr />
  425. <hr />
  426. ````````````````````````````````
  427. Wrong characters:
  428. ```````````````````````````````` example
  429. +++
  430. .
  431. <p>+++</p>
  432. ````````````````````````````````
  433. ```````````````````````````````` example
  434. ===
  435. .
  436. <p>===</p>
  437. ````````````````````````````````
  438. Not enough characters:
  439. ```````````````````````````````` example
  440. --
  441. **
  442. __
  443. .
  444. <p>--
  445. **
  446. __</p>
  447. ````````````````````````````````
  448. One to three spaces indent are allowed:
  449. ```````````````````````````````` example
  450. ***
  451. ***
  452. ***
  453. .
  454. <hr />
  455. <hr />
  456. <hr />
  457. ````````````````````````````````
  458. Four spaces is too many:
  459. ```````````````````````````````` example
  460. ***
  461. .
  462. <pre><code>***
  463. </code></pre>
  464. ````````````````````````````````
  465. ```````````````````````````````` example
  466. Foo
  467. ***
  468. .
  469. <p>Foo
  470. ***</p>
  471. ````````````````````````````````
  472. More than three characters may be used:
  473. ```````````````````````````````` example
  474. _____________________________________
  475. .
  476. <hr />
  477. ````````````````````````````````
  478. Spaces are allowed between the characters:
  479. ```````````````````````````````` example
  480. - - -
  481. .
  482. <hr />
  483. ````````````````````````````````
  484. ```````````````````````````````` example
  485. ** * ** * ** * **
  486. .
  487. <hr />
  488. ````````````````````````````````
  489. ```````````````````````````````` example
  490. - - - -
  491. .
  492. <hr />
  493. ````````````````````````````````
  494. Spaces are allowed at the end:
  495. ```````````````````````````````` example
  496. - - - -
  497. .
  498. <hr />
  499. ````````````````````````````````
  500. However, no other characters may occur in the line:
  501. ```````````````````````````````` example
  502. _ _ _ _ a
  503. a------
  504. ---a---
  505. .
  506. <p>_ _ _ _ a</p>
  507. <p>a------</p>
  508. <p>---a---</p>
  509. ````````````````````````````````
  510. It is required that all of the [non-whitespace characters] be the same.
  511. So, this is not a thematic break:
  512. ```````````````````````````````` example
  513. *-*
  514. .
  515. <p><em>-</em></p>
  516. ````````````````````````````````
  517. Thematic breaks do not need blank lines before or after:
  518. ```````````````````````````````` example
  519. - foo
  520. ***
  521. - bar
  522. .
  523. <ul>
  524. <li>foo</li>
  525. </ul>
  526. <hr />
  527. <ul>
  528. <li>bar</li>
  529. </ul>
  530. ````````````````````````````````
  531. Thematic breaks can interrupt a paragraph:
  532. ```````````````````````````````` example
  533. Foo
  534. ***
  535. bar
  536. .
  537. <p>Foo</p>
  538. <hr />
  539. <p>bar</p>
  540. ````````````````````````````````
  541. If a line of dashes that meets the above conditions for being a
  542. thematic break could also be interpreted as the underline of a [setext
  543. heading], the interpretation as a
  544. [setext heading] takes precedence. Thus, for example,
  545. this is a setext heading, not a paragraph followed by a thematic break:
  546. ```````````````````````````````` example
  547. Foo
  548. ---
  549. bar
  550. .
  551. <h2>Foo</h2>
  552. <p>bar</p>
  553. ````````````````````````````````
  554. When both a thematic break and a list item are possible
  555. interpretations of a line, the thematic break takes precedence:
  556. ```````````````````````````````` example
  557. * Foo
  558. * * *
  559. * Bar
  560. .
  561. <ul>
  562. <li>Foo</li>
  563. </ul>
  564. <hr />
  565. <ul>
  566. <li>Bar</li>
  567. </ul>
  568. ````````````````````````````````
  569. If you want a thematic break in a list item, use a different bullet:
  570. ```````````````````````````````` example
  571. - Foo
  572. - * * *
  573. .
  574. <ul>
  575. <li>Foo</li>
  576. <li>
  577. <hr />
  578. </li>
  579. </ul>
  580. ````````````````````````````````
  581. ## ATX headings
  582. An [ATX heading](@)
  583. consists of a string of characters, parsed as inline content, between an
  584. opening sequence of 1--6 unescaped `#` characters and an optional
  585. closing sequence of any number of unescaped `#` characters.
  586. The opening sequence of `#` characters must be followed by a
  587. [space] or by the end of line. The optional closing sequence of `#`s must be
  588. preceded by a [space] and may be followed by spaces only. The opening
  589. `#` character may be indented 0-3 spaces. The raw contents of the
  590. heading are stripped of leading and trailing spaces before being parsed
  591. as inline content. The heading level is equal to the number of `#`
  592. characters in the opening sequence.
  593. Simple headings:
  594. ```````````````````````````````` example
  595. # foo
  596. ## foo
  597. ### foo
  598. #### foo
  599. ##### foo
  600. ###### foo
  601. .
  602. <h1>foo</h1>
  603. <h2>foo</h2>
  604. <h3>foo</h3>
  605. <h4>foo</h4>
  606. <h5>foo</h5>
  607. <h6>foo</h6>
  608. ````````````````````````````````
  609. More than six `#` characters is not a heading:
  610. ```````````````````````````````` example
  611. ####### foo
  612. .
  613. <p>####### foo</p>
  614. ````````````````````````````````
  615. At least one space is required between the `#` characters and the
  616. heading's contents, unless the heading is empty. Note that many
  617. implementations currently do not require the space. However, the
  618. space was required by the
  619. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  620. and it helps prevent things like the following from being parsed as
  621. headings:
  622. ```````````````````````````````` example
  623. #5 bolt
  624. #hashtag
  625. .
  626. <p>#5 bolt</p>
  627. <p>#hashtag</p>
  628. ````````````````````````````````
  629. This is not a heading, because the first `#` is escaped:
  630. ```````````````````````````````` example
  631. \## foo
  632. .
  633. <p>## foo</p>
  634. ````````````````````````````````
  635. Contents are parsed as inlines:
  636. ```````````````````````````````` example
  637. # foo *bar* \*baz\*
  638. .
  639. <h1>foo <em>bar</em> *baz*</h1>
  640. ````````````````````````````````
  641. Leading and trailing blanks are ignored in parsing inline content:
  642. ```````````````````````````````` example
  643. # foo
  644. .
  645. <h1>foo</h1>
  646. ````````````````````````````````
  647. One to three spaces indentation are allowed:
  648. ```````````````````````````````` example
  649. ### foo
  650. ## foo
  651. # foo
  652. .
  653. <h3>foo</h3>
  654. <h2>foo</h2>
  655. <h1>foo</h1>
  656. ````````````````````````````````
  657. Four spaces are too much:
  658. ```````````````````````````````` example
  659. # foo
  660. .
  661. <pre><code># foo
  662. </code></pre>
  663. ````````````````````````````````
  664. ```````````````````````````````` example
  665. foo
  666. # bar
  667. .
  668. <p>foo
  669. # bar</p>
  670. ````````````````````````````````
  671. A closing sequence of `#` characters is optional:
  672. ```````````````````````````````` example
  673. ## foo ##
  674. ### bar ###
  675. .
  676. <h2>foo</h2>
  677. <h3>bar</h3>
  678. ````````````````````````````````
  679. It need not be the same length as the opening sequence:
  680. ```````````````````````````````` example
  681. # foo ##################################
  682. ##### foo ##
  683. .
  684. <h1>foo</h1>
  685. <h5>foo</h5>
  686. ````````````````````````````````
  687. Spaces are allowed after the closing sequence:
  688. ```````````````````````````````` example
  689. ### foo ###
  690. .
  691. <h3>foo</h3>
  692. ````````````````````````````````
  693. A sequence of `#` characters with anything but [spaces] following it
  694. is not a closing sequence, but counts as part of the contents of the
  695. heading:
  696. ```````````````````````````````` example
  697. ### foo ### b
  698. .
  699. <h3>foo ### b</h3>
  700. ````````````````````````````````
  701. The closing sequence must be preceded by a space:
  702. ```````````````````````````````` example
  703. # foo#
  704. .
  705. <h1>foo#</h1>
  706. ````````````````````````````````
  707. Backslash-escaped `#` characters do not count as part
  708. of the closing sequence:
  709. ```````````````````````````````` example
  710. ### foo \###
  711. ## foo #\##
  712. # foo \#
  713. .
  714. <h3>foo ###</h3>
  715. <h2>foo ###</h2>
  716. <h1>foo #</h1>
  717. ````````````````````````````````
  718. ATX headings need not be separated from surrounding content by blank
  719. lines, and they can interrupt paragraphs:
  720. ```````````````````````````````` example
  721. ****
  722. ## foo
  723. ****
  724. .
  725. <hr />
  726. <h2>foo</h2>
  727. <hr />
  728. ````````````````````````````````
  729. ```````````````````````````````` example
  730. Foo bar
  731. # baz
  732. Bar foo
  733. .
  734. <p>Foo bar</p>
  735. <h1>baz</h1>
  736. <p>Bar foo</p>
  737. ````````````````````````````````
  738. ATX headings can be empty:
  739. ```````````````````````````````` example
  740. ##
  741. #
  742. ### ###
  743. .
  744. <h2></h2>
  745. <h1></h1>
  746. <h3></h3>
  747. ````````````````````````````````
  748. ## Setext headings
  749. A [setext heading](@) consists of one or more
  750. lines of text, each containing at least one [non-whitespace
  751. character], with no more than 3 spaces indentation, followed by
  752. a [setext heading underline]. The lines of text must be such
  753. that, were they not followed by the setext heading underline,
  754. they would be interpreted as a paragraph: they cannot be
  755. interpretable as a [code fence], [ATX heading][ATX headings],
  756. [block quote][block quotes], [thematic break][thematic breaks],
  757. [list item][list items], or [HTML block][HTML blocks].
  758. A [setext heading underline](@) is a sequence of
  759. `=` characters or a sequence of `-` characters, with no more than 3
  760. spaces indentation and any number of trailing spaces. If a line
  761. containing a single `-` can be interpreted as an
  762. empty [list items], it should be interpreted this way
  763. and not as a [setext heading underline].
  764. The heading is a level 1 heading if `=` characters are used in
  765. the [setext heading underline], and a level 2 heading if `-`
  766. characters are used. The contents of the heading are the result
  767. of parsing the preceding lines of text as CommonMark inline
  768. content.
  769. In general, a setext heading need not be preceded or followed by a
  770. blank line. However, it cannot interrupt a paragraph, so when a
  771. setext heading comes after a paragraph, a blank line is needed between
  772. them.
  773. Simple examples:
  774. ```````````````````````````````` example
  775. Foo *bar*
  776. =========
  777. Foo *bar*
  778. ---------
  779. .
  780. <h1>Foo <em>bar</em></h1>
  781. <h2>Foo <em>bar</em></h2>
  782. ````````````````````````````````
  783. The content of the header may span more than one line:
  784. ```````````````````````````````` example
  785. Foo *bar
  786. baz*
  787. ====
  788. .
  789. <h1>Foo <em>bar
  790. baz</em></h1>
  791. ````````````````````````````````
  792. The underlining can be any length:
  793. ```````````````````````````````` example
  794. Foo
  795. -------------------------
  796. Foo
  797. =
  798. .
  799. <h2>Foo</h2>
  800. <h1>Foo</h1>
  801. ````````````````````````````````
  802. The heading content can be indented up to three spaces, and need
  803. not line up with the underlining:
  804. ```````````````````````````````` example
  805. Foo
  806. ---
  807. Foo
  808. -----
  809. Foo
  810. ===
  811. .
  812. <h2>Foo</h2>
  813. <h2>Foo</h2>
  814. <h1>Foo</h1>
  815. ````````````````````````````````
  816. Four spaces indent is too much:
  817. ```````````````````````````````` example
  818. Foo
  819. ---
  820. Foo
  821. ---
  822. .
  823. <pre><code>Foo
  824. ---
  825. Foo
  826. </code></pre>
  827. <hr />
  828. ````````````````````````````````
  829. The setext heading underline can be indented up to three spaces, and
  830. may have trailing spaces:
  831. ```````````````````````````````` example
  832. Foo
  833. ----
  834. .
  835. <h2>Foo</h2>
  836. ````````````````````````````````
  837. Four spaces is too much:
  838. ```````````````````````````````` example
  839. Foo
  840. ---
  841. .
  842. <p>Foo
  843. ---</p>
  844. ````````````````````````````````
  845. The setext heading underline cannot contain internal spaces:
  846. ```````````````````````````````` example
  847. Foo
  848. = =
  849. Foo
  850. --- -
  851. .
  852. <p>Foo
  853. = =</p>
  854. <p>Foo</p>
  855. <hr />
  856. ````````````````````````````````
  857. Trailing spaces in the content line do not cause a line break:
  858. ```````````````````````````````` example
  859. Foo
  860. -----
  861. .
  862. <h2>Foo</h2>
  863. ````````````````````````````````
  864. Nor does a backslash at the end:
  865. ```````````````````````````````` example
  866. Foo\
  867. ----
  868. .
  869. <h2>Foo\</h2>
  870. ````````````````````````````````
  871. Since indicators of block structure take precedence over
  872. indicators of inline structure, the following are setext headings:
  873. ```````````````````````````````` example
  874. `Foo
  875. ----
  876. `
  877. <a title="a lot
  878. ---
  879. of dashes"/>
  880. .
  881. <h2>`Foo</h2>
  882. <p>`</p>
  883. <h2>&lt;a title=&quot;a lot</h2>
  884. <p>of dashes&quot;/&gt;</p>
  885. ````````````````````````````````
  886. The setext heading underline cannot be a [lazy continuation
  887. line] in a list item or block quote:
  888. ```````````````````````````````` example
  889. > Foo
  890. ---
  891. .
  892. <blockquote>
  893. <p>Foo</p>
  894. </blockquote>
  895. <hr />
  896. ````````````````````````````````
  897. ```````````````````````````````` example
  898. > foo
  899. bar
  900. ===
  901. .
  902. <blockquote>
  903. <p>foo
  904. bar
  905. ===</p>
  906. </blockquote>
  907. ````````````````````````````````
  908. ```````````````````````````````` example
  909. - Foo
  910. ---
  911. .
  912. <ul>
  913. <li>Foo</li>
  914. </ul>
  915. <hr />
  916. ````````````````````````````````
  917. A blank line is needed between a paragraph and a following
  918. setext heading, since otherwise the paragraph becomes part
  919. of the heading's content:
  920. ```````````````````````````````` example
  921. Foo
  922. Bar
  923. ---
  924. .
  925. <h2>Foo
  926. Bar</h2>
  927. ````````````````````````````````
  928. But in general a blank line is not required before or after
  929. setext headings:
  930. ```````````````````````````````` example
  931. ---
  932. Foo
  933. ---
  934. Bar
  935. ---
  936. Baz
  937. .
  938. <hr />
  939. <h2>Foo</h2>
  940. <h2>Bar</h2>
  941. <p>Baz</p>
  942. ````````````````````````````````
  943. Setext headings cannot be empty:
  944. ```````````````````````````````` example
  945. ====
  946. .
  947. <p>====</p>
  948. ````````````````````````````````
  949. Setext heading text lines must not be interpretable as block
  950. constructs other than paragraphs. So, the line of dashes
  951. in these examples gets interpreted as a thematic break:
  952. ```````````````````````````````` example
  953. ---
  954. ---
  955. .
  956. <hr />
  957. <hr />
  958. ````````````````````````````````
  959. ```````````````````````````````` example
  960. - foo
  961. -----
  962. .
  963. <ul>
  964. <li>foo</li>
  965. </ul>
  966. <hr />
  967. ````````````````````````````````
  968. ```````````````````````````````` example
  969. foo
  970. ---
  971. .
  972. <pre><code>foo
  973. </code></pre>
  974. <hr />
  975. ````````````````````````````````
  976. ```````````````````````````````` example
  977. > foo
  978. -----
  979. .
  980. <blockquote>
  981. <p>foo</p>
  982. </blockquote>
  983. <hr />
  984. ````````````````````````````````
  985. If you want a heading with `> foo` as its literal text, you can
  986. use backslash escapes:
  987. ```````````````````````````````` example
  988. \> foo
  989. ------
  990. .
  991. <h2>&gt; foo</h2>
  992. ````````````````````````````````
  993. **Compatibility note:** Most existing Markdown implementations
  994. do not allow the text of setext headings to span multiple lines.
  995. But there is no consensus about how to interpret
  996. ``` markdown
  997. Foo
  998. bar
  999. ---
  1000. baz
  1001. ```
  1002. One can find four different interpretations:
  1003. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1004. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1005. 3. paragraph "Foo bar --- baz"
  1006. 4. heading "Foo bar", paragraph "baz"
  1007. We find interpretation 4 most natural, and interpretation 4
  1008. increases the expressive power of CommonMark, by allowing
  1009. multiline headings. Authors who want interpretation 1 can
  1010. put a blank line after the first paragraph:
  1011. ```````````````````````````````` example
  1012. Foo
  1013. bar
  1014. ---
  1015. baz
  1016. .
  1017. <p>Foo</p>
  1018. <h2>bar</h2>
  1019. <p>baz</p>
  1020. ````````````````````````````````
  1021. Authors who want interpretation 2 can put blank lines around
  1022. the thematic break,
  1023. ```````````````````````````````` example
  1024. Foo
  1025. bar
  1026. ---
  1027. baz
  1028. .
  1029. <p>Foo
  1030. bar</p>
  1031. <hr />
  1032. <p>baz</p>
  1033. ````````````````````````````````
  1034. or use a thematic break that cannot count as a [setext heading
  1035. underline], such as
  1036. ```````````````````````````````` example
  1037. Foo
  1038. bar
  1039. * * *
  1040. baz
  1041. .
  1042. <p>Foo
  1043. bar</p>
  1044. <hr />
  1045. <p>baz</p>
  1046. ````````````````````````````````
  1047. Authors who want interpretation 3 can use backslash escapes:
  1048. ```````````````````````````````` example
  1049. Foo
  1050. bar
  1051. \---
  1052. baz
  1053. .
  1054. <p>Foo
  1055. bar
  1056. ---
  1057. baz</p>
  1058. ````````````````````````````````
  1059. ## Indented code blocks
  1060. An [indented code block](@) is composed of one or more
  1061. [indented chunks] separated by blank lines.
  1062. An [indented chunk](@) is a sequence of non-blank lines,
  1063. each indented four or more spaces. The contents of the code block are
  1064. the literal contents of the lines, including trailing
  1065. [line endings], minus four spaces of indentation.
  1066. An indented code block has no [info string].
  1067. An indented code block cannot interrupt a paragraph, so there must be
  1068. a blank line between a paragraph and a following indented code block.
  1069. (A blank line is not needed, however, between a code block and a following
  1070. paragraph.)
  1071. ```````````````````````````````` example
  1072. a simple
  1073. indented code block
  1074. .
  1075. <pre><code>a simple
  1076. indented code block
  1077. </code></pre>
  1078. ````````````````````````````````
  1079. If there is any ambiguity between an interpretation of indentation
  1080. as a code block and as indicating that material belongs to a [list
  1081. item][list items], the list item interpretation takes precedence:
  1082. ```````````````````````````````` example
  1083. - foo
  1084. bar
  1085. .
  1086. <ul>
  1087. <li>
  1088. <p>foo</p>
  1089. <p>bar</p>
  1090. </li>
  1091. </ul>
  1092. ````````````````````````````````
  1093. ```````````````````````````````` example
  1094. 1. foo
  1095. - bar
  1096. .
  1097. <ol>
  1098. <li>
  1099. <p>foo</p>
  1100. <ul>
  1101. <li>bar</li>
  1102. </ul>
  1103. </li>
  1104. </ol>
  1105. ````````````````````````````````
  1106. The contents of a code block are literal text, and do not get parsed
  1107. as Markdown:
  1108. ```````````````````````````````` example
  1109. <a/>
  1110. *hi*
  1111. - one
  1112. .
  1113. <pre><code>&lt;a/&gt;
  1114. *hi*
  1115. - one
  1116. </code></pre>
  1117. ````````````````````````````````
  1118. Here we have three chunks separated by blank lines:
  1119. ```````````````````````````````` example
  1120. chunk1
  1121. chunk2
  1122. chunk3
  1123. .
  1124. <pre><code>chunk1
  1125. chunk2
  1126. chunk3
  1127. </code></pre>
  1128. ````````````````````````````````
  1129. Any initial spaces beyond four will be included in the content, even
  1130. in interior blank lines:
  1131. ```````````````````````````````` example
  1132. chunk1
  1133. chunk2
  1134. .
  1135. <pre><code>chunk1
  1136. chunk2
  1137. </code></pre>
  1138. ````````````````````````````````
  1139. An indented code block cannot interrupt a paragraph. (This
  1140. allows hanging indents and the like.)
  1141. ```````````````````````````````` example
  1142. Foo
  1143. bar
  1144. .
  1145. <p>Foo
  1146. bar</p>
  1147. ````````````````````````````````
  1148. However, any non-blank line with fewer than four leading spaces ends
  1149. the code block immediately. So a paragraph may occur immediately
  1150. after indented code:
  1151. ```````````````````````````````` example
  1152. foo
  1153. bar
  1154. .
  1155. <pre><code>foo
  1156. </code></pre>
  1157. <p>bar</p>
  1158. ````````````````````````````````
  1159. And indented code can occur immediately before and after other kinds of
  1160. blocks:
  1161. ```````````````````````````````` example
  1162. # Heading
  1163. foo
  1164. Heading
  1165. ------
  1166. foo
  1167. ----
  1168. .
  1169. <h1>Heading</h1>
  1170. <pre><code>foo
  1171. </code></pre>
  1172. <h2>Heading</h2>
  1173. <pre><code>foo
  1174. </code></pre>
  1175. <hr />
  1176. ````````````````````````````````
  1177. The first line can be indented more than four spaces:
  1178. ```````````````````````````````` example
  1179. foo
  1180. bar
  1181. .
  1182. <pre><code> foo
  1183. bar
  1184. </code></pre>
  1185. ````````````````````````````````
  1186. Blank lines preceding or following an indented code block
  1187. are not included in it:
  1188. ```````````````````````````````` example
  1189. foo
  1190. .
  1191. <pre><code>foo
  1192. </code></pre>
  1193. ````````````````````````````````
  1194. Trailing spaces are included in the code block's content:
  1195. ```````````````````````````````` example
  1196. foo
  1197. .
  1198. <pre><code>foo
  1199. </code></pre>
  1200. ````````````````````````````````
  1201. ## Fenced code blocks
  1202. A [code fence](@) is a sequence
  1203. of at least three consecutive backtick characters (`` ` ``) or
  1204. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1205. A [fenced code block](@)
  1206. begins with a code fence, indented no more than three spaces.
  1207. The line with the opening code fence may optionally contain some text
  1208. following the code fence; this is trimmed of leading and trailing
  1209. whitespace and called the [info string](@). If the [info string] comes
  1210. after a backtick fence, it may not contain any backtick
  1211. characters. (The reason for this restriction is that otherwise
  1212. some inline code would be incorrectly interpreted as the
  1213. beginning of a fenced code block.)
  1214. The content of the code block consists of all subsequent lines, until
  1215. a closing [code fence] of the same type as the code block
  1216. began with (backticks or tildes), and with at least as many backticks
  1217. or tildes as the opening code fence. If the leading code fence is
  1218. indented N spaces, then up to N spaces of indentation are removed from
  1219. each line of the content (if present). (If a content line is not
  1220. indented, it is preserved unchanged. If it is indented less than N
  1221. spaces, all of the indentation is removed.)
  1222. The closing code fence may be indented up to three spaces, and may be
  1223. followed only by spaces, which are ignored. If the end of the
  1224. containing block (or document) is reached and no closing code fence
  1225. has been found, the code block contains all of the lines after the
  1226. opening code fence until the end of the containing block (or
  1227. document). (An alternative spec would require backtracking in the
  1228. event that a closing code fence is not found. But this makes parsing
  1229. much less efficient, and there seems to be no real down side to the
  1230. behavior described here.)
  1231. A fenced code block may interrupt a paragraph, and does not require
  1232. a blank line either before or after.
  1233. The content of a code fence is treated as literal text, not parsed
  1234. as inlines. The first word of the [info string] is typically used to
  1235. specify the language of the code sample, and rendered in the `class`
  1236. attribute of the `code` tag. However, this spec does not mandate any
  1237. particular treatment of the [info string].
  1238. Here is a simple example with backticks:
  1239. ```````````````````````````````` example
  1240. ```
  1241. <
  1242. >
  1243. ```
  1244. .
  1245. <pre><code>&lt;
  1246. &gt;
  1247. </code></pre>
  1248. ````````````````````````````````
  1249. With tildes:
  1250. ```````````````````````````````` example
  1251. ~~~
  1252. <
  1253. >
  1254. ~~~
  1255. .
  1256. <pre><code>&lt;
  1257. &gt;
  1258. </code></pre>
  1259. ````````````````````````````````
  1260. Fewer than three backticks is not enough:
  1261. ```````````````````````````````` example
  1262. ``
  1263. foo
  1264. ``
  1265. .
  1266. <p><code>foo</code></p>
  1267. ````````````````````````````````
  1268. The closing code fence must use the same character as the opening
  1269. fence:
  1270. ```````````````````````````````` example
  1271. ```
  1272. aaa
  1273. ~~~
  1274. ```
  1275. .
  1276. <pre><code>aaa
  1277. ~~~
  1278. </code></pre>
  1279. ````````````````````````````````
  1280. ```````````````````````````````` example
  1281. ~~~
  1282. aaa
  1283. ```
  1284. ~~~
  1285. .
  1286. <pre><code>aaa
  1287. ```
  1288. </code></pre>
  1289. ````````````````````````````````
  1290. The closing code fence must be at least as long as the opening fence:
  1291. ```````````````````````````````` example
  1292. ````
  1293. aaa
  1294. ```
  1295. ``````
  1296. .
  1297. <pre><code>aaa
  1298. ```
  1299. </code></pre>
  1300. ````````````````````````````````
  1301. ```````````````````````````````` example
  1302. ~~~~
  1303. aaa
  1304. ~~~
  1305. ~~~~
  1306. .
  1307. <pre><code>aaa
  1308. ~~~
  1309. </code></pre>
  1310. ````````````````````````````````
  1311. Unclosed code blocks are closed by the end of the document
  1312. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1313. ```````````````````````````````` example
  1314. ```
  1315. .
  1316. <pre><code></code></pre>
  1317. ````````````````````````````````
  1318. ```````````````````````````````` example
  1319. `````
  1320. ```
  1321. aaa
  1322. .
  1323. <pre><code>
  1324. ```
  1325. aaa
  1326. </code></pre>
  1327. ````````````````````````````````
  1328. ```````````````````````````````` example
  1329. > ```
  1330. > aaa
  1331. bbb
  1332. .
  1333. <blockquote>
  1334. <pre><code>aaa
  1335. </code></pre>
  1336. </blockquote>
  1337. <p>bbb</p>
  1338. ````````````````````````````````
  1339. A code block can have all empty lines as its content:
  1340. ```````````````````````````````` example
  1341. ```
  1342. ```
  1343. .
  1344. <pre><code>
  1345. </code></pre>
  1346. ````````````````````````````````
  1347. A code block can be empty:
  1348. ```````````````````````````````` example
  1349. ```
  1350. ```
  1351. .
  1352. <pre><code></code></pre>
  1353. ````````````````````````````````
  1354. Fences can be indented. If the opening fence is indented,
  1355. content lines will have equivalent opening indentation removed,
  1356. if present:
  1357. ```````````````````````````````` example
  1358. ```
  1359. aaa
  1360. aaa
  1361. ```
  1362. .
  1363. <pre><code>aaa
  1364. aaa
  1365. </code></pre>
  1366. ````````````````````````````````
  1367. ```````````````````````````````` example
  1368. ```
  1369. aaa
  1370. aaa
  1371. aaa
  1372. ```
  1373. .
  1374. <pre><code>aaa
  1375. aaa
  1376. aaa
  1377. </code></pre>
  1378. ````````````````````````````````
  1379. ```````````````````````````````` example
  1380. ```
  1381. aaa
  1382. aaa
  1383. aaa
  1384. ```
  1385. .
  1386. <pre><code>aaa
  1387. aaa
  1388. aaa
  1389. </code></pre>
  1390. ````````````````````````````````
  1391. Four spaces indentation produces an indented code block:
  1392. ```````````````````````````````` example
  1393. ```
  1394. aaa
  1395. ```
  1396. .
  1397. <pre><code>```
  1398. aaa
  1399. ```
  1400. </code></pre>
  1401. ````````````````````````````````
  1402. Closing fences may be indented by 0-3 spaces, and their indentation
  1403. need not match that of the opening fence:
  1404. ```````````````````````````````` example
  1405. ```
  1406. aaa
  1407. ```
  1408. .
  1409. <pre><code>aaa
  1410. </code></pre>
  1411. ````````````````````````````````
  1412. ```````````````````````````````` example
  1413. ```
  1414. aaa
  1415. ```
  1416. .
  1417. <pre><code>aaa
  1418. </code></pre>
  1419. ````````````````````````````````
  1420. This is not a closing fence, because it is indented 4 spaces:
  1421. ```````````````````````````````` example
  1422. ```
  1423. aaa
  1424. ```
  1425. .
  1426. <pre><code>aaa
  1427. ```
  1428. </code></pre>
  1429. ````````````````````````````````
  1430. Code fences (opening and closing) cannot contain internal spaces:
  1431. ```````````````````````````````` example
  1432. ``` ```
  1433. aaa
  1434. .
  1435. <p><code> </code>
  1436. aaa</p>
  1437. ````````````````````````````````
  1438. ```````````````````````````````` example
  1439. ~~~~~~
  1440. aaa
  1441. ~~~ ~~
  1442. .
  1443. <pre><code>aaa
  1444. ~~~ ~~
  1445. </code></pre>
  1446. ````````````````````````````````
  1447. Fenced code blocks can interrupt paragraphs, and can be followed
  1448. directly by paragraphs, without a blank line between:
  1449. ```````````````````````````````` example
  1450. foo
  1451. ```
  1452. bar
  1453. ```
  1454. baz
  1455. .
  1456. <p>foo</p>
  1457. <pre><code>bar
  1458. </code></pre>
  1459. <p>baz</p>
  1460. ````````````````````````````````
  1461. Other blocks can also occur before and after fenced code blocks
  1462. without an intervening blank line:
  1463. ```````````````````````````````` example
  1464. foo
  1465. ---
  1466. ~~~
  1467. bar
  1468. ~~~
  1469. # baz
  1470. .
  1471. <h2>foo</h2>
  1472. <pre><code>bar
  1473. </code></pre>
  1474. <h1>baz</h1>
  1475. ````````````````````````````````
  1476. An [info string] can be provided after the opening code fence.
  1477. Although this spec doesn't mandate any particular treatment of
  1478. the info string, the first word is typically used to specify
  1479. the language of the code block. In HTML output, the language is
  1480. normally indicated by adding a class to the `code` element consisting
  1481. of `language-` followed by the language name.
  1482. ```````````````````````````````` example
  1483. ```ruby
  1484. def foo(x)
  1485. return 3
  1486. end
  1487. ```
  1488. .
  1489. <pre><code class="language-ruby">def foo(x)
  1490. return 3
  1491. end
  1492. </code></pre>
  1493. ````````````````````````````````
  1494. ```````````````````````````````` example
  1495. ~~~~ ruby startline=3 $%@#$
  1496. def foo(x)
  1497. return 3
  1498. end
  1499. ~~~~~~~
  1500. .
  1501. <pre><code class="language-ruby">def foo(x)
  1502. return 3
  1503. end
  1504. </code></pre>
  1505. ````````````````````````````````
  1506. ```````````````````````````````` example
  1507. ````;
  1508. ````
  1509. .
  1510. <pre><code class="language-;"></code></pre>
  1511. ````````````````````````````````
  1512. [Info strings] for backtick code blocks cannot contain backticks:
  1513. ```````````````````````````````` example
  1514. ``` aa ```
  1515. foo
  1516. .
  1517. <p><code>aa</code>
  1518. foo</p>
  1519. ````````````````````````````````
  1520. [Info strings] for tilde code blocks can contain backticks and tildes:
  1521. ```````````````````````````````` example
  1522. ~~~ aa ``` ~~~
  1523. foo
  1524. ~~~
  1525. .
  1526. <pre><code class="language-aa">foo
  1527. </code></pre>
  1528. ````````````````````````````````
  1529. Closing code fences cannot have [info strings]:
  1530. ```````````````````````````````` example
  1531. ```
  1532. ``` aaa
  1533. ```
  1534. .
  1535. <pre><code>``` aaa
  1536. </code></pre>
  1537. ````````````````````````````````
  1538. ## HTML blocks
  1539. An [HTML block](@) is a group of lines that is treated
  1540. as raw HTML (and will not be escaped in HTML output).
  1541. There are seven kinds of [HTML block], which can be defined by their
  1542. start and end conditions. The block begins with a line that meets a
  1543. [start condition](@) (after up to three spaces optional indentation).
  1544. It ends with the first subsequent line that meets a matching [end
  1545. condition](@), or the last line of the document, or the last line of
  1546. the [container block](#container-blocks) containing the current HTML
  1547. block, if no line is encountered that meets the [end condition]. If
  1548. the first line meets both the [start condition] and the [end
  1549. condition], the block will contain just that line.
  1550. 1. **Start condition:** line begins with the string `<script`,
  1551. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1552. the string `>`, or the end of the line.\
  1553. **End condition:** line contains an end tag
  1554. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1555. need not match the start tag).
  1556. 2. **Start condition:** line begins with the string `<!--`.\
  1557. **End condition:** line contains the string `-->`.
  1558. 3. **Start condition:** line begins with the string `<?`.\
  1559. **End condition:** line contains the string `?>`.
  1560. 4. **Start condition:** line begins with the string `<!`
  1561. followed by an uppercase ASCII letter.\
  1562. **End condition:** line contains the character `>`.
  1563. 5. **Start condition:** line begins with the string
  1564. `<![CDATA[`.\
  1565. **End condition:** line contains the string `]]>`.
  1566. 6. **Start condition:** line begins the string `<` or `</`
  1567. followed by one of the strings (case-insensitive) `address`,
  1568. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1569. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1570. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1571. `footer`, `form`, `frame`, `frameset`,
  1572. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1573. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1574. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1575. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1576. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1577. by [whitespace], the end of the line, the string `>`, or
  1578. the string `/>`.\
  1579. **End condition:** line is followed by a [blank line].
  1580. 7. **Start condition:** line begins with a complete [open tag]
  1581. (with any [tag name] other than `script`,
  1582. `style`, or `pre`) or a complete [closing tag],
  1583. followed only by [whitespace] or the end of the line.\
  1584. **End condition:** line is followed by a [blank line].
  1585. HTML blocks continue until they are closed by their appropriate
  1586. [end condition], or the last line of the document or other [container
  1587. block](#container-blocks). This means any HTML **within an HTML
  1588. block** that might otherwise be recognised as a start condition will
  1589. be ignored by the parser and passed through as-is, without changing
  1590. the parser's state.
  1591. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1592. the parser state; as the HTML block was started in by start condition 6, it
  1593. will end at any blank line. This can be surprising:
  1594. ```````````````````````````````` example
  1595. <table><tr><td>
  1596. <pre>
  1597. **Hello**,
  1598. _world_.
  1599. </pre>
  1600. </td></tr></table>
  1601. .
  1602. <table><tr><td>
  1603. <pre>
  1604. **Hello**,
  1605. <p><em>world</em>.
  1606. </pre></p>
  1607. </td></tr></table>
  1608. ````````````````````````````````
  1609. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1610. text remains verbatim — and regular parsing resumes, with a paragraph,
  1611. emphasised `world` and inline and block HTML following.
  1612. All types of [HTML blocks] except type 7 may interrupt
  1613. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1614. (This restriction is intended to prevent unwanted interpretation
  1615. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1616. Some simple examples follow. Here are some basic HTML blocks
  1617. of type 6:
  1618. ```````````````````````````````` example
  1619. <table>
  1620. <tr>
  1621. <td>
  1622. hi
  1623. </td>
  1624. </tr>
  1625. </table>
  1626. okay.
  1627. .
  1628. <table>
  1629. <tr>
  1630. <td>
  1631. hi
  1632. </td>
  1633. </tr>
  1634. </table>
  1635. <p>okay.</p>
  1636. ````````````````````````````````
  1637. ```````````````````````````````` example
  1638. <div>
  1639. *hello*
  1640. <foo><a>
  1641. .
  1642. <div>
  1643. *hello*
  1644. <foo><a>
  1645. ````````````````````````````````
  1646. A block can also start with a closing tag:
  1647. ```````````````````````````````` example
  1648. </div>
  1649. *foo*
  1650. .
  1651. </div>
  1652. *foo*
  1653. ````````````````````````````````
  1654. Here we have two HTML blocks with a Markdown paragraph between them:
  1655. ```````````````````````````````` example
  1656. <DIV CLASS="foo">
  1657. *Markdown*
  1658. </DIV>
  1659. .
  1660. <DIV CLASS="foo">
  1661. <p><em>Markdown</em></p>
  1662. </DIV>
  1663. ````````````````````````````````
  1664. The tag on the first line can be partial, as long
  1665. as it is split where there would be whitespace:
  1666. ```````````````````````````````` example
  1667. <div id="foo"
  1668. class="bar">
  1669. </div>
  1670. .
  1671. <div id="foo"
  1672. class="bar">
  1673. </div>
  1674. ````````````````````````````````
  1675. ```````````````````````````````` example
  1676. <div id="foo" class="bar
  1677. baz">
  1678. </div>
  1679. .
  1680. <div id="foo" class="bar
  1681. baz">
  1682. </div>
  1683. ````````````````````````````````
  1684. An open tag need not be closed:
  1685. ```````````````````````````````` example
  1686. <div>
  1687. *foo*
  1688. *bar*
  1689. .
  1690. <div>
  1691. *foo*
  1692. <p><em>bar</em></p>
  1693. ````````````````````````````````
  1694. A partial tag need not even be completed (garbage
  1695. in, garbage out):
  1696. ```````````````````````````````` example
  1697. <div id="foo"
  1698. *hi*
  1699. .
  1700. <div id="foo"
  1701. *hi*
  1702. ````````````````````````````````
  1703. ```````````````````````````````` example
  1704. <div class
  1705. foo
  1706. .
  1707. <div class
  1708. foo
  1709. ````````````````````````````````
  1710. The initial tag doesn't even need to be a valid
  1711. tag, as long as it starts like one:
  1712. ```````````````````````````````` example
  1713. <div *???-&&&-<---
  1714. *foo*
  1715. .
  1716. <div *???-&&&-<---
  1717. *foo*
  1718. ````````````````````````````````
  1719. In type 6 blocks, the initial tag need not be on a line by
  1720. itself:
  1721. ```````````````````````````````` example
  1722. <div><a href="bar">*foo*</a></div>
  1723. .
  1724. <div><a href="bar">*foo*</a></div>
  1725. ````````````````````````````````
  1726. ```````````````````````````````` example
  1727. <table><tr><td>
  1728. foo
  1729. </td></tr></table>
  1730. .
  1731. <table><tr><td>
  1732. foo
  1733. </td></tr></table>
  1734. ````````````````````````````````
  1735. Everything until the next blank line or end of document
  1736. gets included in the HTML block. So, in the following
  1737. example, what looks like a Markdown code block
  1738. is actually part of the HTML block, which continues until a blank
  1739. line or the end of the document is reached:
  1740. ```````````````````````````````` example
  1741. <div></div>
  1742. ``` c
  1743. int x = 33;
  1744. ```
  1745. .
  1746. <div></div>
  1747. ``` c
  1748. int x = 33;
  1749. ```
  1750. ````````````````````````````````
  1751. To start an [HTML block] with a tag that is *not* in the
  1752. list of block-level tags in (6), you must put the tag by
  1753. itself on the first line (and it must be complete):
  1754. ```````````````````````````````` example
  1755. <a href="foo">
  1756. *bar*
  1757. </a>
  1758. .
  1759. <a href="foo">
  1760. *bar*
  1761. </a>
  1762. ````````````````````````````````
  1763. In type 7 blocks, the [tag name] can be anything:
  1764. ```````````````````````````````` example
  1765. <Warning>
  1766. *bar*
  1767. </Warning>
  1768. .
  1769. <Warning>
  1770. *bar*
  1771. </Warning>
  1772. ````````````````````````````````
  1773. ```````````````````````````````` example
  1774. <i class="foo">
  1775. *bar*
  1776. </i>
  1777. .
  1778. <i class="foo">
  1779. *bar*
  1780. </i>
  1781. ````````````````````````````````
  1782. ```````````````````````````````` example
  1783. </ins>
  1784. *bar*
  1785. .
  1786. </ins>
  1787. *bar*
  1788. ````````````````````````````````
  1789. These rules are designed to allow us to work with tags that
  1790. can function as either block-level or inline-level tags.
  1791. The `<del>` tag is a nice example. We can surround content with
  1792. `<del>` tags in three different ways. In this case, we get a raw
  1793. HTML block, because the `<del>` tag is on a line by itself:
  1794. ```````````````````````````````` example
  1795. <del>
  1796. *foo*
  1797. </del>
  1798. .
  1799. <del>
  1800. *foo*
  1801. </del>
  1802. ````````````````````````````````
  1803. In this case, we get a raw HTML block that just includes
  1804. the `<del>` tag (because it ends with the following blank
  1805. line). So the contents get interpreted as CommonMark:
  1806. ```````````````````````````````` example
  1807. <del>
  1808. *foo*
  1809. </del>
  1810. .
  1811. <del>
  1812. <p><em>foo</em></p>
  1813. </del>
  1814. ````````````````````````````````
  1815. Finally, in this case, the `<del>` tags are interpreted
  1816. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1817. the tag is not on a line by itself, we get inline HTML
  1818. rather than an [HTML block].)
  1819. ```````````````````````````````` example
  1820. <del>*foo*</del>
  1821. .
  1822. <p><del><em>foo</em></del></p>
  1823. ````````````````````````````````
  1824. HTML tags designed to contain literal content
  1825. (`script`, `style`, `pre`), comments, processing instructions,
  1826. and declarations are treated somewhat differently.
  1827. Instead of ending at the first blank line, these blocks
  1828. end at the first line containing a corresponding end tag.
  1829. As a result, these blocks can contain blank lines:
  1830. A pre tag (type 1):
  1831. ```````````````````````````````` example
  1832. <pre language="haskell"><code>
  1833. import Text.HTML.TagSoup
  1834. main :: IO ()
  1835. main = print $ parseTags tags
  1836. </code></pre>
  1837. okay
  1838. .
  1839. <pre language="haskell"><code>
  1840. import Text.HTML.TagSoup
  1841. main :: IO ()
  1842. main = print $ parseTags tags
  1843. </code></pre>
  1844. <p>okay</p>
  1845. ````````````````````````````````
  1846. A script tag (type 1):
  1847. ```````````````````````````````` example
  1848. <script type="text/javascript">
  1849. // JavaScript example
  1850. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1851. </script>
  1852. okay
  1853. .
  1854. <script type="text/javascript">
  1855. // JavaScript example
  1856. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1857. </script>
  1858. <p>okay</p>
  1859. ````````````````````````````````
  1860. A style tag (type 1):
  1861. ```````````````````````````````` example
  1862. <style
  1863. type="text/css">
  1864. h1 {color:red;}
  1865. p {color:blue;}
  1866. </style>
  1867. okay
  1868. .
  1869. <style
  1870. type="text/css">
  1871. h1 {color:red;}
  1872. p {color:blue;}
  1873. </style>
  1874. <p>okay</p>
  1875. ````````````````````````````````
  1876. If there is no matching end tag, the block will end at the
  1877. end of the document (or the enclosing [block quote][block quotes]
  1878. or [list item][list items]):
  1879. ```````````````````````````````` example
  1880. <style
  1881. type="text/css">
  1882. foo
  1883. .
  1884. <style
  1885. type="text/css">
  1886. foo
  1887. ````````````````````````````````
  1888. ```````````````````````````````` example
  1889. > <div>
  1890. > foo
  1891. bar
  1892. .
  1893. <blockquote>
  1894. <div>
  1895. foo
  1896. </blockquote>
  1897. <p>bar</p>
  1898. ````````````````````````````````
  1899. ```````````````````````````````` example
  1900. - <div>
  1901. - foo
  1902. .
  1903. <ul>
  1904. <li>
  1905. <div>
  1906. </li>
  1907. <li>foo</li>
  1908. </ul>
  1909. ````````````````````````````````
  1910. The end tag can occur on the same line as the start tag:
  1911. ```````````````````````````````` example
  1912. <style>p{color:red;}</style>
  1913. *foo*
  1914. .
  1915. <style>p{color:red;}</style>
  1916. <p><em>foo</em></p>
  1917. ````````````````````````````````
  1918. ```````````````````````````````` example
  1919. <!-- foo -->*bar*
  1920. *baz*
  1921. .
  1922. <!-- foo -->*bar*
  1923. <p><em>baz</em></p>
  1924. ````````````````````````````````
  1925. Note that anything on the last line after the
  1926. end tag will be included in the [HTML block]:
  1927. ```````````````````````````````` example
  1928. <script>
  1929. foo
  1930. </script>1. *bar*
  1931. .
  1932. <script>
  1933. foo
  1934. </script>1. *bar*
  1935. ````````````````````````````````
  1936. A comment (type 2):
  1937. ```````````````````````````````` example
  1938. <!-- Foo
  1939. bar
  1940. baz -->
  1941. okay
  1942. .
  1943. <!-- Foo
  1944. bar
  1945. baz -->
  1946. <p>okay</p>
  1947. ````````````````````````````````
  1948. A processing instruction (type 3):
  1949. ```````````````````````````````` example
  1950. <?php
  1951. echo '>';
  1952. ?>
  1953. okay
  1954. .
  1955. <?php
  1956. echo '>';
  1957. ?>
  1958. <p>okay</p>
  1959. ````````````````````````````````
  1960. A declaration (type 4):
  1961. ```````````````````````````````` example
  1962. <!DOCTYPE html>
  1963. .
  1964. <!DOCTYPE html>
  1965. ````````````````````````````````
  1966. CDATA (type 5):
  1967. ```````````````````````````````` example
  1968. <![CDATA[
  1969. function matchwo(a,b)
  1970. {
  1971. if (a < b && a < 0) then {
  1972. return 1;
  1973. } else {
  1974. return 0;
  1975. }
  1976. }
  1977. ]]>
  1978. okay
  1979. .
  1980. <![CDATA[
  1981. function matchwo(a,b)
  1982. {
  1983. if (a < b && a < 0) then {
  1984. return 1;
  1985. } else {
  1986. return 0;
  1987. }
  1988. }
  1989. ]]>
  1990. <p>okay</p>
  1991. ````````````````````````````````
  1992. The opening tag can be indented 1-3 spaces, but not 4:
  1993. ```````````````````````````````` example
  1994. <!-- foo -->
  1995. <!-- foo -->
  1996. .
  1997. <!-- foo -->
  1998. <pre><code>&lt;!-- foo --&gt;
  1999. </code></pre>
  2000. ````````````````````````````````
  2001. ```````````````````````````````` example
  2002. <div>
  2003. <div>
  2004. .
  2005. <div>
  2006. <pre><code>&lt;div&gt;
  2007. </code></pre>
  2008. ````````````````````````````````
  2009. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2010. preceded by a blank line.
  2011. ```````````````````````````````` example
  2012. Foo
  2013. <div>
  2014. bar
  2015. </div>
  2016. .
  2017. <p>Foo</p>
  2018. <div>
  2019. bar
  2020. </div>
  2021. ````````````````````````````````
  2022. However, a following blank line is needed, except at the end of
  2023. a document, and except for blocks of types 1--5, [above][HTML
  2024. block]:
  2025. ```````````````````````````````` example
  2026. <div>
  2027. bar
  2028. </div>
  2029. *foo*
  2030. .
  2031. <div>
  2032. bar
  2033. </div>
  2034. *foo*
  2035. ````````````````````````````````
  2036. HTML blocks of type 7 cannot interrupt a paragraph:
  2037. ```````````````````````````````` example
  2038. Foo
  2039. <a href="bar">
  2040. baz
  2041. .
  2042. <p>Foo
  2043. <a href="bar">
  2044. baz</p>
  2045. ````````````````````````````````
  2046. This rule differs from John Gruber's original Markdown syntax
  2047. specification, which says:
  2048. > The only restrictions are that block-level HTML elements —
  2049. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2050. > surrounding content by blank lines, and the start and end tags of the
  2051. > block should not be indented with tabs or spaces.
  2052. In some ways Gruber's rule is more restrictive than the one given
  2053. here:
  2054. - It requires that an HTML block be preceded by a blank line.
  2055. - It does not allow the start tag to be indented.
  2056. - It requires a matching end tag, which it also does not allow to
  2057. be indented.
  2058. Most Markdown implementations (including some of Gruber's own) do not
  2059. respect all of these restrictions.
  2060. There is one respect, however, in which Gruber's rule is more liberal
  2061. than the one given here, since it allows blank lines to occur inside
  2062. an HTML block. There are two reasons for disallowing them here.
  2063. First, it removes the need to parse balanced tags, which is
  2064. expensive and can require backtracking from the end of the document
  2065. if no matching end tag is found. Second, it provides a very simple
  2066. and flexible way of including Markdown content inside HTML tags:
  2067. simply separate the Markdown from the HTML using blank lines:
  2068. Compare:
  2069. ```````````````````````````````` example
  2070. <div>
  2071. *Emphasized* text.
  2072. </div>
  2073. .
  2074. <div>
  2075. <p><em>Emphasized</em> text.</p>
  2076. </div>
  2077. ````````````````````````````````
  2078. ```````````````````````````````` example
  2079. <div>
  2080. *Emphasized* text.
  2081. </div>
  2082. .
  2083. <div>
  2084. *Emphasized* text.
  2085. </div>
  2086. ````````````````````````````````
  2087. Some Markdown implementations have adopted a convention of
  2088. interpreting content inside tags as text if the open tag has
  2089. the attribute `markdown=1`. The rule given above seems a simpler and
  2090. more elegant way of achieving the same expressive power, which is also
  2091. much simpler to parse.
  2092. The main potential drawback is that one can no longer paste HTML
  2093. blocks into Markdown documents with 100% reliability. However,
  2094. *in most cases* this will work fine, because the blank lines in
  2095. HTML are usually followed by HTML block tags. For example:
  2096. ```````````````````````````````` example
  2097. <table>
  2098. <tr>
  2099. <td>
  2100. Hi
  2101. </td>
  2102. </tr>
  2103. </table>
  2104. .
  2105. <table>
  2106. <tr>
  2107. <td>
  2108. Hi
  2109. </td>
  2110. </tr>
  2111. </table>
  2112. ````````````````````````````````
  2113. There are problems, however, if the inner tags are indented
  2114. *and* separated by spaces, as then they will be interpreted as
  2115. an indented code block:
  2116. ```````````````````````````````` example
  2117. <table>
  2118. <tr>
  2119. <td>
  2120. Hi
  2121. </td>
  2122. </tr>
  2123. </table>
  2124. .
  2125. <table>
  2126. <tr>
  2127. <pre><code>&lt;td&gt;
  2128. Hi
  2129. &lt;/td&gt;
  2130. </code></pre>
  2131. </tr>
  2132. </table>
  2133. ````````````````````````````````
  2134. Fortunately, blank lines are usually not necessary and can be
  2135. deleted. The exception is inside `<pre>` tags, but as described
  2136. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2137. *can* contain blank lines.
  2138. ## Link reference definitions
  2139. A [link reference definition](@)
  2140. consists of a [link label], indented up to three spaces, followed
  2141. by a colon (`:`), optional [whitespace] (including up to one
  2142. [line ending]), a [link destination],
  2143. optional [whitespace] (including up to one
  2144. [line ending]), and an optional [link
  2145. title], which if it is present must be separated
  2146. from the [link destination] by [whitespace].
  2147. No further [non-whitespace characters] may occur on the line.
  2148. A [link reference definition]
  2149. does not correspond to a structural element of a document. Instead, it
  2150. defines a label which can be used in [reference links]
  2151. and reference-style [images] elsewhere in the document. [Link
  2152. reference definitions] can come either before or after the links that use
  2153. them.
  2154. ```````````````````````````````` example
  2155. [foo]: /url "title"
  2156. [foo]
  2157. .
  2158. <p><a href="/url" title="title">foo</a></p>
  2159. ````````````````````````````````
  2160. ```````````````````````````````` example
  2161. [foo]:
  2162. /url
  2163. 'the title'
  2164. [foo]
  2165. .
  2166. <p><a href="/url" title="the title">foo</a></p>
  2167. ````````````````````````````````
  2168. ```````````````````````````````` example
  2169. [Foo*bar\]]:my_(url) 'title (with parens)'
  2170. [Foo*bar\]]
  2171. .
  2172. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2173. ````````````````````````````````
  2174. ```````````````````````````````` example
  2175. [Foo bar]:
  2176. <my url>
  2177. 'title'
  2178. [Foo bar]
  2179. .
  2180. <p><a href="my%20url" title="title">Foo bar</a></p>
  2181. ````````````````````````````````
  2182. The title may extend over multiple lines:
  2183. ```````````````````````````````` example
  2184. [foo]: /url '
  2185. title
  2186. line1
  2187. line2
  2188. '
  2189. [foo]
  2190. .
  2191. <p><a href="/url" title="
  2192. title
  2193. line1
  2194. line2
  2195. ">foo</a></p>
  2196. ````````````````````````````````
  2197. However, it may not contain a [blank line]:
  2198. ```````````````````````````````` example
  2199. [foo]: /url 'title
  2200. with blank line'
  2201. [foo]
  2202. .
  2203. <p>[foo]: /url 'title</p>
  2204. <p>with blank line'</p>
  2205. <p>[foo]</p>
  2206. ````````````````````````````````
  2207. The title may be omitted:
  2208. ```````````````````````````````` example
  2209. [foo]:
  2210. /url
  2211. [foo]
  2212. .
  2213. <p><a href="/url">foo</a></p>
  2214. ````````````````````````````````
  2215. The link destination may not be omitted:
  2216. ```````````````````````````````` example
  2217. [foo]:
  2218. [foo]
  2219. .
  2220. <p>[foo]:</p>
  2221. <p>[foo]</p>
  2222. ````````````````````````````````
  2223. However, an empty link destination may be specified using
  2224. angle brackets:
  2225. ```````````````````````````````` example
  2226. [foo]: <>
  2227. [foo]
  2228. .
  2229. <p><a href="">foo</a></p>
  2230. ````````````````````````````````
  2231. The title must be separated from the link destination by
  2232. whitespace:
  2233. ```````````````````````````````` example
  2234. [foo]: <bar>(baz)
  2235. [foo]
  2236. .
  2237. <p>[foo]: <bar>(baz)</p>
  2238. <p>[foo]</p>
  2239. ````````````````````````````````
  2240. Both title and destination can contain backslash escapes
  2241. and literal backslashes:
  2242. ```````````````````````````````` example
  2243. [foo]: /url\bar\*baz "foo\"bar\baz"
  2244. [foo]
  2245. .
  2246. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2247. ````````````````````````````````
  2248. A link can come before its corresponding definition:
  2249. ```````````````````````````````` example
  2250. [foo]
  2251. [foo]: url
  2252. .
  2253. <p><a href="url">foo</a></p>
  2254. ````````````````````````````````
  2255. If there are several matching definitions, the first one takes
  2256. precedence:
  2257. ```````````````````````````````` example
  2258. [foo]
  2259. [foo]: first
  2260. [foo]: second
  2261. .
  2262. <p><a href="first">foo</a></p>
  2263. ````````````````````````````````
  2264. As noted in the section on [Links], matching of labels is
  2265. case-insensitive (see [matches]).
  2266. ```````````````````````````````` example
  2267. [FOO]: /url
  2268. [Foo]
  2269. .
  2270. <p><a href="/url">Foo</a></p>
  2271. ````````````````````````````````
  2272. ```````````````````````````````` example
  2273. [ΑΓΩ]: /φου
  2274. [αγω]
  2275. .
  2276. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2277. ````````````````````````````````
  2278. Here is a link reference definition with no corresponding link.
  2279. It contributes nothing to the document.
  2280. ```````````````````````````````` example
  2281. [foo]: /url
  2282. .
  2283. ````````````````````````````````
  2284. Here is another one:
  2285. ```````````````````````````````` example
  2286. [
  2287. foo
  2288. ]: /url
  2289. bar
  2290. .
  2291. <p>bar</p>
  2292. ````````````````````````````````
  2293. This is not a link reference definition, because there are
  2294. [non-whitespace characters] after the title:
  2295. ```````````````````````````````` example
  2296. [foo]: /url "title" ok
  2297. .
  2298. <p>[foo]: /url &quot;title&quot; ok</p>
  2299. ````````````````````````````````
  2300. This is a link reference definition, but it has no title:
  2301. ```````````````````````````````` example
  2302. [foo]: /url
  2303. "title" ok
  2304. .
  2305. <p>&quot;title&quot; ok</p>
  2306. ````````````````````````````````
  2307. This is not a link reference definition, because it is indented
  2308. four spaces:
  2309. ```````````````````````````````` example
  2310. [foo]: /url "title"
  2311. [foo]
  2312. .
  2313. <pre><code>[foo]: /url &quot;title&quot;
  2314. </code></pre>
  2315. <p>[foo]</p>
  2316. ````````````````````````````````
  2317. This is not a link reference definition, because it occurs inside
  2318. a code block:
  2319. ```````````````````````````````` example
  2320. ```
  2321. [foo]: /url
  2322. ```
  2323. [foo]
  2324. .
  2325. <pre><code>[foo]: /url
  2326. </code></pre>
  2327. <p>[foo]</p>
  2328. ````````````````````````````````
  2329. A [link reference definition] cannot interrupt a paragraph.
  2330. ```````````````````````````````` example
  2331. Foo
  2332. [bar]: /baz
  2333. [bar]
  2334. .
  2335. <p>Foo
  2336. [bar]: /baz</p>
  2337. <p>[bar]</p>
  2338. ````````````````````````````````
  2339. However, it can directly follow other block elements, such as headings
  2340. and thematic breaks, and it need not be followed by a blank line.
  2341. ```````````````````````````````` example
  2342. # [Foo]
  2343. [foo]: /url
  2344. > bar
  2345. .
  2346. <h1><a href="/url">Foo</a></h1>
  2347. <blockquote>
  2348. <p>bar</p>
  2349. </blockquote>
  2350. ````````````````````````````````
  2351. ```````````````````````````````` example
  2352. [foo]: /url
  2353. bar
  2354. ===
  2355. [foo]
  2356. .
  2357. <h1>bar</h1>
  2358. <p><a href="/url">foo</a></p>
  2359. ````````````````````````````````
  2360. ```````````````````````````````` example
  2361. [foo]: /url
  2362. ===
  2363. [foo]
  2364. .
  2365. <p>===
  2366. <a href="/url">foo</a></p>
  2367. ````````````````````````````````
  2368. Several [link reference definitions]
  2369. can occur one after another, without intervening blank lines.
  2370. ```````````````````````````````` example
  2371. [foo]: /foo-url "foo"
  2372. [bar]: /bar-url
  2373. "bar"
  2374. [baz]: /baz-url
  2375. [foo],
  2376. [bar],
  2377. [baz]
  2378. .
  2379. <p><a href="/foo-url" title="foo">foo</a>,
  2380. <a href="/bar-url" title="bar">bar</a>,
  2381. <a href="/baz-url">baz</a></p>
  2382. ````````````````````````````````
  2383. [Link reference definitions] can occur
  2384. inside block containers, like lists and block quotations. They
  2385. affect the entire document, not just the container in which they
  2386. are defined:
  2387. ```````````````````````````````` example
  2388. [foo]
  2389. > [foo]: /url
  2390. .
  2391. <p><a href="/url">foo</a></p>
  2392. <blockquote>
  2393. </blockquote>
  2394. ````````````````````````````````
  2395. ## Paragraphs
  2396. A sequence of non-blank lines that cannot be interpreted as other
  2397. kinds of blocks forms a [paragraph](@).
  2398. The contents of the paragraph are the result of parsing the
  2399. paragraph's raw content as inlines. The paragraph's raw content
  2400. is formed by concatenating the lines and removing initial and final
  2401. [whitespace].
  2402. A simple example with two paragraphs:
  2403. ```````````````````````````````` example
  2404. aaa
  2405. bbb
  2406. .
  2407. <p>aaa</p>
  2408. <p>bbb</p>
  2409. ````````````````````````````````
  2410. Paragraphs can contain multiple lines, but no blank lines:
  2411. ```````````````````````````````` example
  2412. aaa
  2413. bbb
  2414. ccc
  2415. ddd
  2416. .
  2417. <p>aaa
  2418. bbb</p>
  2419. <p>ccc
  2420. ddd</p>
  2421. ````````````````````````````````
  2422. Multiple blank lines between paragraph have no effect:
  2423. ```````````````````````````````` example
  2424. aaa
  2425. bbb
  2426. .
  2427. <p>aaa</p>
  2428. <p>bbb</p>
  2429. ````````````````````````````````
  2430. Leading spaces are skipped:
  2431. ```````````````````````````````` example
  2432. aaa
  2433. bbb
  2434. .
  2435. <p>aaa
  2436. bbb</p>
  2437. ````````````````````````````````
  2438. Lines after the first may be indented any amount, since indented
  2439. code blocks cannot interrupt paragraphs.
  2440. ```````````````````````````````` example
  2441. aaa
  2442. bbb
  2443. ccc
  2444. .
  2445. <p>aaa
  2446. bbb
  2447. ccc</p>
  2448. ````````````````````````````````
  2449. However, the first line may be indented at most three spaces,
  2450. or an indented code block will be triggered:
  2451. ```````````````````````````````` example
  2452. aaa
  2453. bbb
  2454. .
  2455. <p>aaa
  2456. bbb</p>
  2457. ````````````````````````````````
  2458. ```````````````````````````````` example
  2459. aaa
  2460. bbb
  2461. .
  2462. <pre><code>aaa
  2463. </code></pre>
  2464. <p>bbb</p>
  2465. ````````````````````````````````
  2466. Final spaces are stripped before inline parsing, so a paragraph
  2467. that ends with two or more spaces will not end with a [hard line
  2468. break]:
  2469. ```````````````````````````````` example
  2470. aaa
  2471. bbb
  2472. .
  2473. <p>aaa<br />
  2474. bbb</p>
  2475. ````````````````````````````````
  2476. ## Blank lines
  2477. [Blank lines] between block-level elements are ignored,
  2478. except for the role they play in determining whether a [list]
  2479. is [tight] or [loose].
  2480. Blank lines at the beginning and end of the document are also ignored.
  2481. ```````````````````````````````` example
  2482. aaa
  2483. # aaa
  2484. .
  2485. <p>aaa</p>
  2486. <h1>aaa</h1>
  2487. ````````````````````````````````
  2488. # Container blocks
  2489. A [container block](#container-blocks) is a block that has other
  2490. blocks as its contents. There are two basic kinds of container blocks:
  2491. [block quotes] and [list items].
  2492. [Lists] are meta-containers for [list items].
  2493. We define the syntax for container blocks recursively. The general
  2494. form of the definition is:
  2495. > If X is a sequence of blocks, then the result of
  2496. > transforming X in such-and-such a way is a container of type Y
  2497. > with these blocks as its content.
  2498. So, we explain what counts as a block quote or list item by explaining
  2499. how these can be *generated* from their contents. This should suffice
  2500. to define the syntax, although it does not give a recipe for *parsing*
  2501. these constructions. (A recipe is provided below in the section entitled
  2502. [A parsing strategy](#appendix-a-parsing-strategy).)
  2503. ## Block quotes
  2504. A [block quote marker](@)
  2505. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2506. with a following space, or (b) a single character `>` not followed by a space.
  2507. The following rules define [block quotes]:
  2508. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2509. of blocks *Bs*, then the result of prepending a [block quote
  2510. marker] to the beginning of each line in *Ls*
  2511. is a [block quote](#block-quotes) containing *Bs*.
  2512. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2513. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2514. the initial [block quote marker] from one or
  2515. more lines in which the next [non-whitespace character] after the [block
  2516. quote marker] is [paragraph continuation
  2517. text] is a block quote with *Bs* as its content.
  2518. [Paragraph continuation text](@) is text
  2519. that will be parsed as part of the content of a paragraph, but does
  2520. not occur at the beginning of the paragraph.
  2521. 3. **Consecutiveness.** A document cannot contain two [block
  2522. quotes] in a row unless there is a [blank line] between them.
  2523. Nothing else counts as a [block quote](#block-quotes).
  2524. Here is a simple example:
  2525. ```````````````````````````````` example
  2526. > # Foo
  2527. > bar
  2528. > baz
  2529. .
  2530. <blockquote>
  2531. <h1>Foo</h1>
  2532. <p>bar
  2533. baz</p>
  2534. </blockquote>
  2535. ````````````````````````````````
  2536. The spaces after the `>` characters can be omitted:
  2537. ```````````````````````````````` example
  2538. ># Foo
  2539. >bar
  2540. > baz
  2541. .
  2542. <blockquote>
  2543. <h1>Foo</h1>
  2544. <p>bar
  2545. baz</p>
  2546. </blockquote>
  2547. ````````````````````````````````
  2548. The `>` characters can be indented 1-3 spaces:
  2549. ```````````````````````````````` example
  2550. > # Foo
  2551. > bar
  2552. > baz
  2553. .
  2554. <blockquote>
  2555. <h1>Foo</h1>
  2556. <p>bar
  2557. baz</p>
  2558. </blockquote>
  2559. ````````````````````````````````
  2560. Four spaces gives us a code block:
  2561. ```````````````````````````````` example
  2562. > # Foo
  2563. > bar
  2564. > baz
  2565. .
  2566. <pre><code>&gt; # Foo
  2567. &gt; bar
  2568. &gt; baz
  2569. </code></pre>
  2570. ````````````````````````````````
  2571. The Laziness clause allows us to omit the `>` before
  2572. [paragraph continuation text]:
  2573. ```````````````````````````````` example
  2574. > # Foo
  2575. > bar
  2576. baz
  2577. .
  2578. <blockquote>
  2579. <h1>Foo</h1>
  2580. <p>bar
  2581. baz</p>
  2582. </blockquote>
  2583. ````````````````````````````````
  2584. A block quote can contain some lazy and some non-lazy
  2585. continuation lines:
  2586. ```````````````````````````````` example
  2587. > bar
  2588. baz
  2589. > foo
  2590. .
  2591. <blockquote>
  2592. <p>bar
  2593. baz
  2594. foo</p>
  2595. </blockquote>
  2596. ````````````````````````````````
  2597. Laziness only applies to lines that would have been continuations of
  2598. paragraphs had they been prepended with [block quote markers].
  2599. For example, the `> ` cannot be omitted in the second line of
  2600. ``` markdown
  2601. > foo
  2602. > ---
  2603. ```
  2604. without changing the meaning:
  2605. ```````````````````````````````` example
  2606. > foo
  2607. ---
  2608. .
  2609. <blockquote>
  2610. <p>foo</p>
  2611. </blockquote>
  2612. <hr />
  2613. ````````````````````````````````
  2614. Similarly, if we omit the `> ` in the second line of
  2615. ``` markdown
  2616. > - foo
  2617. > - bar
  2618. ```
  2619. then the block quote ends after the first line:
  2620. ```````````````````````````````` example
  2621. > - foo
  2622. - bar
  2623. .
  2624. <blockquote>
  2625. <ul>
  2626. <li>foo</li>
  2627. </ul>
  2628. </blockquote>
  2629. <ul>
  2630. <li>bar</li>
  2631. </ul>
  2632. ````````````````````````````````
  2633. For the same reason, we can't omit the `> ` in front of
  2634. subsequent lines of an indented or fenced code block:
  2635. ```````````````````````````````` example
  2636. > foo
  2637. bar
  2638. .
  2639. <blockquote>
  2640. <pre><code>foo
  2641. </code></pre>
  2642. </blockquote>
  2643. <pre><code>bar
  2644. </code></pre>
  2645. ````````````````````````````````
  2646. ```````````````````````````````` example
  2647. > ```
  2648. foo
  2649. ```
  2650. .
  2651. <blockquote>
  2652. <pre><code></code></pre>
  2653. </blockquote>
  2654. <p>foo</p>
  2655. <pre><code></code></pre>
  2656. ````````````````````````````````
  2657. Note that in the following case, we have a [lazy
  2658. continuation line]:
  2659. ```````````````````````````````` example
  2660. > foo
  2661. - bar
  2662. .
  2663. <blockquote>
  2664. <p>foo
  2665. - bar</p>
  2666. </blockquote>
  2667. ````````````````````````````````
  2668. To see why, note that in
  2669. ```markdown
  2670. > foo
  2671. > - bar
  2672. ```
  2673. the `- bar` is indented too far to start a list, and can't
  2674. be an indented code block because indented code blocks cannot
  2675. interrupt paragraphs, so it is [paragraph continuation text].
  2676. A block quote can be empty:
  2677. ```````````````````````````````` example
  2678. >
  2679. .
  2680. <blockquote>
  2681. </blockquote>
  2682. ````````````````````````````````
  2683. ```````````````````````````````` example
  2684. >
  2685. >
  2686. >
  2687. .
  2688. <blockquote>
  2689. </blockquote>
  2690. ````````````````````````````````
  2691. A block quote can have initial or final blank lines:
  2692. ```````````````````````````````` example
  2693. >
  2694. > foo
  2695. >
  2696. .
  2697. <blockquote>
  2698. <p>foo</p>
  2699. </blockquote>
  2700. ````````````````````````````````
  2701. A blank line always separates block quotes:
  2702. ```````````````````````````````` example
  2703. > foo
  2704. > bar
  2705. .
  2706. <blockquote>
  2707. <p>foo</p>
  2708. </blockquote>
  2709. <blockquote>
  2710. <p>bar</p>
  2711. </blockquote>
  2712. ````````````````````````````````
  2713. (Most current Markdown implementations, including John Gruber's
  2714. original `Markdown.pl`, will parse this example as a single block quote
  2715. with two paragraphs. But it seems better to allow the author to decide
  2716. whether two block quotes or one are wanted.)
  2717. Consecutiveness means that if we put these block quotes together,
  2718. we get a single block quote:
  2719. ```````````````````````````````` example
  2720. > foo
  2721. > bar
  2722. .
  2723. <blockquote>
  2724. <p>foo
  2725. bar</p>
  2726. </blockquote>
  2727. ````````````````````````````````
  2728. To get a block quote with two paragraphs, use:
  2729. ```````````````````````````````` example
  2730. > foo
  2731. >
  2732. > bar
  2733. .
  2734. <blockquote>
  2735. <p>foo</p>
  2736. <p>bar</p>
  2737. </blockquote>
  2738. ````````````````````````````````
  2739. Block quotes can interrupt paragraphs:
  2740. ```````````````````````````````` example
  2741. foo
  2742. > bar
  2743. .
  2744. <p>foo</p>
  2745. <blockquote>
  2746. <p>bar</p>
  2747. </blockquote>
  2748. ````````````````````````````````
  2749. In general, blank lines are not needed before or after block
  2750. quotes:
  2751. ```````````````````````````````` example
  2752. > aaa
  2753. ***
  2754. > bbb
  2755. .
  2756. <blockquote>
  2757. <p>aaa</p>
  2758. </blockquote>
  2759. <hr />
  2760. <blockquote>
  2761. <p>bbb</p>
  2762. </blockquote>
  2763. ````````````````````````````````
  2764. However, because of laziness, a blank line is needed between
  2765. a block quote and a following paragraph:
  2766. ```````````````````````````````` example
  2767. > bar
  2768. baz
  2769. .
  2770. <blockquote>
  2771. <p>bar
  2772. baz</p>
  2773. </blockquote>
  2774. ````````````````````````````````
  2775. ```````````````````````````````` example
  2776. > bar
  2777. baz
  2778. .
  2779. <blockquote>
  2780. <p>bar</p>
  2781. </blockquote>
  2782. <p>baz</p>
  2783. ````````````````````````````````
  2784. ```````````````````````````````` example
  2785. > bar
  2786. >
  2787. baz
  2788. .
  2789. <blockquote>
  2790. <p>bar</p>
  2791. </blockquote>
  2792. <p>baz</p>
  2793. ````````````````````````````````
  2794. It is a consequence of the Laziness rule that any number
  2795. of initial `>`s may be omitted on a continuation line of a
  2796. nested block quote:
  2797. ```````````````````````````````` example
  2798. > > > foo
  2799. bar
  2800. .
  2801. <blockquote>
  2802. <blockquote>
  2803. <blockquote>
  2804. <p>foo
  2805. bar</p>
  2806. </blockquote>
  2807. </blockquote>
  2808. </blockquote>
  2809. ````````````````````````````````
  2810. ```````````````````````````````` example
  2811. >>> foo
  2812. > bar
  2813. >>baz
  2814. .
  2815. <blockquote>
  2816. <blockquote>
  2817. <blockquote>
  2818. <p>foo
  2819. bar
  2820. baz</p>
  2821. </blockquote>
  2822. </blockquote>
  2823. </blockquote>
  2824. ````````````````````````````````
  2825. When including an indented code block in a block quote,
  2826. remember that the [block quote marker] includes
  2827. both the `>` and a following space. So *five spaces* are needed after
  2828. the `>`:
  2829. ```````````````````````````````` example
  2830. > code
  2831. > not code
  2832. .
  2833. <blockquote>
  2834. <pre><code>code
  2835. </code></pre>
  2836. </blockquote>
  2837. <blockquote>
  2838. <p>not code</p>
  2839. </blockquote>
  2840. ````````````````````````````````
  2841. ## List items
  2842. A [list marker](@) is a
  2843. [bullet list marker] or an [ordered list marker].
  2844. A [bullet list marker](@)
  2845. is a `-`, `+`, or `*` character.
  2846. An [ordered list marker](@)
  2847. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2848. `.` character or a `)` character. (The reason for the length
  2849. limit is that with 10 digits we start seeing integer overflows
  2850. in some browsers.)
  2851. The following rules define [list items]:
  2852. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2853. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  2854. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2855. of prepending *M* and the following spaces to the first line of
  2856. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2857. list item with *Bs* as its contents. The type of the list item
  2858. (bullet or ordered) is determined by the type of its list marker.
  2859. If the list item is ordered, then it is also assigned a start
  2860. number, based on the ordered list marker.
  2861. Exceptions:
  2862. 1. When the first list item in a [list] interrupts
  2863. a paragraph---that is, when it starts on a line that would
  2864. otherwise count as [paragraph continuation text]---then (a)
  2865. the lines *Ls* must not begin with a blank line, and (b) if
  2866. the list item is ordered, the start number must be 1.
  2867. 2. If any line is a [thematic break][thematic breaks] then
  2868. that line is not a list item.
  2869. For example, let *Ls* be the lines
  2870. ```````````````````````````````` example
  2871. A paragraph
  2872. with two lines.
  2873. indented code
  2874. > A block quote.
  2875. .
  2876. <p>A paragraph
  2877. with two lines.</p>
  2878. <pre><code>indented code
  2879. </code></pre>
  2880. <blockquote>
  2881. <p>A block quote.</p>
  2882. </blockquote>
  2883. ````````````````````````````````
  2884. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2885. that the following is an ordered list item with start number 1,
  2886. and the same contents as *Ls*:
  2887. ```````````````````````````````` example
  2888. 1. A paragraph
  2889. with two lines.
  2890. indented code
  2891. > A block quote.
  2892. .
  2893. <ol>
  2894. <li>
  2895. <p>A paragraph
  2896. with two lines.</p>
  2897. <pre><code>indented code
  2898. </code></pre>
  2899. <blockquote>
  2900. <p>A block quote.</p>
  2901. </blockquote>
  2902. </li>
  2903. </ol>
  2904. ````````````````````````````````
  2905. The most important thing to notice is that the position of
  2906. the text after the list marker determines how much indentation
  2907. is needed in subsequent blocks in the list item. If the list
  2908. marker takes up two spaces, and there are three spaces between
  2909. the list marker and the next [non-whitespace character], then blocks
  2910. must be indented five spaces in order to fall under the list
  2911. item.
  2912. Here are some examples showing how far content must be indented to be
  2913. put under the list item:
  2914. ```````````````````````````````` example
  2915. - one
  2916. two
  2917. .
  2918. <ul>
  2919. <li>one</li>
  2920. </ul>
  2921. <p>two</p>
  2922. ````````````````````````````````
  2923. ```````````````````````````````` example
  2924. - one
  2925. two
  2926. .
  2927. <ul>
  2928. <li>
  2929. <p>one</p>
  2930. <p>two</p>
  2931. </li>
  2932. </ul>
  2933. ````````````````````````````````
  2934. ```````````````````````````````` example
  2935. - one
  2936. two
  2937. .
  2938. <ul>
  2939. <li>one</li>
  2940. </ul>
  2941. <pre><code> two
  2942. </code></pre>
  2943. ````````````````````````````````
  2944. ```````````````````````````````` example
  2945. - one
  2946. two
  2947. .
  2948. <ul>
  2949. <li>
  2950. <p>one</p>
  2951. <p>two</p>
  2952. </li>
  2953. </ul>
  2954. ````````````````````````````````
  2955. It is tempting to think of this in terms of columns: the continuation
  2956. blocks must be indented at least to the column of the first
  2957. [non-whitespace character] after the list marker. However, that is not quite right.
  2958. The spaces after the list marker determine how much relative indentation
  2959. is needed. Which column this indentation reaches will depend on
  2960. how the list item is embedded in other constructions, as shown by
  2961. this example:
  2962. ```````````````````````````````` example
  2963. > > 1. one
  2964. >>
  2965. >> two
  2966. .
  2967. <blockquote>
  2968. <blockquote>
  2969. <ol>
  2970. <li>
  2971. <p>one</p>
  2972. <p>two</p>
  2973. </li>
  2974. </ol>
  2975. </blockquote>
  2976. </blockquote>
  2977. ````````````````````````````````
  2978. Here `two` occurs in the same column as the list marker `1.`,
  2979. but is actually contained in the list item, because there is
  2980. sufficient indentation after the last containing blockquote marker.
  2981. The converse is also possible. In the following example, the word `two`
  2982. occurs far to the right of the initial text of the list item, `one`, but
  2983. it is not considered part of the list item, because it is not indented
  2984. far enough past the blockquote marker:
  2985. ```````````````````````````````` example
  2986. >>- one
  2987. >>
  2988. > > two
  2989. .
  2990. <blockquote>
  2991. <blockquote>
  2992. <ul>
  2993. <li>one</li>
  2994. </ul>
  2995. <p>two</p>
  2996. </blockquote>
  2997. </blockquote>
  2998. ````````````````````````````````
  2999. Note that at least one space is needed between the list marker and
  3000. any following content, so these are not list items:
  3001. ```````````````````````````````` example
  3002. -one
  3003. 2.two
  3004. .
  3005. <p>-one</p>
  3006. <p>2.two</p>
  3007. ````````````````````````````````
  3008. A list item may contain blocks that are separated by more than
  3009. one blank line.
  3010. ```````````````````````````````` example
  3011. - foo
  3012. bar
  3013. .
  3014. <ul>
  3015. <li>
  3016. <p>foo</p>
  3017. <p>bar</p>
  3018. </li>
  3019. </ul>
  3020. ````````````````````````````````
  3021. A list item may contain any kind of block:
  3022. ```````````````````````````````` example
  3023. 1. foo
  3024. ```
  3025. bar
  3026. ```
  3027. baz
  3028. > bam
  3029. .
  3030. <ol>
  3031. <li>
  3032. <p>foo</p>
  3033. <pre><code>bar
  3034. </code></pre>
  3035. <p>baz</p>
  3036. <blockquote>
  3037. <p>bam</p>
  3038. </blockquote>
  3039. </li>
  3040. </ol>
  3041. ````````````````````````````````
  3042. A list item that contains an indented code block will preserve
  3043. empty lines within the code block verbatim.
  3044. ```````````````````````````````` example
  3045. - Foo
  3046. bar
  3047. baz
  3048. .
  3049. <ul>
  3050. <li>
  3051. <p>Foo</p>
  3052. <pre><code>bar
  3053. baz
  3054. </code></pre>
  3055. </li>
  3056. </ul>
  3057. ````````````````````````````````
  3058. Note that ordered list start numbers must be nine digits or less:
  3059. ```````````````````````````````` example
  3060. 123456789. ok
  3061. .
  3062. <ol start="123456789">
  3063. <li>ok</li>
  3064. </ol>
  3065. ````````````````````````````````
  3066. ```````````````````````````````` example
  3067. 1234567890. not ok
  3068. .
  3069. <p>1234567890. not ok</p>
  3070. ````````````````````````````````
  3071. A start number may begin with 0s:
  3072. ```````````````````````````````` example
  3073. 0. ok
  3074. .
  3075. <ol start="0">
  3076. <li>ok</li>
  3077. </ol>
  3078. ````````````````````````````````
  3079. ```````````````````````````````` example
  3080. 003. ok
  3081. .
  3082. <ol start="3">
  3083. <li>ok</li>
  3084. </ol>
  3085. ````````````````````````````````
  3086. A start number may not be negative:
  3087. ```````````````````````````````` example
  3088. -1. not ok
  3089. .
  3090. <p>-1. not ok</p>
  3091. ````````````````````````````````
  3092. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3093. constitute a sequence of blocks *Bs* starting with an indented code
  3094. block, and *M* is a list marker of width *W* followed by
  3095. one space, then the result of prepending *M* and the following
  3096. space to the first line of *Ls*, and indenting subsequent lines of
  3097. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3098. If a line is empty, then it need not be indented. The type of the
  3099. list item (bullet or ordered) is determined by the type of its list
  3100. marker. If the list item is ordered, then it is also assigned a
  3101. start number, based on the ordered list marker.
  3102. An indented code block will have to be indented four spaces beyond
  3103. the edge of the region where text will be included in the list item.
  3104. In the following case that is 6 spaces:
  3105. ```````````````````````````````` example
  3106. - foo
  3107. bar
  3108. .
  3109. <ul>
  3110. <li>
  3111. <p>foo</p>
  3112. <pre><code>bar
  3113. </code></pre>
  3114. </li>
  3115. </ul>
  3116. ````````````````````````````````
  3117. And in this case it is 11 spaces:
  3118. ```````````````````````````````` example
  3119. 10. foo
  3120. bar
  3121. .
  3122. <ol start="10">
  3123. <li>
  3124. <p>foo</p>
  3125. <pre><code>bar
  3126. </code></pre>
  3127. </li>
  3128. </ol>
  3129. ````````````````````````````````
  3130. If the *first* block in the list item is an indented code block,
  3131. then by rule #2, the contents must be indented *one* space after the
  3132. list marker:
  3133. ```````````````````````````````` example
  3134. indented code
  3135. paragraph
  3136. more code
  3137. .
  3138. <pre><code>indented code
  3139. </code></pre>
  3140. <p>paragraph</p>
  3141. <pre><code>more code
  3142. </code></pre>
  3143. ````````````````````````````````
  3144. ```````````````````````````````` example
  3145. 1. indented code
  3146. paragraph
  3147. more code
  3148. .
  3149. <ol>
  3150. <li>
  3151. <pre><code>indented code
  3152. </code></pre>
  3153. <p>paragraph</p>
  3154. <pre><code>more code
  3155. </code></pre>
  3156. </li>
  3157. </ol>
  3158. ````````````````````````````````
  3159. Note that an additional space indent is interpreted as space
  3160. inside the code block:
  3161. ```````````````````````````````` example
  3162. 1. indented code
  3163. paragraph
  3164. more code
  3165. .
  3166. <ol>
  3167. <li>
  3168. <pre><code> indented code
  3169. </code></pre>
  3170. <p>paragraph</p>
  3171. <pre><code>more code
  3172. </code></pre>
  3173. </li>
  3174. </ol>
  3175. ````````````````````````````````
  3176. Note that rules #1 and #2 only apply to two cases: (a) cases
  3177. in which the lines to be included in a list item begin with a
  3178. [non-whitespace character], and (b) cases in which
  3179. they begin with an indented code
  3180. block. In a case like the following, where the first block begins with
  3181. a three-space indent, the rules do not allow us to form a list item by
  3182. indenting the whole thing and prepending a list marker:
  3183. ```````````````````````````````` example
  3184. foo
  3185. bar
  3186. .
  3187. <p>foo</p>
  3188. <p>bar</p>
  3189. ````````````````````````````````
  3190. ```````````````````````````````` example
  3191. - foo
  3192. bar
  3193. .
  3194. <ul>
  3195. <li>foo</li>
  3196. </ul>
  3197. <p>bar</p>
  3198. ````````````````````````````````
  3199. This is not a significant restriction, because when a block begins
  3200. with 1-3 spaces indent, the indentation can always be removed without
  3201. a change in interpretation, allowing rule #1 to be applied. So, in
  3202. the above case:
  3203. ```````````````````````````````` example
  3204. - foo
  3205. bar
  3206. .
  3207. <ul>
  3208. <li>
  3209. <p>foo</p>
  3210. <p>bar</p>
  3211. </li>
  3212. </ul>
  3213. ````````````````````````````````
  3214. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3215. starting with a single [blank line] constitute a (possibly empty)
  3216. sequence of blocks *Bs*, not separated from each other by more than
  3217. one blank line, and *M* is a list marker of width *W*,
  3218. then the result of prepending *M* to the first line of *Ls*, and
  3219. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3220. item with *Bs* as its contents.
  3221. If a line is empty, then it need not be indented. The type of the
  3222. list item (bullet or ordered) is determined by the type of its list
  3223. marker. If the list item is ordered, then it is also assigned a
  3224. start number, based on the ordered list marker.
  3225. Here are some list items that start with a blank line but are not empty:
  3226. ```````````````````````````````` example
  3227. -
  3228. foo
  3229. -
  3230. ```
  3231. bar
  3232. ```
  3233. -
  3234. baz
  3235. .
  3236. <ul>
  3237. <li>foo</li>
  3238. <li>
  3239. <pre><code>bar
  3240. </code></pre>
  3241. </li>
  3242. <li>
  3243. <pre><code>baz
  3244. </code></pre>
  3245. </li>
  3246. </ul>
  3247. ````````````````````````````````
  3248. When the list item starts with a blank line, the number of spaces
  3249. following the list marker doesn't change the required indentation:
  3250. ```````````````````````````````` example
  3251. -
  3252. foo
  3253. .
  3254. <ul>
  3255. <li>foo</li>
  3256. </ul>
  3257. ````````````````````````````````
  3258. A list item can begin with at most one blank line.
  3259. In the following example, `foo` is not part of the list
  3260. item:
  3261. ```````````````````````````````` example
  3262. -
  3263. foo
  3264. .
  3265. <ul>
  3266. <li></li>
  3267. </ul>
  3268. <p>foo</p>
  3269. ````````````````````````````````
  3270. Here is an empty bullet list item:
  3271. ```````````````````````````````` example
  3272. - foo
  3273. -
  3274. - bar
  3275. .
  3276. <ul>
  3277. <li>foo</li>
  3278. <li></li>
  3279. <li>bar</li>
  3280. </ul>
  3281. ````````````````````````````````
  3282. It does not matter whether there are spaces following the [list marker]:
  3283. ```````````````````````````````` example
  3284. - foo
  3285. -
  3286. - bar
  3287. .
  3288. <ul>
  3289. <li>foo</li>
  3290. <li></li>
  3291. <li>bar</li>
  3292. </ul>
  3293. ````````````````````````````````
  3294. Here is an empty ordered list item:
  3295. ```````````````````````````````` example
  3296. 1. foo
  3297. 2.
  3298. 3. bar
  3299. .
  3300. <ol>
  3301. <li>foo</li>
  3302. <li></li>
  3303. <li>bar</li>
  3304. </ol>
  3305. ````````````````````````````````
  3306. A list may start or end with an empty list item:
  3307. ```````````````````````````````` example
  3308. *
  3309. .
  3310. <ul>
  3311. <li></li>
  3312. </ul>
  3313. ````````````````````````````````
  3314. However, an empty list item cannot interrupt a paragraph:
  3315. ```````````````````````````````` example
  3316. foo
  3317. *
  3318. foo
  3319. 1.
  3320. .
  3321. <p>foo
  3322. *</p>
  3323. <p>foo
  3324. 1.</p>
  3325. ````````````````````````````````
  3326. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3327. according to rule #1, #2, or #3, then the result of indenting each line
  3328. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3329. list item with the same contents and attributes. If a line is
  3330. empty, then it need not be indented.
  3331. Indented one space:
  3332. ```````````````````````````````` example
  3333. 1. A paragraph
  3334. with two lines.
  3335. indented code
  3336. > A block quote.
  3337. .
  3338. <ol>
  3339. <li>
  3340. <p>A paragraph
  3341. with two lines.</p>
  3342. <pre><code>indented code
  3343. </code></pre>
  3344. <blockquote>
  3345. <p>A block quote.</p>
  3346. </blockquote>
  3347. </li>
  3348. </ol>
  3349. ````````````````````````````````
  3350. Indented two spaces:
  3351. ```````````````````````````````` example
  3352. 1. A paragraph
  3353. with two lines.
  3354. indented code
  3355. > A block quote.
  3356. .
  3357. <ol>
  3358. <li>
  3359. <p>A paragraph
  3360. with two lines.</p>
  3361. <pre><code>indented code
  3362. </code></pre>
  3363. <blockquote>
  3364. <p>A block quote.</p>
  3365. </blockquote>
  3366. </li>
  3367. </ol>
  3368. ````````````````````````````````
  3369. Indented three spaces:
  3370. ```````````````````````````````` example
  3371. 1. A paragraph
  3372. with two lines.
  3373. indented code
  3374. > A block quote.
  3375. .
  3376. <ol>
  3377. <li>
  3378. <p>A paragraph
  3379. with two lines.</p>
  3380. <pre><code>indented code
  3381. </code></pre>
  3382. <blockquote>
  3383. <p>A block quote.</p>
  3384. </blockquote>
  3385. </li>
  3386. </ol>
  3387. ````````````````````````````````
  3388. Four spaces indent gives a code block:
  3389. ```````````````````````````````` example
  3390. 1. A paragraph
  3391. with two lines.
  3392. indented code
  3393. > A block quote.
  3394. .
  3395. <pre><code>1. A paragraph
  3396. with two lines.
  3397. indented code
  3398. &gt; A block quote.
  3399. </code></pre>
  3400. ````````````````````````````````
  3401. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3402. item](#list-items) with contents *Bs*, then the result of deleting
  3403. some or all of the indentation from one or more lines in which the
  3404. next [non-whitespace character] after the indentation is
  3405. [paragraph continuation text] is a
  3406. list item with the same contents and attributes. The unindented
  3407. lines are called
  3408. [lazy continuation line](@)s.
  3409. Here is an example with [lazy continuation lines]:
  3410. ```````````````````````````````` example
  3411. 1. A paragraph
  3412. with two lines.
  3413. indented code
  3414. > A block quote.
  3415. .
  3416. <ol>
  3417. <li>
  3418. <p>A paragraph
  3419. with two lines.</p>
  3420. <pre><code>indented code
  3421. </code></pre>
  3422. <blockquote>
  3423. <p>A block quote.</p>
  3424. </blockquote>
  3425. </li>
  3426. </ol>
  3427. ````````````````````````````````
  3428. Indentation can be partially deleted:
  3429. ```````````````````````````````` example
  3430. 1. A paragraph
  3431. with two lines.
  3432. .
  3433. <ol>
  3434. <li>A paragraph
  3435. with two lines.</li>
  3436. </ol>
  3437. ````````````````````````````````
  3438. These examples show how laziness can work in nested structures:
  3439. ```````````````````````````````` example
  3440. > 1. > Blockquote
  3441. continued here.
  3442. .
  3443. <blockquote>
  3444. <ol>
  3445. <li>
  3446. <blockquote>
  3447. <p>Blockquote
  3448. continued here.</p>
  3449. </blockquote>
  3450. </li>
  3451. </ol>
  3452. </blockquote>
  3453. ````````````````````````````````
  3454. ```````````````````````````````` example
  3455. > 1. > Blockquote
  3456. > continued here.
  3457. .
  3458. <blockquote>
  3459. <ol>
  3460. <li>
  3461. <blockquote>
  3462. <p>Blockquote
  3463. continued here.</p>
  3464. </blockquote>
  3465. </li>
  3466. </ol>
  3467. </blockquote>
  3468. ````````````````````````````````
  3469. 6. **That's all.** Nothing that is not counted as a list item by rules
  3470. #1--5 counts as a [list item](#list-items).
  3471. The rules for sublists follow from the general rules
  3472. [above][List items]. A sublist must be indented the same number
  3473. of spaces a paragraph would need to be in order to be included
  3474. in the list item.
  3475. So, in this case we need two spaces indent:
  3476. ```````````````````````````````` example
  3477. - foo
  3478. - bar
  3479. - baz
  3480. - boo
  3481. .
  3482. <ul>
  3483. <li>foo
  3484. <ul>
  3485. <li>bar
  3486. <ul>
  3487. <li>baz
  3488. <ul>
  3489. <li>boo</li>
  3490. </ul>
  3491. </li>
  3492. </ul>
  3493. </li>
  3494. </ul>
  3495. </li>
  3496. </ul>
  3497. ````````````````````````````````
  3498. One is not enough:
  3499. ```````````````````````````````` example
  3500. - foo
  3501. - bar
  3502. - baz
  3503. - boo
  3504. .
  3505. <ul>
  3506. <li>foo</li>
  3507. <li>bar</li>
  3508. <li>baz</li>
  3509. <li>boo</li>
  3510. </ul>
  3511. ````````````````````````````````
  3512. Here we need four, because the list marker is wider:
  3513. ```````````````````````````````` example
  3514. 10) foo
  3515. - bar
  3516. .
  3517. <ol start="10">
  3518. <li>foo
  3519. <ul>
  3520. <li>bar</li>
  3521. </ul>
  3522. </li>
  3523. </ol>
  3524. ````````````````````````````````
  3525. Three is not enough:
  3526. ```````````````````````````````` example
  3527. 10) foo
  3528. - bar
  3529. .
  3530. <ol start="10">
  3531. <li>foo</li>
  3532. </ol>
  3533. <ul>
  3534. <li>bar</li>
  3535. </ul>
  3536. ````````````````````````````````
  3537. A list may be the first block in a list item:
  3538. ```````````````````````````````` example
  3539. - - foo
  3540. .
  3541. <ul>
  3542. <li>
  3543. <ul>
  3544. <li>foo</li>
  3545. </ul>
  3546. </li>
  3547. </ul>
  3548. ````````````````````````````````
  3549. ```````````````````````````````` example
  3550. 1. - 2. foo
  3551. .
  3552. <ol>
  3553. <li>
  3554. <ul>
  3555. <li>
  3556. <ol start="2">
  3557. <li>foo</li>
  3558. </ol>
  3559. </li>
  3560. </ul>
  3561. </li>
  3562. </ol>
  3563. ````````````````````````````````
  3564. A list item can contain a heading:
  3565. ```````````````````````````````` example
  3566. - # Foo
  3567. - Bar
  3568. ---
  3569. baz
  3570. .
  3571. <ul>
  3572. <li>
  3573. <h1>Foo</h1>
  3574. </li>
  3575. <li>
  3576. <h2>Bar</h2>
  3577. baz</li>
  3578. </ul>
  3579. ````````````````````````````````
  3580. ### Motivation
  3581. John Gruber's Markdown spec says the following about list items:
  3582. 1. "List markers typically start at the left margin, but may be indented
  3583. by up to three spaces. List markers must be followed by one or more
  3584. spaces or a tab."
  3585. 2. "To make lists look nice, you can wrap items with hanging indents....
  3586. But if you don't want to, you don't have to."
  3587. 3. "List items may consist of multiple paragraphs. Each subsequent
  3588. paragraph in a list item must be indented by either 4 spaces or one
  3589. tab."
  3590. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3591. but here again, Markdown will allow you to be lazy."
  3592. 5. "To put a blockquote within a list item, the blockquote's `>`
  3593. delimiters need to be indented."
  3594. 6. "To put a code block within a list item, the code block needs to be
  3595. indented twice — 8 spaces or two tabs."
  3596. These rules specify that a paragraph under a list item must be indented
  3597. four spaces (presumably, from the left margin, rather than the start of
  3598. the list marker, but this is not said), and that code under a list item
  3599. must be indented eight spaces instead of the usual four. They also say
  3600. that a block quote must be indented, but not by how much; however, the
  3601. example given has four spaces indentation. Although nothing is said
  3602. about other kinds of block-level content, it is certainly reasonable to
  3603. infer that *all* block elements under a list item, including other
  3604. lists, must be indented four spaces. This principle has been called the
  3605. *four-space rule*.
  3606. The four-space rule is clear and principled, and if the reference
  3607. implementation `Markdown.pl` had followed it, it probably would have
  3608. become the standard. However, `Markdown.pl` allowed paragraphs and
  3609. sublists to start with only two spaces indentation, at least on the
  3610. outer level. Worse, its behavior was inconsistent: a sublist of an
  3611. outer-level list needed two spaces indentation, but a sublist of this
  3612. sublist needed three spaces. It is not surprising, then, that different
  3613. implementations of Markdown have developed very different rules for
  3614. determining what comes under a list item. (Pandoc and python-Markdown,
  3615. for example, stuck with Gruber's syntax description and the four-space
  3616. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3617. followed `Markdown.pl`'s behavior more closely.)
  3618. Unfortunately, given the divergences between implementations, there
  3619. is no way to give a spec for list items that will be guaranteed not
  3620. to break any existing documents. However, the spec given here should
  3621. correctly handle lists formatted with either the four-space rule or
  3622. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3623. in a way that is natural for a human to read.
  3624. The strategy here is to let the width and indentation of the list marker
  3625. determine the indentation necessary for blocks to fall under the list
  3626. item, rather than having a fixed and arbitrary number. The writer can
  3627. think of the body of the list item as a unit which gets indented to the
  3628. right enough to fit the list marker (and any indentation on the list
  3629. marker). (The laziness rule, #5, then allows continuation lines to be
  3630. unindented if needed.)
  3631. This rule is superior, we claim, to any rule requiring a fixed level of
  3632. indentation from the margin. The four-space rule is clear but
  3633. unnatural. It is quite unintuitive that
  3634. ``` markdown
  3635. - foo
  3636. bar
  3637. - baz
  3638. ```
  3639. should be parsed as two lists with an intervening paragraph,
  3640. ``` html
  3641. <ul>
  3642. <li>foo</li>
  3643. </ul>
  3644. <p>bar</p>
  3645. <ul>
  3646. <li>baz</li>
  3647. </ul>
  3648. ```
  3649. as the four-space rule demands, rather than a single list,
  3650. ``` html
  3651. <ul>
  3652. <li>
  3653. <p>foo</p>
  3654. <p>bar</p>
  3655. <ul>
  3656. <li>baz</li>
  3657. </ul>
  3658. </li>
  3659. </ul>
  3660. ```
  3661. The choice of four spaces is arbitrary. It can be learned, but it is
  3662. not likely to be guessed, and it trips up beginners regularly.
  3663. Would it help to adopt a two-space rule? The problem is that such
  3664. a rule, together with the rule allowing 1--3 spaces indentation of the
  3665. initial list marker, allows text that is indented *less than* the
  3666. original list marker to be included in the list item. For example,
  3667. `Markdown.pl` parses
  3668. ``` markdown
  3669. - one
  3670. two
  3671. ```
  3672. as a single list item, with `two` a continuation paragraph:
  3673. ``` html
  3674. <ul>
  3675. <li>
  3676. <p>one</p>
  3677. <p>two</p>
  3678. </li>
  3679. </ul>
  3680. ```
  3681. and similarly
  3682. ``` markdown
  3683. > - one
  3684. >
  3685. > two
  3686. ```
  3687. as
  3688. ``` html
  3689. <blockquote>
  3690. <ul>
  3691. <li>
  3692. <p>one</p>
  3693. <p>two</p>
  3694. </li>
  3695. </ul>
  3696. </blockquote>
  3697. ```
  3698. This is extremely unintuitive.
  3699. Rather than requiring a fixed indent from the margin, we could require
  3700. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3701. may itself be indented). This proposal would remove the last anomaly
  3702. discussed. Unlike the spec presented above, it would count the following
  3703. as a list item with a subparagraph, even though the paragraph `bar`
  3704. is not indented as far as the first paragraph `foo`:
  3705. ``` markdown
  3706. 10. foo
  3707. bar
  3708. ```
  3709. Arguably this text does read like a list item with `bar` as a subparagraph,
  3710. which may count in favor of the proposal. However, on this proposal indented
  3711. code would have to be indented six spaces after the list marker. And this
  3712. would break a lot of existing Markdown, which has the pattern:
  3713. ``` markdown
  3714. 1. foo
  3715. indented code
  3716. ```
  3717. where the code is indented eight spaces. The spec above, by contrast, will
  3718. parse this text as expected, since the code block's indentation is measured
  3719. from the beginning of `foo`.
  3720. The one case that needs special treatment is a list item that *starts*
  3721. with indented code. How much indentation is required in that case, since
  3722. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3723. that in such cases, we require one space indentation from the list marker
  3724. (and then the normal four spaces for the indented code). This will match the
  3725. four-space rule in cases where the list marker plus its initial indentation
  3726. takes four spaces (a common case), but diverge in other cases.
  3727. ## Lists
  3728. A [list](@) is a sequence of one or more
  3729. list items [of the same type]. The list items
  3730. may be separated by any number of blank lines.
  3731. Two list items are [of the same type](@)
  3732. if they begin with a [list marker] of the same type.
  3733. Two list markers are of the
  3734. same type if (a) they are bullet list markers using the same character
  3735. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3736. delimiter (either `.` or `)`).
  3737. A list is an [ordered list](@)
  3738. if its constituent list items begin with
  3739. [ordered list markers], and a
  3740. [bullet list](@) if its constituent list
  3741. items begin with [bullet list markers].
  3742. The [start number](@)
  3743. of an [ordered list] is determined by the list number of
  3744. its initial list item. The numbers of subsequent list items are
  3745. disregarded.
  3746. A list is [loose](@) if any of its constituent
  3747. list items are separated by blank lines, or if any of its constituent
  3748. list items directly contain two block-level elements with a blank line
  3749. between them. Otherwise a list is [tight](@).
  3750. (The difference in HTML output is that paragraphs in a loose list are
  3751. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3752. Changing the bullet or ordered list delimiter starts a new list:
  3753. ```````````````````````````````` example
  3754. - foo
  3755. - bar
  3756. + baz
  3757. .
  3758. <ul>
  3759. <li>foo</li>
  3760. <li>bar</li>
  3761. </ul>
  3762. <ul>
  3763. <li>baz</li>
  3764. </ul>
  3765. ````````````````````````````````
  3766. ```````````````````````````````` example
  3767. 1. foo
  3768. 2. bar
  3769. 3) baz
  3770. .
  3771. <ol>
  3772. <li>foo</li>
  3773. <li>bar</li>
  3774. </ol>
  3775. <ol start="3">
  3776. <li>baz</li>
  3777. </ol>
  3778. ````````````````````````````````
  3779. In CommonMark, a list can interrupt a paragraph. That is,
  3780. no blank line is needed to separate a paragraph from a following
  3781. list:
  3782. ```````````````````````````````` example
  3783. Foo
  3784. - bar
  3785. - baz
  3786. .
  3787. <p>Foo</p>
  3788. <ul>
  3789. <li>bar</li>
  3790. <li>baz</li>
  3791. </ul>
  3792. ````````````````````````````````
  3793. `Markdown.pl` does not allow this, through fear of triggering a list
  3794. via a numeral in a hard-wrapped line:
  3795. ``` markdown
  3796. The number of windows in my house is
  3797. 14. The number of doors is 6.
  3798. ```
  3799. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3800. interrupt a paragraph, even though the same considerations might
  3801. apply.
  3802. In CommonMark, we do allow lists to interrupt paragraphs, for
  3803. two reasons. First, it is natural and not uncommon for people
  3804. to start lists without blank lines:
  3805. ``` markdown
  3806. I need to buy
  3807. - new shoes
  3808. - a coat
  3809. - a plane ticket
  3810. ```
  3811. Second, we are attracted to a
  3812. > [principle of uniformity](@):
  3813. > if a chunk of text has a certain
  3814. > meaning, it will continue to have the same meaning when put into a
  3815. > container block (such as a list item or blockquote).
  3816. (Indeed, the spec for [list items] and [block quotes] presupposes
  3817. this principle.) This principle implies that if
  3818. ``` markdown
  3819. * I need to buy
  3820. - new shoes
  3821. - a coat
  3822. - a plane ticket
  3823. ```
  3824. is a list item containing a paragraph followed by a nested sublist,
  3825. as all Markdown implementations agree it is (though the paragraph
  3826. may be rendered without `<p>` tags, since the list is "tight"),
  3827. then
  3828. ``` markdown
  3829. I need to buy
  3830. - new shoes
  3831. - a coat
  3832. - a plane ticket
  3833. ```
  3834. by itself should be a paragraph followed by a nested sublist.
  3835. Since it is well established Markdown practice to allow lists to
  3836. interrupt paragraphs inside list items, the [principle of
  3837. uniformity] requires us to allow this outside list items as
  3838. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3839. takes a different approach, requiring blank lines before lists
  3840. even inside other list items.)
  3841. In order to solve of unwanted lists in paragraphs with
  3842. hard-wrapped numerals, we allow only lists starting with `1` to
  3843. interrupt paragraphs. Thus,
  3844. ```````````````````````````````` example
  3845. The number of windows in my house is
  3846. 14. The number of doors is 6.
  3847. .
  3848. <p>The number of windows in my house is
  3849. 14. The number of doors is 6.</p>
  3850. ````````````````````````````````
  3851. We may still get an unintended result in cases like
  3852. ```````````````````````````````` example
  3853. The number of windows in my house is
  3854. 1. The number of doors is 6.
  3855. .
  3856. <p>The number of windows in my house is</p>
  3857. <ol>
  3858. <li>The number of doors is 6.</li>
  3859. </ol>
  3860. ````````````````````````````````
  3861. but this rule should prevent most spurious list captures.
  3862. There can be any number of blank lines between items:
  3863. ```````````````````````````````` example
  3864. - foo
  3865. - bar
  3866. - baz
  3867. .
  3868. <ul>
  3869. <li>
  3870. <p>foo</p>
  3871. </li>
  3872. <li>
  3873. <p>bar</p>
  3874. </li>
  3875. <li>
  3876. <p>baz</p>
  3877. </li>
  3878. </ul>
  3879. ````````````````````````````````
  3880. ```````````````````````````````` example
  3881. - foo
  3882. - bar
  3883. - baz
  3884. bim
  3885. .
  3886. <ul>
  3887. <li>foo
  3888. <ul>
  3889. <li>bar
  3890. <ul>
  3891. <li>
  3892. <p>baz</p>
  3893. <p>bim</p>
  3894. </li>
  3895. </ul>
  3896. </li>
  3897. </ul>
  3898. </li>
  3899. </ul>
  3900. ````````````````````````````````
  3901. To separate consecutive lists of the same type, or to separate a
  3902. list from an indented code block that would otherwise be parsed
  3903. as a subparagraph of the final list item, you can insert a blank HTML
  3904. comment:
  3905. ```````````````````````````````` example
  3906. - foo
  3907. - bar
  3908. <!-- -->
  3909. - baz
  3910. - bim
  3911. .
  3912. <ul>
  3913. <li>foo</li>
  3914. <li>bar</li>
  3915. </ul>
  3916. <!-- -->
  3917. <ul>
  3918. <li>baz</li>
  3919. <li>bim</li>
  3920. </ul>
  3921. ````````````````````````````````
  3922. ```````````````````````````````` example
  3923. - foo
  3924. notcode
  3925. - foo
  3926. <!-- -->
  3927. code
  3928. .
  3929. <ul>
  3930. <li>
  3931. <p>foo</p>
  3932. <p>notcode</p>
  3933. </li>
  3934. <li>
  3935. <p>foo</p>
  3936. </li>
  3937. </ul>
  3938. <!-- -->
  3939. <pre><code>code
  3940. </code></pre>
  3941. ````````````````````````````````
  3942. List items need not be indented to the same level. The following
  3943. list items will be treated as items at the same list level,
  3944. since none is indented enough to belong to the previous list
  3945. item:
  3946. ```````````````````````````````` example
  3947. - a
  3948. - b
  3949. - c
  3950. - d
  3951. - e
  3952. - f
  3953. - g
  3954. .
  3955. <ul>
  3956. <li>a</li>
  3957. <li>b</li>
  3958. <li>c</li>
  3959. <li>d</li>
  3960. <li>e</li>
  3961. <li>f</li>
  3962. <li>g</li>
  3963. </ul>
  3964. ````````````````````````````````
  3965. ```````````````````````````````` example
  3966. 1. a
  3967. 2. b
  3968. 3. c
  3969. .
  3970. <ol>
  3971. <li>
  3972. <p>a</p>
  3973. </li>
  3974. <li>
  3975. <p>b</p>
  3976. </li>
  3977. <li>
  3978. <p>c</p>
  3979. </li>
  3980. </ol>
  3981. ````````````````````````````````
  3982. Note, however, that list items may not be indented more than
  3983. three spaces. Here `- e` is treated as a paragraph continuation
  3984. line, because it is indented more than three spaces:
  3985. ```````````````````````````````` example
  3986. - a
  3987. - b
  3988. - c
  3989. - d
  3990. - e
  3991. .
  3992. <ul>
  3993. <li>a</li>
  3994. <li>b</li>
  3995. <li>c</li>
  3996. <li>d
  3997. - e</li>
  3998. </ul>
  3999. ````````````````````````````````
  4000. And here, `3. c` is treated as in indented code block,
  4001. because it is indented four spaces and preceded by a
  4002. blank line.
  4003. ```````````````````````````````` example
  4004. 1. a
  4005. 2. b
  4006. 3. c
  4007. .
  4008. <ol>
  4009. <li>
  4010. <p>a</p>
  4011. </li>
  4012. <li>
  4013. <p>b</p>
  4014. </li>
  4015. </ol>
  4016. <pre><code>3. c
  4017. </code></pre>
  4018. ````````````````````````````````
  4019. This is a loose list, because there is a blank line between
  4020. two of the list items:
  4021. ```````````````````````````````` example
  4022. - a
  4023. - b
  4024. - c
  4025. .
  4026. <ul>
  4027. <li>
  4028. <p>a</p>
  4029. </li>
  4030. <li>
  4031. <p>b</p>
  4032. </li>
  4033. <li>
  4034. <p>c</p>
  4035. </li>
  4036. </ul>
  4037. ````````````````````````````````
  4038. So is this, with a empty second item:
  4039. ```````````````````````````````` example
  4040. * a
  4041. *
  4042. * c
  4043. .
  4044. <ul>
  4045. <li>
  4046. <p>a</p>
  4047. </li>
  4048. <li></li>
  4049. <li>
  4050. <p>c</p>
  4051. </li>
  4052. </ul>
  4053. ````````````````````````````````
  4054. These are loose lists, even though there is no space between the items,
  4055. because one of the items directly contains two block-level elements
  4056. with a blank line between them:
  4057. ```````````````````````````````` example
  4058. - a
  4059. - b
  4060. c
  4061. - d
  4062. .
  4063. <ul>
  4064. <li>
  4065. <p>a</p>
  4066. </li>
  4067. <li>
  4068. <p>b</p>
  4069. <p>c</p>
  4070. </li>
  4071. <li>
  4072. <p>d</p>
  4073. </li>
  4074. </ul>
  4075. ````````````````````````````````
  4076. ```````````````````````````````` example
  4077. - a
  4078. - b
  4079. [ref]: /url
  4080. - d
  4081. .
  4082. <ul>
  4083. <li>
  4084. <p>a</p>
  4085. </li>
  4086. <li>
  4087. <p>b</p>
  4088. </li>
  4089. <li>
  4090. <p>d</p>
  4091. </li>
  4092. </ul>
  4093. ````````````````````````````````
  4094. This is a tight list, because the blank lines are in a code block:
  4095. ```````````````````````````````` example
  4096. - a
  4097. - ```
  4098. b
  4099. ```
  4100. - c
  4101. .
  4102. <ul>
  4103. <li>a</li>
  4104. <li>
  4105. <pre><code>b
  4106. </code></pre>
  4107. </li>
  4108. <li>c</li>
  4109. </ul>
  4110. ````````````````````````````````
  4111. This is a tight list, because the blank line is between two
  4112. paragraphs of a sublist. So the sublist is loose while
  4113. the outer list is tight:
  4114. ```````````````````````````````` example
  4115. - a
  4116. - b
  4117. c
  4118. - d
  4119. .
  4120. <ul>
  4121. <li>a
  4122. <ul>
  4123. <li>
  4124. <p>b</p>
  4125. <p>c</p>
  4126. </li>
  4127. </ul>
  4128. </li>
  4129. <li>d</li>
  4130. </ul>
  4131. ````````````````````````````````
  4132. This is a tight list, because the blank line is inside the
  4133. block quote:
  4134. ```````````````````````````````` example
  4135. * a
  4136. > b
  4137. >
  4138. * c
  4139. .
  4140. <ul>
  4141. <li>a
  4142. <blockquote>
  4143. <p>b</p>
  4144. </blockquote>
  4145. </li>
  4146. <li>c</li>
  4147. </ul>
  4148. ````````````````````````````````
  4149. This list is tight, because the consecutive block elements
  4150. are not separated by blank lines:
  4151. ```````````````````````````````` example
  4152. - a
  4153. > b
  4154. ```
  4155. c
  4156. ```
  4157. - d
  4158. .
  4159. <ul>
  4160. <li>a
  4161. <blockquote>
  4162. <p>b</p>
  4163. </blockquote>
  4164. <pre><code>c
  4165. </code></pre>
  4166. </li>
  4167. <li>d</li>
  4168. </ul>
  4169. ````````````````````````````````
  4170. A single-paragraph list is tight:
  4171. ```````````````````````````````` example
  4172. - a
  4173. .
  4174. <ul>
  4175. <li>a</li>
  4176. </ul>
  4177. ````````````````````````````````
  4178. ```````````````````````````````` example
  4179. - a
  4180. - b
  4181. .
  4182. <ul>
  4183. <li>a
  4184. <ul>
  4185. <li>b</li>
  4186. </ul>
  4187. </li>
  4188. </ul>
  4189. ````````````````````````````````
  4190. This list is loose, because of the blank line between the
  4191. two block elements in the list item:
  4192. ```````````````````````````````` example
  4193. 1. ```
  4194. foo
  4195. ```
  4196. bar
  4197. .
  4198. <ol>
  4199. <li>
  4200. <pre><code>foo
  4201. </code></pre>
  4202. <p>bar</p>
  4203. </li>
  4204. </ol>
  4205. ````````````````````````````````
  4206. Here the outer list is loose, the inner list tight:
  4207. ```````````````````````````````` example
  4208. * foo
  4209. * bar
  4210. baz
  4211. .
  4212. <ul>
  4213. <li>
  4214. <p>foo</p>
  4215. <ul>
  4216. <li>bar</li>
  4217. </ul>
  4218. <p>baz</p>
  4219. </li>
  4220. </ul>
  4221. ````````````````````````````````
  4222. ```````````````````````````````` example
  4223. - a
  4224. - b
  4225. - c
  4226. - d
  4227. - e
  4228. - f
  4229. .
  4230. <ul>
  4231. <li>
  4232. <p>a</p>
  4233. <ul>
  4234. <li>b</li>
  4235. <li>c</li>
  4236. </ul>
  4237. </li>
  4238. <li>
  4239. <p>d</p>
  4240. <ul>
  4241. <li>e</li>
  4242. <li>f</li>
  4243. </ul>
  4244. </li>
  4245. </ul>
  4246. ````````````````````````````````
  4247. # Inlines
  4248. Inlines are parsed sequentially from the beginning of the character
  4249. stream to the end (left to right, in left-to-right languages).
  4250. Thus, for example, in
  4251. ```````````````````````````````` example
  4252. `hi`lo`
  4253. .
  4254. <p><code>hi</code>lo`</p>
  4255. ````````````````````````````````
  4256. `hi` is parsed as code, leaving the backtick at the end as a literal
  4257. backtick.
  4258. ## Backslash escapes
  4259. Any ASCII punctuation character may be backslash-escaped:
  4260. ```````````````````````````````` example
  4261. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4262. .
  4263. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4264. ````````````````````````````````
  4265. Backslashes before other characters are treated as literal
  4266. backslashes:
  4267. ```````````````````````````````` example
  4268. \→\A\a\ \3\φ\«
  4269. .
  4270. <p>\→\A\a\ \3\φ\«</p>
  4271. ````````````````````````````````
  4272. Escaped characters are treated as regular characters and do
  4273. not have their usual Markdown meanings:
  4274. ```````````````````````````````` example
  4275. \*not emphasized*
  4276. \<br/> not a tag
  4277. \[not a link](/foo)
  4278. \`not code`
  4279. 1\. not a list
  4280. \* not a list
  4281. \# not a heading
  4282. \[foo]: /url "not a reference"
  4283. .
  4284. <p>*not emphasized*
  4285. &lt;br/&gt; not a tag
  4286. [not a link](/foo)
  4287. `not code`
  4288. 1. not a list
  4289. * not a list
  4290. # not a heading
  4291. [foo]: /url &quot;not a reference&quot;</p>
  4292. ````````````````````````````````
  4293. If a backslash is itself escaped, the following character is not:
  4294. ```````````````````````````````` example
  4295. \\*emphasis*
  4296. .
  4297. <p>\<em>emphasis</em></p>
  4298. ````````````````````````````````
  4299. A backslash at the end of the line is a [hard line break]:
  4300. ```````````````````````````````` example
  4301. foo\
  4302. bar
  4303. .
  4304. <p>foo<br />
  4305. bar</p>
  4306. ````````````````````````````````
  4307. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4308. raw HTML:
  4309. ```````````````````````````````` example
  4310. `` \[\` ``
  4311. .
  4312. <p><code>\[\`</code></p>
  4313. ````````````````````````````````
  4314. ```````````````````````````````` example
  4315. \[\]
  4316. .
  4317. <pre><code>\[\]
  4318. </code></pre>
  4319. ````````````````````````````````
  4320. ```````````````````````````````` example
  4321. ~~~
  4322. \[\]
  4323. ~~~
  4324. .
  4325. <pre><code>\[\]
  4326. </code></pre>
  4327. ````````````````````````````````
  4328. ```````````````````````````````` example
  4329. <http://example.com?find=\*>
  4330. .
  4331. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4332. ````````````````````````````````
  4333. ```````````````````````````````` example
  4334. <a href="/bar\/)">
  4335. .
  4336. <a href="/bar\/)">
  4337. ````````````````````````````````
  4338. But they work in all other contexts, including URLs and link titles,
  4339. link references, and [info strings] in [fenced code blocks]:
  4340. ```````````````````````````````` example
  4341. [foo](/bar\* "ti\*tle")
  4342. .
  4343. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4344. ````````````````````````````````
  4345. ```````````````````````````````` example
  4346. [foo]
  4347. [foo]: /bar\* "ti\*tle"
  4348. .
  4349. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4350. ````````````````````````````````
  4351. ```````````````````````````````` example
  4352. ``` foo\+bar
  4353. foo
  4354. ```
  4355. .
  4356. <pre><code class="language-foo+bar">foo
  4357. </code></pre>
  4358. ````````````````````````````````
  4359. ## Entity and numeric character references
  4360. Valid HTML entity references and numeric character references
  4361. can be used in place of the corresponding Unicode character,
  4362. with the following exceptions:
  4363. - Entity and character references are not recognized in code
  4364. blocks and code spans.
  4365. - Entity and character references cannot stand in place of
  4366. special characters that define structural elements in
  4367. CommonMark. For example, although `&#42;` can be used
  4368. in place of a literal `*` character, `&#42;` cannot replace
  4369. `*` in emphasis delimiters, bullet list markers, or thematic
  4370. breaks.
  4371. Conforming CommonMark parsers need not store information about
  4372. whether a particular character was represented in the source
  4373. using a Unicode character or an entity reference.
  4374. [Entity references](@) consist of `&` + any of the valid
  4375. HTML5 entity names + `;`. The
  4376. document <https://html.spec.whatwg.org/multipage/entities.json>
  4377. is used as an authoritative source for the valid entity
  4378. references and their corresponding code points.
  4379. ```````````````````````````````` example
  4380. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4381. &frac34; &HilbertSpace; &DifferentialD;
  4382. &ClockwiseContourIntegral; &ngE;
  4383. .
  4384. <p>  &amp; © Æ Ď
  4385. ¾ ℋ ⅆ
  4386. ∲ ≧̸</p>
  4387. ````````````````````````````````
  4388. [Decimal numeric character
  4389. references](@)
  4390. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4391. numeric character reference is parsed as the corresponding
  4392. Unicode character. Invalid Unicode code points will be replaced by
  4393. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4394. the code point `U+0000` will also be replaced by `U+FFFD`.
  4395. ```````````````````````````````` example
  4396. &#35; &#1234; &#992; &#0;
  4397. .
  4398. <p># Ӓ Ϡ �</p>
  4399. ````````````````````````````````
  4400. [Hexadecimal numeric character
  4401. references](@) consist of `&#` +
  4402. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4403. They too are parsed as the corresponding Unicode character (this
  4404. time specified with a hexadecimal numeral instead of decimal).
  4405. ```````````````````````````````` example
  4406. &#X22; &#XD06; &#xcab;
  4407. .
  4408. <p>&quot; ആ ಫ</p>
  4409. ````````````````````````````````
  4410. Here are some nonentities:
  4411. ```````````````````````````````` example
  4412. &nbsp &x; &#; &#x;
  4413. &#987654321;
  4414. &#abcdef0;
  4415. &ThisIsNotDefined; &hi?;
  4416. .
  4417. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4418. &amp;#987654321;
  4419. &amp;#abcdef0;
  4420. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4421. ````````````````````````````````
  4422. Although HTML5 does accept some entity references
  4423. without a trailing semicolon (such as `&copy`), these are not
  4424. recognized here, because it makes the grammar too ambiguous:
  4425. ```````````````````````````````` example
  4426. &copy
  4427. .
  4428. <p>&amp;copy</p>
  4429. ````````````````````````````````
  4430. Strings that are not on the list of HTML5 named entities are not
  4431. recognized as entity references either:
  4432. ```````````````````````````````` example
  4433. &MadeUpEntity;
  4434. .
  4435. <p>&amp;MadeUpEntity;</p>
  4436. ````````````````````````````````
  4437. Entity and numeric character references are recognized in any
  4438. context besides code spans or code blocks, including
  4439. URLs, [link titles], and [fenced code block][] [info strings]:
  4440. ```````````````````````````````` example
  4441. <a href="&ouml;&ouml;.html">
  4442. .
  4443. <a href="&ouml;&ouml;.html">
  4444. ````````````````````````````````
  4445. ```````````````````````````````` example
  4446. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4447. .
  4448. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4449. ````````````````````````````````
  4450. ```````````````````````````````` example
  4451. [foo]
  4452. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4453. .
  4454. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4455. ````````````````````````````````
  4456. ```````````````````````````````` example
  4457. ``` f&ouml;&ouml;
  4458. foo
  4459. ```
  4460. .
  4461. <pre><code class="language-föö">foo
  4462. </code></pre>
  4463. ````````````````````````````````
  4464. Entity and numeric character references are treated as literal
  4465. text in code spans and code blocks:
  4466. ```````````````````````````````` example
  4467. `f&ouml;&ouml;`
  4468. .
  4469. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4470. ````````````````````````````````
  4471. ```````````````````````````````` example
  4472. f&ouml;f&ouml;
  4473. .
  4474. <pre><code>f&amp;ouml;f&amp;ouml;
  4475. </code></pre>
  4476. ````````````````````````````````
  4477. Entity and numeric character references cannot be used
  4478. in place of symbols indicating structure in CommonMark
  4479. documents.
  4480. ```````````````````````````````` example
  4481. &#42;foo&#42;
  4482. *foo*
  4483. .
  4484. <p>*foo*
  4485. <em>foo</em></p>
  4486. ````````````````````````````````
  4487. ```````````````````````````````` example
  4488. &#42; foo
  4489. * foo
  4490. .
  4491. <p>* foo</p>
  4492. <ul>
  4493. <li>foo</li>
  4494. </ul>
  4495. ````````````````````````````````
  4496. ```````````````````````````````` example
  4497. foo&#10;&#10;bar
  4498. .
  4499. <p>foo
  4500. bar</p>
  4501. ````````````````````````````````
  4502. ```````````````````````````````` example
  4503. &#9;foo
  4504. .
  4505. <p>→foo</p>
  4506. ````````````````````````````````
  4507. ```````````````````````````````` example
  4508. [a](url &quot;tit&quot;)
  4509. .
  4510. <p>[a](url &quot;tit&quot;)</p>
  4511. ````````````````````````````````
  4512. ## Code spans
  4513. A [backtick string](@)
  4514. is a string of one or more backtick characters (`` ` ``) that is neither
  4515. preceded nor followed by a backtick.
  4516. A [code span](@) begins with a backtick string and ends with
  4517. a backtick string of equal length. The contents of the code span are
  4518. the characters between the two backtick strings, normalized in the
  4519. following ways:
  4520. - First, [line endings] are converted to [spaces].
  4521. - If the resulting string both begins *and* ends with a [space]
  4522. character, but does not consist entirely of [space]
  4523. characters, a single [space] character is removed from the
  4524. front and back. This allows you to include code that begins
  4525. or ends with backtick characters, which must be separated by
  4526. whitespace from the opening or closing backtick strings.
  4527. This is a simple code span:
  4528. ```````````````````````````````` example
  4529. `foo`
  4530. .
  4531. <p><code>foo</code></p>
  4532. ````````````````````````````````
  4533. Here two backticks are used, because the code contains a backtick.
  4534. This example also illustrates stripping of a single leading and
  4535. trailing space:
  4536. ```````````````````````````````` example
  4537. `` foo ` bar ``
  4538. .
  4539. <p><code>foo ` bar</code></p>
  4540. ````````````````````````````````
  4541. This example shows the motivation for stripping leading and trailing
  4542. spaces:
  4543. ```````````````````````````````` example
  4544. ` `` `
  4545. .
  4546. <p><code>``</code></p>
  4547. ````````````````````````````````
  4548. Note that only *one* space is stripped:
  4549. ```````````````````````````````` example
  4550. ` `` `
  4551. .
  4552. <p><code> `` </code></p>
  4553. ````````````````````````````````
  4554. The stripping only happens if the space is on both
  4555. sides of the string:
  4556. ```````````````````````````````` example
  4557. ` a`
  4558. .
  4559. <p><code> a</code></p>
  4560. ````````````````````````````````
  4561. Only [spaces], and not [unicode whitespace] in general, are
  4562. stripped in this way:
  4563. ```````````````````````````````` example
  4564. ` b `
  4565. .
  4566. <p><code> b </code></p>
  4567. ````````````````````````````````
  4568. No stripping occurs if the code span contains only spaces:
  4569. ```````````````````````````````` example
  4570. ` `
  4571. ` `
  4572. .
  4573. <p><code> </code>
  4574. <code> </code></p>
  4575. ````````````````````````````````
  4576. [Line endings] are treated like spaces:
  4577. ```````````````````````````````` example
  4578. ``
  4579. foo
  4580. bar
  4581. baz
  4582. ``
  4583. .
  4584. <p><code>foo bar baz</code></p>
  4585. ````````````````````````````````
  4586. ```````````````````````````````` example
  4587. ``
  4588. foo
  4589. ``
  4590. .
  4591. <p><code>foo </code></p>
  4592. ````````````````````````````````
  4593. Interior spaces are not collapsed:
  4594. ```````````````````````````````` example
  4595. `foo bar
  4596. baz`
  4597. .
  4598. <p><code>foo bar baz</code></p>
  4599. ````````````````````````````````
  4600. Note that browsers will typically collapse consecutive spaces
  4601. when rendering `<code>` elements, so it is recommended that
  4602. the following CSS be used:
  4603. code{white-space: pre-wrap;}
  4604. Note that backslash escapes do not work in code spans. All backslashes
  4605. are treated literally:
  4606. ```````````````````````````````` example
  4607. `foo\`bar`
  4608. .
  4609. <p><code>foo\</code>bar`</p>
  4610. ````````````````````````````````
  4611. Backslash escapes are never needed, because one can always choose a
  4612. string of *n* backtick characters as delimiters, where the code does
  4613. not contain any strings of exactly *n* backtick characters.
  4614. ```````````````````````````````` example
  4615. ``foo`bar``
  4616. .
  4617. <p><code>foo`bar</code></p>
  4618. ````````````````````````````````
  4619. ```````````````````````````````` example
  4620. ` foo `` bar `
  4621. .
  4622. <p><code>foo `` bar</code></p>
  4623. ````````````````````````````````
  4624. Code span backticks have higher precedence than any other inline
  4625. constructs except HTML tags and autolinks. Thus, for example, this is
  4626. not parsed as emphasized text, since the second `*` is part of a code
  4627. span:
  4628. ```````````````````````````````` example
  4629. *foo`*`
  4630. .
  4631. <p>*foo<code>*</code></p>
  4632. ````````````````````````````````
  4633. And this is not parsed as a link:
  4634. ```````````````````````````````` example
  4635. [not a `link](/foo`)
  4636. .
  4637. <p>[not a <code>link](/foo</code>)</p>
  4638. ````````````````````````````````
  4639. Code spans, HTML tags, and autolinks have the same precedence.
  4640. Thus, this is code:
  4641. ```````````````````````````````` example
  4642. `<a href="`">`
  4643. .
  4644. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4645. ````````````````````````````````
  4646. But this is an HTML tag:
  4647. ```````````````````````````````` example
  4648. <a href="`">`
  4649. .
  4650. <p><a href="`">`</p>
  4651. ````````````````````````````````
  4652. And this is code:
  4653. ```````````````````````````````` example
  4654. `<http://foo.bar.`baz>`
  4655. .
  4656. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4657. ````````````````````````````````
  4658. But this is an autolink:
  4659. ```````````````````````````````` example
  4660. <http://foo.bar.`baz>`
  4661. .
  4662. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4663. ````````````````````````````````
  4664. When a backtick string is not closed by a matching backtick string,
  4665. we just have literal backticks:
  4666. ```````````````````````````````` example
  4667. ```foo``
  4668. .
  4669. <p>```foo``</p>
  4670. ````````````````````````````````
  4671. ```````````````````````````````` example
  4672. `foo
  4673. .
  4674. <p>`foo</p>
  4675. ````````````````````````````````
  4676. The following case also illustrates the need for opening and
  4677. closing backtick strings to be equal in length:
  4678. ```````````````````````````````` example
  4679. `foo``bar``
  4680. .
  4681. <p>`foo<code>bar</code></p>
  4682. ````````````````````````````````
  4683. ## Emphasis and strong emphasis
  4684. John Gruber's original [Markdown syntax
  4685. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4686. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4687. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4688. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4689. > tag.
  4690. This is enough for most users, but these rules leave much undecided,
  4691. especially when it comes to nested emphasis. The original
  4692. `Markdown.pl` test suite makes it clear that triple `***` and
  4693. `___` delimiters can be used for strong emphasis, and most
  4694. implementations have also allowed the following patterns:
  4695. ``` markdown
  4696. ***strong emph***
  4697. ***strong** in emph*
  4698. ***emph* in strong**
  4699. **in strong *emph***
  4700. *in emph **strong***
  4701. ```
  4702. The following patterns are less widely supported, but the intent
  4703. is clear and they are useful (especially in contexts like bibliography
  4704. entries):
  4705. ``` markdown
  4706. *emph *with emph* in it*
  4707. **strong **with strong** in it**
  4708. ```
  4709. Many implementations have also restricted intraword emphasis to
  4710. the `*` forms, to avoid unwanted emphasis in words containing
  4711. internal underscores. (It is best practice to put these in code
  4712. spans, but users often do not.)
  4713. ``` markdown
  4714. internal emphasis: foo*bar*baz
  4715. no emphasis: foo_bar_baz
  4716. ```
  4717. The rules given below capture all of these patterns, while allowing
  4718. for efficient parsing strategies that do not backtrack.
  4719. First, some definitions. A [delimiter run](@) is either
  4720. a sequence of one or more `*` characters that is not preceded or
  4721. followed by a non-backslash-escaped `*` character, or a sequence
  4722. of one or more `_` characters that is not preceded or followed by
  4723. a non-backslash-escaped `_` character.
  4724. A [left-flanking delimiter run](@) is
  4725. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4726. and either (2a) not followed by a [punctuation character], or
  4727. (2b) followed by a [punctuation character] and
  4728. preceded by [Unicode whitespace] or a [punctuation character].
  4729. For purposes of this definition, the beginning and the end of
  4730. the line count as Unicode whitespace.
  4731. A [right-flanking delimiter run](@) is
  4732. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4733. and either (2a) not preceded by a [punctuation character], or
  4734. (2b) preceded by a [punctuation character] and
  4735. followed by [Unicode whitespace] or a [punctuation character].
  4736. For purposes of this definition, the beginning and the end of
  4737. the line count as Unicode whitespace.
  4738. Here are some examples of delimiter runs.
  4739. - left-flanking but not right-flanking:
  4740. ```
  4741. ***abc
  4742. _abc
  4743. **"abc"
  4744. _"abc"
  4745. ```
  4746. - right-flanking but not left-flanking:
  4747. ```
  4748. abc***
  4749. abc_
  4750. "abc"**
  4751. "abc"_
  4752. ```
  4753. - Both left and right-flanking:
  4754. ```
  4755. abc***def
  4756. "abc"_"def"
  4757. ```
  4758. - Neither left nor right-flanking:
  4759. ```
  4760. abc *** def
  4761. a _ b
  4762. ```
  4763. (The idea of distinguishing left-flanking and right-flanking
  4764. delimiter runs based on the character before and the character
  4765. after comes from Roopesh Chander's
  4766. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4767. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4768. run," and its rules for distinguishing left- and right-flanking runs
  4769. are a bit more complex than the ones given here.)
  4770. The following rules define emphasis and strong emphasis:
  4771. 1. A single `*` character [can open emphasis](@)
  4772. iff (if and only if) it is part of a [left-flanking delimiter run].
  4773. 2. A single `_` character [can open emphasis] iff
  4774. it is part of a [left-flanking delimiter run]
  4775. and either (a) not part of a [right-flanking delimiter run]
  4776. or (b) part of a [right-flanking delimiter run]
  4777. preceded by punctuation.
  4778. 3. A single `*` character [can close emphasis](@)
  4779. iff it is part of a [right-flanking delimiter run].
  4780. 4. A single `_` character [can close emphasis] iff
  4781. it is part of a [right-flanking delimiter run]
  4782. and either (a) not part of a [left-flanking delimiter run]
  4783. or (b) part of a [left-flanking delimiter run]
  4784. followed by punctuation.
  4785. 5. A double `**` [can open strong emphasis](@)
  4786. iff it is part of a [left-flanking delimiter run].
  4787. 6. A double `__` [can open strong emphasis] iff
  4788. it is part of a [left-flanking delimiter run]
  4789. and either (a) not part of a [right-flanking delimiter run]
  4790. or (b) part of a [right-flanking delimiter run]
  4791. preceded by punctuation.
  4792. 7. A double `**` [can close strong emphasis](@)
  4793. iff it is part of a [right-flanking delimiter run].
  4794. 8. A double `__` [can close strong emphasis] iff
  4795. it is part of a [right-flanking delimiter run]
  4796. and either (a) not part of a [left-flanking delimiter run]
  4797. or (b) part of a [left-flanking delimiter run]
  4798. followed by punctuation.
  4799. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4800. with a delimiter that [can close emphasis], and that uses the same
  4801. character (`_` or `*`) as the opening delimiter. The
  4802. opening and closing delimiters must belong to separate
  4803. [delimiter runs]. If one of the delimiters can both
  4804. open and close emphasis, then the sum of the lengths of the
  4805. delimiter runs containing the opening and closing delimiters
  4806. must not be a multiple of 3 unless both lengths are
  4807. multiples of 3.
  4808. 10. Strong emphasis begins with a delimiter that
  4809. [can open strong emphasis] and ends with a delimiter that
  4810. [can close strong emphasis], and that uses the same character
  4811. (`_` or `*`) as the opening delimiter. The
  4812. opening and closing delimiters must belong to separate
  4813. [delimiter runs]. If one of the delimiters can both open
  4814. and close strong emphasis, then the sum of the lengths of
  4815. the delimiter runs containing the opening and closing
  4816. delimiters must not be a multiple of 3 unless both lengths
  4817. are multiples of 3.
  4818. 11. A literal `*` character cannot occur at the beginning or end of
  4819. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4820. is backslash-escaped.
  4821. 12. A literal `_` character cannot occur at the beginning or end of
  4822. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4823. is backslash-escaped.
  4824. Where rules 1--12 above are compatible with multiple parsings,
  4825. the following principles resolve ambiguity:
  4826. 13. The number of nestings should be minimized. Thus, for example,
  4827. an interpretation `<strong>...</strong>` is always preferred to
  4828. `<em><em>...</em></em>`.
  4829. 14. An interpretation `<em><strong>...</strong></em>` is always
  4830. preferred to `<strong><em>...</em></strong>`.
  4831. 15. When two potential emphasis or strong emphasis spans overlap,
  4832. so that the second begins before the first ends and ends after
  4833. the first ends, the first takes precedence. Thus, for example,
  4834. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4835. than `*foo <em>bar* baz</em>`.
  4836. 16. When there are two potential emphasis or strong emphasis spans
  4837. with the same closing delimiter, the shorter one (the one that
  4838. opens later) takes precedence. Thus, for example,
  4839. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4840. rather than `<strong>foo **bar baz</strong>`.
  4841. 17. Inline code spans, links, images, and HTML tags group more tightly
  4842. than emphasis. So, when there is a choice between an interpretation
  4843. that contains one of these elements and one that does not, the
  4844. former always wins. Thus, for example, `*[foo*](bar)` is
  4845. parsed as `*<a href="bar">foo*</a>` rather than as
  4846. `<em>[foo</em>](bar)`.
  4847. These rules can be illustrated through a series of examples.
  4848. Rule 1:
  4849. ```````````````````````````````` example
  4850. *foo bar*
  4851. .
  4852. <p><em>foo bar</em></p>
  4853. ````````````````````````````````
  4854. This is not emphasis, because the opening `*` is followed by
  4855. whitespace, and hence not part of a [left-flanking delimiter run]:
  4856. ```````````````````````````````` example
  4857. a * foo bar*
  4858. .
  4859. <p>a * foo bar*</p>
  4860. ````````````````````````````````
  4861. This is not emphasis, because the opening `*` is preceded
  4862. by an alphanumeric and followed by punctuation, and hence
  4863. not part of a [left-flanking delimiter run]:
  4864. ```````````````````````````````` example
  4865. a*"foo"*
  4866. .
  4867. <p>a*&quot;foo&quot;*</p>
  4868. ````````````````````````````````
  4869. Unicode nonbreaking spaces count as whitespace, too:
  4870. ```````````````````````````````` example
  4871. * a *
  4872. .
  4873. <p>* a *</p>
  4874. ````````````````````````````````
  4875. Intraword emphasis with `*` is permitted:
  4876. ```````````````````````````````` example
  4877. foo*bar*
  4878. .
  4879. <p>foo<em>bar</em></p>
  4880. ````````````````````````````````
  4881. ```````````````````````````````` example
  4882. 5*6*78
  4883. .
  4884. <p>5<em>6</em>78</p>
  4885. ````````````````````````````````
  4886. Rule 2:
  4887. ```````````````````````````````` example
  4888. _foo bar_
  4889. .
  4890. <p><em>foo bar</em></p>
  4891. ````````````````````````````````
  4892. This is not emphasis, because the opening `_` is followed by
  4893. whitespace:
  4894. ```````````````````````````````` example
  4895. _ foo bar_
  4896. .
  4897. <p>_ foo bar_</p>
  4898. ````````````````````````````````
  4899. This is not emphasis, because the opening `_` is preceded
  4900. by an alphanumeric and followed by punctuation:
  4901. ```````````````````````````````` example
  4902. a_"foo"_
  4903. .
  4904. <p>a_&quot;foo&quot;_</p>
  4905. ````````````````````````````````
  4906. Emphasis with `_` is not allowed inside words:
  4907. ```````````````````````````````` example
  4908. foo_bar_
  4909. .
  4910. <p>foo_bar_</p>
  4911. ````````````````````````````````
  4912. ```````````````````````````````` example
  4913. 5_6_78
  4914. .
  4915. <p>5_6_78</p>
  4916. ````````````````````````````````
  4917. ```````````````````````````````` example
  4918. пристаням_стремятся_
  4919. .
  4920. <p>пристаням_стремятся_</p>
  4921. ````````````````````````````````
  4922. Here `_` does not generate emphasis, because the first delimiter run
  4923. is right-flanking and the second left-flanking:
  4924. ```````````````````````````````` example
  4925. aa_"bb"_cc
  4926. .
  4927. <p>aa_&quot;bb&quot;_cc</p>
  4928. ````````````````````````````````
  4929. This is emphasis, even though the opening delimiter is
  4930. both left- and right-flanking, because it is preceded by
  4931. punctuation:
  4932. ```````````````````````````````` example
  4933. foo-_(bar)_
  4934. .
  4935. <p>foo-<em>(bar)</em></p>
  4936. ````````````````````````````````
  4937. Rule 3:
  4938. This is not emphasis, because the closing delimiter does
  4939. not match the opening delimiter:
  4940. ```````````````````````````````` example
  4941. _foo*
  4942. .
  4943. <p>_foo*</p>
  4944. ````````````````````````````````
  4945. This is not emphasis, because the closing `*` is preceded by
  4946. whitespace:
  4947. ```````````````````````````````` example
  4948. *foo bar *
  4949. .
  4950. <p>*foo bar *</p>
  4951. ````````````````````````````````
  4952. A newline also counts as whitespace:
  4953. ```````````````````````````````` example
  4954. *foo bar
  4955. *
  4956. .
  4957. <p>*foo bar
  4958. *</p>
  4959. ````````````````````````````````
  4960. This is not emphasis, because the second `*` is
  4961. preceded by punctuation and followed by an alphanumeric
  4962. (hence it is not part of a [right-flanking delimiter run]:
  4963. ```````````````````````````````` example
  4964. *(*foo)
  4965. .
  4966. <p>*(*foo)</p>
  4967. ````````````````````````````````
  4968. The point of this restriction is more easily appreciated
  4969. with this example:
  4970. ```````````````````````````````` example
  4971. *(*foo*)*
  4972. .
  4973. <p><em>(<em>foo</em>)</em></p>
  4974. ````````````````````````````````
  4975. Intraword emphasis with `*` is allowed:
  4976. ```````````````````````````````` example
  4977. *foo*bar
  4978. .
  4979. <p><em>foo</em>bar</p>
  4980. ````````````````````````````````
  4981. Rule 4:
  4982. This is not emphasis, because the closing `_` is preceded by
  4983. whitespace:
  4984. ```````````````````````````````` example
  4985. _foo bar _
  4986. .
  4987. <p>_foo bar _</p>
  4988. ````````````````````````````````
  4989. This is not emphasis, because the second `_` is
  4990. preceded by punctuation and followed by an alphanumeric:
  4991. ```````````````````````````````` example
  4992. _(_foo)
  4993. .
  4994. <p>_(_foo)</p>
  4995. ````````````````````````````````
  4996. This is emphasis within emphasis:
  4997. ```````````````````````````````` example
  4998. _(_foo_)_
  4999. .
  5000. <p><em>(<em>foo</em>)</em></p>
  5001. ````````````````````````````````
  5002. Intraword emphasis is disallowed for `_`:
  5003. ```````````````````````````````` example
  5004. _foo_bar
  5005. .
  5006. <p>_foo_bar</p>
  5007. ````````````````````````````````
  5008. ```````````````````````````````` example
  5009. _пристаням_стремятся
  5010. .
  5011. <p>_пристаням_стремятся</p>
  5012. ````````````````````````````````
  5013. ```````````````````````````````` example
  5014. _foo_bar_baz_
  5015. .
  5016. <p><em>foo_bar_baz</em></p>
  5017. ````````````````````````````````
  5018. This is emphasis, even though the closing delimiter is
  5019. both left- and right-flanking, because it is followed by
  5020. punctuation:
  5021. ```````````````````````````````` example
  5022. _(bar)_.
  5023. .
  5024. <p><em>(bar)</em>.</p>
  5025. ````````````````````````````````
  5026. Rule 5:
  5027. ```````````````````````````````` example
  5028. **foo bar**
  5029. .
  5030. <p><strong>foo bar</strong></p>
  5031. ````````````````````````````````
  5032. This is not strong emphasis, because the opening delimiter is
  5033. followed by whitespace:
  5034. ```````````````````````````````` example
  5035. ** foo bar**
  5036. .
  5037. <p>** foo bar**</p>
  5038. ````````````````````````````````
  5039. This is not strong emphasis, because the opening `**` is preceded
  5040. by an alphanumeric and followed by punctuation, and hence
  5041. not part of a [left-flanking delimiter run]:
  5042. ```````````````````````````````` example
  5043. a**"foo"**
  5044. .
  5045. <p>a**&quot;foo&quot;**</p>
  5046. ````````````````````````````````
  5047. Intraword strong emphasis with `**` is permitted:
  5048. ```````````````````````````````` example
  5049. foo**bar**
  5050. .
  5051. <p>foo<strong>bar</strong></p>
  5052. ````````````````````````````````
  5053. Rule 6:
  5054. ```````````````````````````````` example
  5055. __foo bar__
  5056. .
  5057. <p><strong>foo bar</strong></p>
  5058. ````````````````````````````````
  5059. This is not strong emphasis, because the opening delimiter is
  5060. followed by whitespace:
  5061. ```````````````````````````````` example
  5062. __ foo bar__
  5063. .
  5064. <p>__ foo bar__</p>
  5065. ````````````````````````````````
  5066. A newline counts as whitespace:
  5067. ```````````````````````````````` example
  5068. __
  5069. foo bar__
  5070. .
  5071. <p>__
  5072. foo bar__</p>
  5073. ````````````````````````````````
  5074. This is not strong emphasis, because the opening `__` is preceded
  5075. by an alphanumeric and followed by punctuation:
  5076. ```````````````````````````````` example
  5077. a__"foo"__
  5078. .
  5079. <p>a__&quot;foo&quot;__</p>
  5080. ````````````````````````````````
  5081. Intraword strong emphasis is forbidden with `__`:
  5082. ```````````````````````````````` example
  5083. foo__bar__
  5084. .
  5085. <p>foo__bar__</p>
  5086. ````````````````````````````````
  5087. ```````````````````````````````` example
  5088. 5__6__78
  5089. .
  5090. <p>5__6__78</p>
  5091. ````````````````````````````````
  5092. ```````````````````````````````` example
  5093. пристаням__стремятся__
  5094. .
  5095. <p>пристаням__стремятся__</p>
  5096. ````````````````````````````````
  5097. ```````````````````````````````` example
  5098. __foo, __bar__, baz__
  5099. .
  5100. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5101. ````````````````````````````````
  5102. This is strong emphasis, even though the opening delimiter is
  5103. both left- and right-flanking, because it is preceded by
  5104. punctuation:
  5105. ```````````````````````````````` example
  5106. foo-__(bar)__
  5107. .
  5108. <p>foo-<strong>(bar)</strong></p>
  5109. ````````````````````````````````
  5110. Rule 7:
  5111. This is not strong emphasis, because the closing delimiter is preceded
  5112. by whitespace:
  5113. ```````````````````````````````` example
  5114. **foo bar **
  5115. .
  5116. <p>**foo bar **</p>
  5117. ````````````````````````````````
  5118. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5119. Rule 11.)
  5120. This is not strong emphasis, because the second `**` is
  5121. preceded by punctuation and followed by an alphanumeric:
  5122. ```````````````````````````````` example
  5123. **(**foo)
  5124. .
  5125. <p>**(**foo)</p>
  5126. ````````````````````````````````
  5127. The point of this restriction is more easily appreciated
  5128. with these examples:
  5129. ```````````````````````````````` example
  5130. *(**foo**)*
  5131. .
  5132. <p><em>(<strong>foo</strong>)</em></p>
  5133. ````````````````````````````````
  5134. ```````````````````````````````` example
  5135. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5136. *Asclepias physocarpa*)**
  5137. .
  5138. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5139. <em>Asclepias physocarpa</em>)</strong></p>
  5140. ````````````````````````````````
  5141. ```````````````````````````````` example
  5142. **foo "*bar*" foo**
  5143. .
  5144. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5145. ````````````````````````````````
  5146. Intraword emphasis:
  5147. ```````````````````````````````` example
  5148. **foo**bar
  5149. .
  5150. <p><strong>foo</strong>bar</p>
  5151. ````````````````````````````````
  5152. Rule 8:
  5153. This is not strong emphasis, because the closing delimiter is
  5154. preceded by whitespace:
  5155. ```````````````````````````````` example
  5156. __foo bar __
  5157. .
  5158. <p>__foo bar __</p>
  5159. ````````````````````````````````
  5160. This is not strong emphasis, because the second `__` is
  5161. preceded by punctuation and followed by an alphanumeric:
  5162. ```````````````````````````````` example
  5163. __(__foo)
  5164. .
  5165. <p>__(__foo)</p>
  5166. ````````````````````````````````
  5167. The point of this restriction is more easily appreciated
  5168. with this example:
  5169. ```````````````````````````````` example
  5170. _(__foo__)_
  5171. .
  5172. <p><em>(<strong>foo</strong>)</em></p>
  5173. ````````````````````````````````
  5174. Intraword strong emphasis is forbidden with `__`:
  5175. ```````````````````````````````` example
  5176. __foo__bar
  5177. .
  5178. <p>__foo__bar</p>
  5179. ````````````````````````````````
  5180. ```````````````````````````````` example
  5181. __пристаням__стремятся
  5182. .
  5183. <p>__пристаням__стремятся</p>
  5184. ````````````````````````````````
  5185. ```````````````````````````````` example
  5186. __foo__bar__baz__
  5187. .
  5188. <p><strong>foo__bar__baz</strong></p>
  5189. ````````````````````````````````
  5190. This is strong emphasis, even though the closing delimiter is
  5191. both left- and right-flanking, because it is followed by
  5192. punctuation:
  5193. ```````````````````````````````` example
  5194. __(bar)__.
  5195. .
  5196. <p><strong>(bar)</strong>.</p>
  5197. ````````````````````````````````
  5198. Rule 9:
  5199. Any nonempty sequence of inline elements can be the contents of an
  5200. emphasized span.
  5201. ```````````````````````````````` example
  5202. *foo [bar](/url)*
  5203. .
  5204. <p><em>foo <a href="/url">bar</a></em></p>
  5205. ````````````````````````````````
  5206. ```````````````````````````````` example
  5207. *foo
  5208. bar*
  5209. .
  5210. <p><em>foo
  5211. bar</em></p>
  5212. ````````````````````````````````
  5213. In particular, emphasis and strong emphasis can be nested
  5214. inside emphasis:
  5215. ```````````````````````````````` example
  5216. _foo __bar__ baz_
  5217. .
  5218. <p><em>foo <strong>bar</strong> baz</em></p>
  5219. ````````````````````````````````
  5220. ```````````````````````````````` example
  5221. _foo _bar_ baz_
  5222. .
  5223. <p><em>foo <em>bar</em> baz</em></p>
  5224. ````````````````````````````````
  5225. ```````````````````````````````` example
  5226. __foo_ bar_
  5227. .
  5228. <p><em><em>foo</em> bar</em></p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. *foo *bar**
  5232. .
  5233. <p><em>foo <em>bar</em></em></p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. *foo **bar** baz*
  5237. .
  5238. <p><em>foo <strong>bar</strong> baz</em></p>
  5239. ````````````````````````````````
  5240. ```````````````````````````````` example
  5241. *foo**bar**baz*
  5242. .
  5243. <p><em>foo<strong>bar</strong>baz</em></p>
  5244. ````````````````````````````````
  5245. Note that in the preceding case, the interpretation
  5246. ``` markdown
  5247. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5248. ```
  5249. is precluded by the condition that a delimiter that
  5250. can both open and close (like the `*` after `foo`)
  5251. cannot form emphasis if the sum of the lengths of
  5252. the delimiter runs containing the opening and
  5253. closing delimiters is a multiple of 3 unless
  5254. both lengths are multiples of 3.
  5255. For the same reason, we don't get two consecutive
  5256. emphasis sections in this example:
  5257. ```````````````````````````````` example
  5258. *foo**bar*
  5259. .
  5260. <p><em>foo**bar</em></p>
  5261. ````````````````````````````````
  5262. The same condition ensures that the following
  5263. cases are all strong emphasis nested inside
  5264. emphasis, even when the interior spaces are
  5265. omitted:
  5266. ```````````````````````````````` example
  5267. ***foo** bar*
  5268. .
  5269. <p><em><strong>foo</strong> bar</em></p>
  5270. ````````````````````````````````
  5271. ```````````````````````````````` example
  5272. *foo **bar***
  5273. .
  5274. <p><em>foo <strong>bar</strong></em></p>
  5275. ````````````````````````````````
  5276. ```````````````````````````````` example
  5277. *foo**bar***
  5278. .
  5279. <p><em>foo<strong>bar</strong></em></p>
  5280. ````````````````````````````````
  5281. When the lengths of the interior closing and opening
  5282. delimiter runs are *both* multiples of 3, though,
  5283. they can match to create emphasis:
  5284. ```````````````````````````````` example
  5285. foo***bar***baz
  5286. .
  5287. <p>foo<em><strong>bar</strong></em>baz</p>
  5288. ````````````````````````````````
  5289. ```````````````````````````````` example
  5290. foo******bar*********baz
  5291. .
  5292. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5293. ````````````````````````````````
  5294. Indefinite levels of nesting are possible:
  5295. ```````````````````````````````` example
  5296. *foo **bar *baz* bim** bop*
  5297. .
  5298. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5299. ````````````````````````````````
  5300. ```````````````````````````````` example
  5301. *foo [*bar*](/url)*
  5302. .
  5303. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5304. ````````````````````````````````
  5305. There can be no empty emphasis or strong emphasis:
  5306. ```````````````````````````````` example
  5307. ** is not an empty emphasis
  5308. .
  5309. <p>** is not an empty emphasis</p>
  5310. ````````````````````````````````
  5311. ```````````````````````````````` example
  5312. **** is not an empty strong emphasis
  5313. .
  5314. <p>**** is not an empty strong emphasis</p>
  5315. ````````````````````````````````
  5316. Rule 10:
  5317. Any nonempty sequence of inline elements can be the contents of an
  5318. strongly emphasized span.
  5319. ```````````````````````````````` example
  5320. **foo [bar](/url)**
  5321. .
  5322. <p><strong>foo <a href="/url">bar</a></strong></p>
  5323. ````````````````````````````````
  5324. ```````````````````````````````` example
  5325. **foo
  5326. bar**
  5327. .
  5328. <p><strong>foo
  5329. bar</strong></p>
  5330. ````````````````````````````````
  5331. In particular, emphasis and strong emphasis can be nested
  5332. inside strong emphasis:
  5333. ```````````````````````````````` example
  5334. __foo _bar_ baz__
  5335. .
  5336. <p><strong>foo <em>bar</em> baz</strong></p>
  5337. ````````````````````````````````
  5338. ```````````````````````````````` example
  5339. __foo __bar__ baz__
  5340. .
  5341. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5342. ````````````````````````````````
  5343. ```````````````````````````````` example
  5344. ____foo__ bar__
  5345. .
  5346. <p><strong><strong>foo</strong> bar</strong></p>
  5347. ````````````````````````````````
  5348. ```````````````````````````````` example
  5349. **foo **bar****
  5350. .
  5351. <p><strong>foo <strong>bar</strong></strong></p>
  5352. ````````````````````````````````
  5353. ```````````````````````````````` example
  5354. **foo *bar* baz**
  5355. .
  5356. <p><strong>foo <em>bar</em> baz</strong></p>
  5357. ````````````````````````````````
  5358. ```````````````````````````````` example
  5359. **foo*bar*baz**
  5360. .
  5361. <p><strong>foo<em>bar</em>baz</strong></p>
  5362. ````````````````````````````````
  5363. ```````````````````````````````` example
  5364. ***foo* bar**
  5365. .
  5366. <p><strong><em>foo</em> bar</strong></p>
  5367. ````````````````````````````````
  5368. ```````````````````````````````` example
  5369. **foo *bar***
  5370. .
  5371. <p><strong>foo <em>bar</em></strong></p>
  5372. ````````````````````````````````
  5373. Indefinite levels of nesting are possible:
  5374. ```````````````````````````````` example
  5375. **foo *bar **baz**
  5376. bim* bop**
  5377. .
  5378. <p><strong>foo <em>bar <strong>baz</strong>
  5379. bim</em> bop</strong></p>
  5380. ````````````````````````````````
  5381. ```````````````````````````````` example
  5382. **foo [*bar*](/url)**
  5383. .
  5384. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5385. ````````````````````````````````
  5386. There can be no empty emphasis or strong emphasis:
  5387. ```````````````````````````````` example
  5388. __ is not an empty emphasis
  5389. .
  5390. <p>__ is not an empty emphasis</p>
  5391. ````````````````````````````````
  5392. ```````````````````````````````` example
  5393. ____ is not an empty strong emphasis
  5394. .
  5395. <p>____ is not an empty strong emphasis</p>
  5396. ````````````````````````````````
  5397. Rule 11:
  5398. ```````````````````````````````` example
  5399. foo ***
  5400. .
  5401. <p>foo ***</p>
  5402. ````````````````````````````````
  5403. ```````````````````````````````` example
  5404. foo *\**
  5405. .
  5406. <p>foo <em>*</em></p>
  5407. ````````````````````````````````
  5408. ```````````````````````````````` example
  5409. foo *_*
  5410. .
  5411. <p>foo <em>_</em></p>
  5412. ````````````````````````````````
  5413. ```````````````````````````````` example
  5414. foo *****
  5415. .
  5416. <p>foo *****</p>
  5417. ````````````````````````````````
  5418. ```````````````````````````````` example
  5419. foo **\***
  5420. .
  5421. <p>foo <strong>*</strong></p>
  5422. ````````````````````````````````
  5423. ```````````````````````````````` example
  5424. foo **_**
  5425. .
  5426. <p>foo <strong>_</strong></p>
  5427. ````````````````````````````````
  5428. Note that when delimiters do not match evenly, Rule 11 determines
  5429. that the excess literal `*` characters will appear outside of the
  5430. emphasis, rather than inside it:
  5431. ```````````````````````````````` example
  5432. **foo*
  5433. .
  5434. <p>*<em>foo</em></p>
  5435. ````````````````````````````````
  5436. ```````````````````````````````` example
  5437. *foo**
  5438. .
  5439. <p><em>foo</em>*</p>
  5440. ````````````````````````````````
  5441. ```````````````````````````````` example
  5442. ***foo**
  5443. .
  5444. <p>*<strong>foo</strong></p>
  5445. ````````````````````````````````
  5446. ```````````````````````````````` example
  5447. ****foo*
  5448. .
  5449. <p>***<em>foo</em></p>
  5450. ````````````````````````````````
  5451. ```````````````````````````````` example
  5452. **foo***
  5453. .
  5454. <p><strong>foo</strong>*</p>
  5455. ````````````````````````````````
  5456. ```````````````````````````````` example
  5457. *foo****
  5458. .
  5459. <p><em>foo</em>***</p>
  5460. ````````````````````````````````
  5461. Rule 12:
  5462. ```````````````````````````````` example
  5463. foo ___
  5464. .
  5465. <p>foo ___</p>
  5466. ````````````````````````````````
  5467. ```````````````````````````````` example
  5468. foo _\__
  5469. .
  5470. <p>foo <em>_</em></p>
  5471. ````````````````````````````````
  5472. ```````````````````````````````` example
  5473. foo _*_
  5474. .
  5475. <p>foo <em>*</em></p>
  5476. ````````````````````````````````
  5477. ```````````````````````````````` example
  5478. foo _____
  5479. .
  5480. <p>foo _____</p>
  5481. ````````````````````````````````
  5482. ```````````````````````````````` example
  5483. foo __\___
  5484. .
  5485. <p>foo <strong>_</strong></p>
  5486. ````````````````````````````````
  5487. ```````````````````````````````` example
  5488. foo __*__
  5489. .
  5490. <p>foo <strong>*</strong></p>
  5491. ````````````````````````````````
  5492. ```````````````````````````````` example
  5493. __foo_
  5494. .
  5495. <p>_<em>foo</em></p>
  5496. ````````````````````````````````
  5497. Note that when delimiters do not match evenly, Rule 12 determines
  5498. that the excess literal `_` characters will appear outside of the
  5499. emphasis, rather than inside it:
  5500. ```````````````````````````````` example
  5501. _foo__
  5502. .
  5503. <p><em>foo</em>_</p>
  5504. ````````````````````````````````
  5505. ```````````````````````````````` example
  5506. ___foo__
  5507. .
  5508. <p>_<strong>foo</strong></p>
  5509. ````````````````````````````````
  5510. ```````````````````````````````` example
  5511. ____foo_
  5512. .
  5513. <p>___<em>foo</em></p>
  5514. ````````````````````````````````
  5515. ```````````````````````````````` example
  5516. __foo___
  5517. .
  5518. <p><strong>foo</strong>_</p>
  5519. ````````````````````````````````
  5520. ```````````````````````````````` example
  5521. _foo____
  5522. .
  5523. <p><em>foo</em>___</p>
  5524. ````````````````````````````````
  5525. Rule 13 implies that if you want emphasis nested directly inside
  5526. emphasis, you must use different delimiters:
  5527. ```````````````````````````````` example
  5528. **foo**
  5529. .
  5530. <p><strong>foo</strong></p>
  5531. ````````````````````````````````
  5532. ```````````````````````````````` example
  5533. *_foo_*
  5534. .
  5535. <p><em><em>foo</em></em></p>
  5536. ````````````````````````````````
  5537. ```````````````````````````````` example
  5538. __foo__
  5539. .
  5540. <p><strong>foo</strong></p>
  5541. ````````````````````````````````
  5542. ```````````````````````````````` example
  5543. _*foo*_
  5544. .
  5545. <p><em><em>foo</em></em></p>
  5546. ````````````````````````````````
  5547. However, strong emphasis within strong emphasis is possible without
  5548. switching delimiters:
  5549. ```````````````````````````````` example
  5550. ****foo****
  5551. .
  5552. <p><strong><strong>foo</strong></strong></p>
  5553. ````````````````````````````````
  5554. ```````````````````````````````` example
  5555. ____foo____
  5556. .
  5557. <p><strong><strong>foo</strong></strong></p>
  5558. ````````````````````````````````
  5559. Rule 13 can be applied to arbitrarily long sequences of
  5560. delimiters:
  5561. ```````````````````````````````` example
  5562. ******foo******
  5563. .
  5564. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5565. ````````````````````````````````
  5566. Rule 14:
  5567. ```````````````````````````````` example
  5568. ***foo***
  5569. .
  5570. <p><em><strong>foo</strong></em></p>
  5571. ````````````````````````````````
  5572. ```````````````````````````````` example
  5573. _____foo_____
  5574. .
  5575. <p><em><strong><strong>foo</strong></strong></em></p>
  5576. ````````````````````````````````
  5577. Rule 15:
  5578. ```````````````````````````````` example
  5579. *foo _bar* baz_
  5580. .
  5581. <p><em>foo _bar</em> baz_</p>
  5582. ````````````````````````````````
  5583. ```````````````````````````````` example
  5584. *foo __bar *baz bim__ bam*
  5585. .
  5586. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5587. ````````````````````````````````
  5588. Rule 16:
  5589. ```````````````````````````````` example
  5590. **foo **bar baz**
  5591. .
  5592. <p>**foo <strong>bar baz</strong></p>
  5593. ````````````````````````````````
  5594. ```````````````````````````````` example
  5595. *foo *bar baz*
  5596. .
  5597. <p>*foo <em>bar baz</em></p>
  5598. ````````````````````````````````
  5599. Rule 17:
  5600. ```````````````````````````````` example
  5601. *[bar*](/url)
  5602. .
  5603. <p>*<a href="/url">bar*</a></p>
  5604. ````````````````````````````````
  5605. ```````````````````````````````` example
  5606. _foo [bar_](/url)
  5607. .
  5608. <p>_foo <a href="/url">bar_</a></p>
  5609. ````````````````````````````````
  5610. ```````````````````````````````` example
  5611. *<img src="foo" title="*"/>
  5612. .
  5613. <p>*<img src="foo" title="*"/></p>
  5614. ````````````````````````````````
  5615. ```````````````````````````````` example
  5616. **<a href="**">
  5617. .
  5618. <p>**<a href="**"></p>
  5619. ````````````````````````````````
  5620. ```````````````````````````````` example
  5621. __<a href="__">
  5622. .
  5623. <p>__<a href="__"></p>
  5624. ````````````````````````````````
  5625. ```````````````````````````````` example
  5626. *a `*`*
  5627. .
  5628. <p><em>a <code>*</code></em></p>
  5629. ````````````````````````````````
  5630. ```````````````````````````````` example
  5631. _a `_`_
  5632. .
  5633. <p><em>a <code>_</code></em></p>
  5634. ````````````````````````````````
  5635. ```````````````````````````````` example
  5636. **a<http://foo.bar/?q=**>
  5637. .
  5638. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5639. ````````````````````````````````
  5640. ```````````````````````````````` example
  5641. __a<http://foo.bar/?q=__>
  5642. .
  5643. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5644. ````````````````````````````````
  5645. ## Links
  5646. A link contains [link text] (the visible text), a [link destination]
  5647. (the URI that is the link destination), and optionally a [link title].
  5648. There are two basic kinds of links in Markdown. In [inline links] the
  5649. destination and title are given immediately after the link text. In
  5650. [reference links] the destination and title are defined elsewhere in
  5651. the document.
  5652. A [link text](@) consists of a sequence of zero or more
  5653. inline elements enclosed by square brackets (`[` and `]`). The
  5654. following rules apply:
  5655. - Links may not contain other links, at any level of nesting. If
  5656. multiple otherwise valid link definitions appear nested inside each
  5657. other, the inner-most definition is used.
  5658. - Brackets are allowed in the [link text] only if (a) they
  5659. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5660. with an open bracket `[`, a sequence of zero or more inlines, and
  5661. a close bracket `]`.
  5662. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5663. than the brackets in link text. Thus, for example,
  5664. `` [foo`]` `` could not be a link text, since the second `]`
  5665. is part of a code span.
  5666. - The brackets in link text bind more tightly than markers for
  5667. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5668. A [link destination](@) consists of either
  5669. - a sequence of zero or more characters between an opening `<` and a
  5670. closing `>` that contains no line breaks or unescaped
  5671. `<` or `>` characters, or
  5672. - a nonempty sequence of characters that does not start with
  5673. `<`, does not include ASCII space or control characters, and
  5674. includes parentheses only if (a) they are backslash-escaped or
  5675. (b) they are part of a balanced pair of unescaped parentheses.
  5676. (Implementations may impose limits on parentheses nesting to
  5677. avoid performance issues, but at least three levels of nesting
  5678. should be supported.)
  5679. A [link title](@) consists of either
  5680. - a sequence of zero or more characters between straight double-quote
  5681. characters (`"`), including a `"` character only if it is
  5682. backslash-escaped, or
  5683. - a sequence of zero or more characters between straight single-quote
  5684. characters (`'`), including a `'` character only if it is
  5685. backslash-escaped, or
  5686. - a sequence of zero or more characters between matching parentheses
  5687. (`(...)`), including a `(` or `)` character only if it is
  5688. backslash-escaped.
  5689. Although [link titles] may span multiple lines, they may not contain
  5690. a [blank line].
  5691. An [inline link](@) consists of a [link text] followed immediately
  5692. by a left parenthesis `(`, optional [whitespace], an optional
  5693. [link destination], an optional [link title] separated from the link
  5694. destination by [whitespace], optional [whitespace], and a right
  5695. parenthesis `)`. The link's text consists of the inlines contained
  5696. in the [link text] (excluding the enclosing square brackets).
  5697. The link's URI consists of the link destination, excluding enclosing
  5698. `<...>` if present, with backslash-escapes in effect as described
  5699. above. The link's title consists of the link title, excluding its
  5700. enclosing delimiters, with backslash-escapes in effect as described
  5701. above.
  5702. Here is a simple inline link:
  5703. ```````````````````````````````` example
  5704. [link](/uri "title")
  5705. .
  5706. <p><a href="/uri" title="title">link</a></p>
  5707. ````````````````````````````````
  5708. The title may be omitted:
  5709. ```````````````````````````````` example
  5710. [link](/uri)
  5711. .
  5712. <p><a href="/uri">link</a></p>
  5713. ````````````````````````````````
  5714. Both the title and the destination may be omitted:
  5715. ```````````````````````````````` example
  5716. [link]()
  5717. .
  5718. <p><a href="">link</a></p>
  5719. ````````````````````````````````
  5720. ```````````````````````````````` example
  5721. [link](<>)
  5722. .
  5723. <p><a href="">link</a></p>
  5724. ````````````````````````````````
  5725. The destination can only contain spaces if it is
  5726. enclosed in pointy brackets:
  5727. ```````````````````````````````` example
  5728. [link](/my uri)
  5729. .
  5730. <p>[link](/my uri)</p>
  5731. ````````````````````````````````
  5732. ```````````````````````````````` example
  5733. [link](</my uri>)
  5734. .
  5735. <p><a href="/my%20uri">link</a></p>
  5736. ````````````````````````````````
  5737. The destination cannot contain line breaks,
  5738. even if enclosed in pointy brackets:
  5739. ```````````````````````````````` example
  5740. [link](foo
  5741. bar)
  5742. .
  5743. <p>[link](foo
  5744. bar)</p>
  5745. ````````````````````````````````
  5746. ```````````````````````````````` example
  5747. [link](<foo
  5748. bar>)
  5749. .
  5750. <p>[link](<foo
  5751. bar>)</p>
  5752. ````````````````````````````````
  5753. The destination can contain `)` if it is enclosed
  5754. in pointy brackets:
  5755. ```````````````````````````````` example
  5756. [a](<b)c>)
  5757. .
  5758. <p><a href="b)c">a</a></p>
  5759. ````````````````````````````````
  5760. Pointy brackets that enclose links must be unescaped:
  5761. ```````````````````````````````` example
  5762. [link](<foo\>)
  5763. .
  5764. <p>[link](&lt;foo&gt;)</p>
  5765. ````````````````````````````````
  5766. These are not links, because the opening pointy bracket
  5767. is not matched properly:
  5768. ```````````````````````````````` example
  5769. [a](<b)c
  5770. [a](<b)c>
  5771. [a](<b>c)
  5772. .
  5773. <p>[a](&lt;b)c
  5774. [a](&lt;b)c&gt;
  5775. [a](<b>c)</p>
  5776. ````````````````````````````````
  5777. Parentheses inside the link destination may be escaped:
  5778. ```````````````````````````````` example
  5779. [link](\(foo\))
  5780. .
  5781. <p><a href="(foo)">link</a></p>
  5782. ````````````````````````````````
  5783. Any number of parentheses are allowed without escaping, as long as they are
  5784. balanced:
  5785. ```````````````````````````````` example
  5786. [link](foo(and(bar)))
  5787. .
  5788. <p><a href="foo(and(bar))">link</a></p>
  5789. ````````````````````````````````
  5790. However, if you have unbalanced parentheses, you need to escape or use the
  5791. `<...>` form:
  5792. ```````````````````````````````` example
  5793. [link](foo\(and\(bar\))
  5794. .
  5795. <p><a href="foo(and(bar)">link</a></p>
  5796. ````````````````````````````````
  5797. ```````````````````````````````` example
  5798. [link](<foo(and(bar)>)
  5799. .
  5800. <p><a href="foo(and(bar)">link</a></p>
  5801. ````````````````````````````````
  5802. Parentheses and other symbols can also be escaped, as usual
  5803. in Markdown:
  5804. ```````````````````````````````` example
  5805. [link](foo\)\:)
  5806. .
  5807. <p><a href="foo):">link</a></p>
  5808. ````````````````````````````````
  5809. A link can contain fragment identifiers and queries:
  5810. ```````````````````````````````` example
  5811. [link](#fragment)
  5812. [link](http://example.com#fragment)
  5813. [link](http://example.com?foo=3#frag)
  5814. .
  5815. <p><a href="#fragment">link</a></p>
  5816. <p><a href="http://example.com#fragment">link</a></p>
  5817. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5818. ````````````````````````````````
  5819. Note that a backslash before a non-escapable character is
  5820. just a backslash:
  5821. ```````````````````````````````` example
  5822. [link](foo\bar)
  5823. .
  5824. <p><a href="foo%5Cbar">link</a></p>
  5825. ````````````````````````````````
  5826. URL-escaping should be left alone inside the destination, as all
  5827. URL-escaped characters are also valid URL characters. Entity and
  5828. numerical character references in the destination will be parsed
  5829. into the corresponding Unicode code points, as usual. These may
  5830. be optionally URL-escaped when written as HTML, but this spec
  5831. does not enforce any particular policy for rendering URLs in
  5832. HTML or other formats. Renderers may make different decisions
  5833. about how to escape or normalize URLs in the output.
  5834. ```````````````````````````````` example
  5835. [link](foo%20b&auml;)
  5836. .
  5837. <p><a href="foo%20b%C3%A4">link</a></p>
  5838. ````````````````````````````````
  5839. Note that, because titles can often be parsed as destinations,
  5840. if you try to omit the destination and keep the title, you'll
  5841. get unexpected results:
  5842. ```````````````````````````````` example
  5843. [link]("title")
  5844. .
  5845. <p><a href="%22title%22">link</a></p>
  5846. ````````````````````````````````
  5847. Titles may be in single quotes, double quotes, or parentheses:
  5848. ```````````````````````````````` example
  5849. [link](/url "title")
  5850. [link](/url 'title')
  5851. [link](/url (title))
  5852. .
  5853. <p><a href="/url" title="title">link</a>
  5854. <a href="/url" title="title">link</a>
  5855. <a href="/url" title="title">link</a></p>
  5856. ````````````````````````````````
  5857. Backslash escapes and entity and numeric character references
  5858. may be used in titles:
  5859. ```````````````````````````````` example
  5860. [link](/url "title \"&quot;")
  5861. .
  5862. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5863. ````````````````````````````````
  5864. Titles must be separated from the link using a [whitespace].
  5865. Other [Unicode whitespace] like non-breaking space doesn't work.
  5866. ```````````````````````````````` example
  5867. [link](/url "title")
  5868. .
  5869. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5870. ````````````````````````````````
  5871. Nested balanced quotes are not allowed without escaping:
  5872. ```````````````````````````````` example
  5873. [link](/url "title "and" title")
  5874. .
  5875. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5876. ````````````````````````````````
  5877. But it is easy to work around this by using a different quote type:
  5878. ```````````````````````````````` example
  5879. [link](/url 'title "and" title')
  5880. .
  5881. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5882. ````````````````````````````````
  5883. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5884. title, and its test suite included a test demonstrating this.
  5885. But it is hard to see a good rationale for the extra complexity this
  5886. brings, since there are already many ways---backslash escaping,
  5887. entity and numeric character references, or using a different
  5888. quote type for the enclosing title---to write titles containing
  5889. double quotes. `Markdown.pl`'s handling of titles has a number
  5890. of other strange features. For example, it allows single-quoted
  5891. titles in inline links, but not reference links. And, in
  5892. reference links but not inline links, it allows a title to begin
  5893. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5894. titles with no closing quotation mark, though 1.0.2b8 does not.
  5895. It seems preferable to adopt a simple, rational rule that works
  5896. the same way in inline links and link reference definitions.)
  5897. [Whitespace] is allowed around the destination and title:
  5898. ```````````````````````````````` example
  5899. [link]( /uri
  5900. "title" )
  5901. .
  5902. <p><a href="/uri" title="title">link</a></p>
  5903. ````````````````````````````````
  5904. But it is not allowed between the link text and the
  5905. following parenthesis:
  5906. ```````````````````````````````` example
  5907. [link] (/uri)
  5908. .
  5909. <p>[link] (/uri)</p>
  5910. ````````````````````````````````
  5911. The link text may contain balanced brackets, but not unbalanced ones,
  5912. unless they are escaped:
  5913. ```````````````````````````````` example
  5914. [link [foo [bar]]](/uri)
  5915. .
  5916. <p><a href="/uri">link [foo [bar]]</a></p>
  5917. ````````````````````````````````
  5918. ```````````````````````````````` example
  5919. [link] bar](/uri)
  5920. .
  5921. <p>[link] bar](/uri)</p>
  5922. ````````````````````````````````
  5923. ```````````````````````````````` example
  5924. [link [bar](/uri)
  5925. .
  5926. <p>[link <a href="/uri">bar</a></p>
  5927. ````````````````````````````````
  5928. ```````````````````````````````` example
  5929. [link \[bar](/uri)
  5930. .
  5931. <p><a href="/uri">link [bar</a></p>
  5932. ````````````````````````````````
  5933. The link text may contain inline content:
  5934. ```````````````````````````````` example
  5935. [link *foo **bar** `#`*](/uri)
  5936. .
  5937. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5938. ````````````````````````````````
  5939. ```````````````````````````````` example
  5940. [![moon](moon.jpg)](/uri)
  5941. .
  5942. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5943. ````````````````````````````````
  5944. However, links may not contain other links, at any level of nesting.
  5945. ```````````````````````````````` example
  5946. [foo [bar](/uri)](/uri)
  5947. .
  5948. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5949. ````````````````````````````````
  5950. ```````````````````````````````` example
  5951. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5952. .
  5953. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5954. ````````````````````````````````
  5955. ```````````````````````````````` example
  5956. ![[[foo](uri1)](uri2)](uri3)
  5957. .
  5958. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5959. ````````````````````````````````
  5960. These cases illustrate the precedence of link text grouping over
  5961. emphasis grouping:
  5962. ```````````````````````````````` example
  5963. *[foo*](/uri)
  5964. .
  5965. <p>*<a href="/uri">foo*</a></p>
  5966. ````````````````````````````````
  5967. ```````````````````````````````` example
  5968. [foo *bar](baz*)
  5969. .
  5970. <p><a href="baz*">foo *bar</a></p>
  5971. ````````````````````````````````
  5972. Note that brackets that *aren't* part of links do not take
  5973. precedence:
  5974. ```````````````````````````````` example
  5975. *foo [bar* baz]
  5976. .
  5977. <p><em>foo [bar</em> baz]</p>
  5978. ````````````````````````````````
  5979. These cases illustrate the precedence of HTML tags, code spans,
  5980. and autolinks over link grouping:
  5981. ```````````````````````````````` example
  5982. [foo <bar attr="](baz)">
  5983. .
  5984. <p>[foo <bar attr="](baz)"></p>
  5985. ````````````````````````````````
  5986. ```````````````````````````````` example
  5987. [foo`](/uri)`
  5988. .
  5989. <p>[foo<code>](/uri)</code></p>
  5990. ````````````````````````````````
  5991. ```````````````````````````````` example
  5992. [foo<http://example.com/?search=](uri)>
  5993. .
  5994. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5995. ````````````````````````````````
  5996. There are three kinds of [reference link](@)s:
  5997. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5998. and [shortcut](#shortcut-reference-link).
  5999. A [full reference link](@)
  6000. consists of a [link text] immediately followed by a [link label]
  6001. that [matches] a [link reference definition] elsewhere in the document.
  6002. A [link label](@) begins with a left bracket (`[`) and ends
  6003. with the first right bracket (`]`) that is not backslash-escaped.
  6004. Between these brackets there must be at least one [non-whitespace character].
  6005. Unescaped square bracket characters are not allowed inside the
  6006. opening and closing square brackets of [link labels]. A link
  6007. label can have at most 999 characters inside the square
  6008. brackets.
  6009. One label [matches](@)
  6010. another just in case their normalized forms are equal. To normalize a
  6011. label, strip off the opening and closing brackets,
  6012. perform the *Unicode case fold*, strip leading and trailing
  6013. [whitespace] and collapse consecutive internal
  6014. [whitespace] to a single space. If there are multiple
  6015. matching reference link definitions, the one that comes first in the
  6016. document is used. (It is desirable in such cases to emit a warning.)
  6017. The contents of the first link label are parsed as inlines, which are
  6018. used as the link's text. The link's URI and title are provided by the
  6019. matching [link reference definition].
  6020. Here is a simple example:
  6021. ```````````````````````````````` example
  6022. [foo][bar]
  6023. [bar]: /url "title"
  6024. .
  6025. <p><a href="/url" title="title">foo</a></p>
  6026. ````````````````````````````````
  6027. The rules for the [link text] are the same as with
  6028. [inline links]. Thus:
  6029. The link text may contain balanced brackets, but not unbalanced ones,
  6030. unless they are escaped:
  6031. ```````````````````````````````` example
  6032. [link [foo [bar]]][ref]
  6033. [ref]: /uri
  6034. .
  6035. <p><a href="/uri">link [foo [bar]]</a></p>
  6036. ````````````````````````````````
  6037. ```````````````````````````````` example
  6038. [link \[bar][ref]
  6039. [ref]: /uri
  6040. .
  6041. <p><a href="/uri">link [bar</a></p>
  6042. ````````````````````````````````
  6043. The link text may contain inline content:
  6044. ```````````````````````````````` example
  6045. [link *foo **bar** `#`*][ref]
  6046. [ref]: /uri
  6047. .
  6048. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6049. ````````````````````````````````
  6050. ```````````````````````````````` example
  6051. [![moon](moon.jpg)][ref]
  6052. [ref]: /uri
  6053. .
  6054. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6055. ````````````````````````````````
  6056. However, links may not contain other links, at any level of nesting.
  6057. ```````````````````````````````` example
  6058. [foo [bar](/uri)][ref]
  6059. [ref]: /uri
  6060. .
  6061. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6062. ````````````````````````````````
  6063. ```````````````````````````````` example
  6064. [foo *bar [baz][ref]*][ref]
  6065. [ref]: /uri
  6066. .
  6067. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6068. ````````````````````````````````
  6069. (In the examples above, we have two [shortcut reference links]
  6070. instead of one [full reference link].)
  6071. The following cases illustrate the precedence of link text grouping over
  6072. emphasis grouping:
  6073. ```````````````````````````````` example
  6074. *[foo*][ref]
  6075. [ref]: /uri
  6076. .
  6077. <p>*<a href="/uri">foo*</a></p>
  6078. ````````````````````````````````
  6079. ```````````````````````````````` example
  6080. [foo *bar][ref]
  6081. [ref]: /uri
  6082. .
  6083. <p><a href="/uri">foo *bar</a></p>
  6084. ````````````````````````````````
  6085. These cases illustrate the precedence of HTML tags, code spans,
  6086. and autolinks over link grouping:
  6087. ```````````````````````````````` example
  6088. [foo <bar attr="][ref]">
  6089. [ref]: /uri
  6090. .
  6091. <p>[foo <bar attr="][ref]"></p>
  6092. ````````````````````````````````
  6093. ```````````````````````````````` example
  6094. [foo`][ref]`
  6095. [ref]: /uri
  6096. .
  6097. <p>[foo<code>][ref]</code></p>
  6098. ````````````````````````````````
  6099. ```````````````````````````````` example
  6100. [foo<http://example.com/?search=][ref]>
  6101. [ref]: /uri
  6102. .
  6103. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6104. ````````````````````````````````
  6105. Matching is case-insensitive:
  6106. ```````````````````````````````` example
  6107. [foo][BaR]
  6108. [bar]: /url "title"
  6109. .
  6110. <p><a href="/url" title="title">foo</a></p>
  6111. ````````````````````````````````
  6112. Unicode case fold is used:
  6113. ```````````````````````````````` example
  6114. [Толпой][Толпой] is a Russian word.
  6115. [ТОЛПОЙ]: /url
  6116. .
  6117. <p><a href="/url">Толпой</a> is a Russian word.</p>
  6118. ````````````````````````````````
  6119. Consecutive internal [whitespace] is treated as one space for
  6120. purposes of determining matching:
  6121. ```````````````````````````````` example
  6122. [Foo
  6123. bar]: /url
  6124. [Baz][Foo bar]
  6125. .
  6126. <p><a href="/url">Baz</a></p>
  6127. ````````````````````````````````
  6128. No [whitespace] is allowed between the [link text] and the
  6129. [link label]:
  6130. ```````````````````````````````` example
  6131. [foo] [bar]
  6132. [bar]: /url "title"
  6133. .
  6134. <p>[foo] <a href="/url" title="title">bar</a></p>
  6135. ````````````````````````````````
  6136. ```````````````````````````````` example
  6137. [foo]
  6138. [bar]
  6139. [bar]: /url "title"
  6140. .
  6141. <p>[foo]
  6142. <a href="/url" title="title">bar</a></p>
  6143. ````````````````````````````````
  6144. This is a departure from John Gruber's original Markdown syntax
  6145. description, which explicitly allows whitespace between the link
  6146. text and the link label. It brings reference links in line with
  6147. [inline links], which (according to both original Markdown and
  6148. this spec) cannot have whitespace after the link text. More
  6149. importantly, it prevents inadvertent capture of consecutive
  6150. [shortcut reference links]. If whitespace is allowed between the
  6151. link text and the link label, then in the following we will have
  6152. a single reference link, not two shortcut reference links, as
  6153. intended:
  6154. ``` markdown
  6155. [foo]
  6156. [bar]
  6157. [foo]: /url1
  6158. [bar]: /url2
  6159. ```
  6160. (Note that [shortcut reference links] were introduced by Gruber
  6161. himself in a beta version of `Markdown.pl`, but never included
  6162. in the official syntax description. Without shortcut reference
  6163. links, it is harmless to allow space between the link text and
  6164. link label; but once shortcut references are introduced, it is
  6165. too dangerous to allow this, as it frequently leads to
  6166. unintended results.)
  6167. When there are multiple matching [link reference definitions],
  6168. the first is used:
  6169. ```````````````````````````````` example
  6170. [foo]: /url1
  6171. [foo]: /url2
  6172. [bar][foo]
  6173. .
  6174. <p><a href="/url1">bar</a></p>
  6175. ````````````````````````````````
  6176. Note that matching is performed on normalized strings, not parsed
  6177. inline content. So the following does not match, even though the
  6178. labels define equivalent inline content:
  6179. ```````````````````````````````` example
  6180. [bar][foo\!]
  6181. [foo!]: /url
  6182. .
  6183. <p>[bar][foo!]</p>
  6184. ````````````````````````````````
  6185. [Link labels] cannot contain brackets, unless they are
  6186. backslash-escaped:
  6187. ```````````````````````````````` example
  6188. [foo][ref[]
  6189. [ref[]: /uri
  6190. .
  6191. <p>[foo][ref[]</p>
  6192. <p>[ref[]: /uri</p>
  6193. ````````````````````````````````
  6194. ```````````````````````````````` example
  6195. [foo][ref[bar]]
  6196. [ref[bar]]: /uri
  6197. .
  6198. <p>[foo][ref[bar]]</p>
  6199. <p>[ref[bar]]: /uri</p>
  6200. ````````````````````````````````
  6201. ```````````````````````````````` example
  6202. [[[foo]]]
  6203. [[[foo]]]: /url
  6204. .
  6205. <p>[[[foo]]]</p>
  6206. <p>[[[foo]]]: /url</p>
  6207. ````````````````````````````````
  6208. ```````````````````````````````` example
  6209. [foo][ref\[]
  6210. [ref\[]: /uri
  6211. .
  6212. <p><a href="/uri">foo</a></p>
  6213. ````````````````````````````````
  6214. Note that in this example `]` is not backslash-escaped:
  6215. ```````````````````````````````` example
  6216. [bar\\]: /uri
  6217. [bar\\]
  6218. .
  6219. <p><a href="/uri">bar\</a></p>
  6220. ````````````````````````````````
  6221. A [link label] must contain at least one [non-whitespace character]:
  6222. ```````````````````````````````` example
  6223. []
  6224. []: /uri
  6225. .
  6226. <p>[]</p>
  6227. <p>[]: /uri</p>
  6228. ````````````````````````````````
  6229. ```````````````````````````````` example
  6230. [
  6231. ]
  6232. [
  6233. ]: /uri
  6234. .
  6235. <p>[
  6236. ]</p>
  6237. <p>[
  6238. ]: /uri</p>
  6239. ````````````````````````````````
  6240. A [collapsed reference link](@)
  6241. consists of a [link label] that [matches] a
  6242. [link reference definition] elsewhere in the
  6243. document, followed by the string `[]`.
  6244. The contents of the first link label are parsed as inlines,
  6245. which are used as the link's text. The link's URI and title are
  6246. provided by the matching reference link definition. Thus,
  6247. `[foo][]` is equivalent to `[foo][foo]`.
  6248. ```````````````````````````````` example
  6249. [foo][]
  6250. [foo]: /url "title"
  6251. .
  6252. <p><a href="/url" title="title">foo</a></p>
  6253. ````````````````````````````````
  6254. ```````````````````````````````` example
  6255. [*foo* bar][]
  6256. [*foo* bar]: /url "title"
  6257. .
  6258. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6259. ````````````````````````````````
  6260. The link labels are case-insensitive:
  6261. ```````````````````````````````` example
  6262. [Foo][]
  6263. [foo]: /url "title"
  6264. .
  6265. <p><a href="/url" title="title">Foo</a></p>
  6266. ````````````````````````````````
  6267. As with full reference links, [whitespace] is not
  6268. allowed between the two sets of brackets:
  6269. ```````````````````````````````` example
  6270. [foo]
  6271. []
  6272. [foo]: /url "title"
  6273. .
  6274. <p><a href="/url" title="title">foo</a>
  6275. []</p>
  6276. ````````````````````````````````
  6277. A [shortcut reference link](@)
  6278. consists of a [link label] that [matches] a
  6279. [link reference definition] elsewhere in the
  6280. document and is not followed by `[]` or a link label.
  6281. The contents of the first link label are parsed as inlines,
  6282. which are used as the link's text. The link's URI and title
  6283. are provided by the matching link reference definition.
  6284. Thus, `[foo]` is equivalent to `[foo][]`.
  6285. ```````````````````````````````` example
  6286. [foo]
  6287. [foo]: /url "title"
  6288. .
  6289. <p><a href="/url" title="title">foo</a></p>
  6290. ````````````````````````````````
  6291. ```````````````````````````````` example
  6292. [*foo* bar]
  6293. [*foo* bar]: /url "title"
  6294. .
  6295. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6296. ````````````````````````````````
  6297. ```````````````````````````````` example
  6298. [[*foo* bar]]
  6299. [*foo* bar]: /url "title"
  6300. .
  6301. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6302. ````````````````````````````````
  6303. ```````````````````````````````` example
  6304. [[bar [foo]
  6305. [foo]: /url
  6306. .
  6307. <p>[[bar <a href="/url">foo</a></p>
  6308. ````````````````````````````````
  6309. The link labels are case-insensitive:
  6310. ```````````````````````````````` example
  6311. [Foo]
  6312. [foo]: /url "title"
  6313. .
  6314. <p><a href="/url" title="title">Foo</a></p>
  6315. ````````````````````````````````
  6316. A space after the link text should be preserved:
  6317. ```````````````````````````````` example
  6318. [foo] bar
  6319. [foo]: /url
  6320. .
  6321. <p><a href="/url">foo</a> bar</p>
  6322. ````````````````````````````````
  6323. If you just want bracketed text, you can backslash-escape the
  6324. opening bracket to avoid links:
  6325. ```````````````````````````````` example
  6326. \[foo]
  6327. [foo]: /url "title"
  6328. .
  6329. <p>[foo]</p>
  6330. ````````````````````````````````
  6331. Note that this is a link, because a link label ends with the first
  6332. following closing bracket:
  6333. ```````````````````````````````` example
  6334. [foo*]: /url
  6335. *[foo*]
  6336. .
  6337. <p>*<a href="/url">foo*</a></p>
  6338. ````````````````````````````````
  6339. Full and compact references take precedence over shortcut
  6340. references:
  6341. ```````````````````````````````` example
  6342. [foo][bar]
  6343. [foo]: /url1
  6344. [bar]: /url2
  6345. .
  6346. <p><a href="/url2">foo</a></p>
  6347. ````````````````````````````````
  6348. ```````````````````````````````` example
  6349. [foo][]
  6350. [foo]: /url1
  6351. .
  6352. <p><a href="/url1">foo</a></p>
  6353. ````````````````````````````````
  6354. Inline links also take precedence:
  6355. ```````````````````````````````` example
  6356. [foo]()
  6357. [foo]: /url1
  6358. .
  6359. <p><a href="">foo</a></p>
  6360. ````````````````````````````````
  6361. ```````````````````````````````` example
  6362. [foo](not a link)
  6363. [foo]: /url1
  6364. .
  6365. <p><a href="/url1">foo</a>(not a link)</p>
  6366. ````````````````````````````````
  6367. In the following case `[bar][baz]` is parsed as a reference,
  6368. `[foo]` as normal text:
  6369. ```````````````````````````````` example
  6370. [foo][bar][baz]
  6371. [baz]: /url
  6372. .
  6373. <p>[foo]<a href="/url">bar</a></p>
  6374. ````````````````````````````````
  6375. Here, though, `[foo][bar]` is parsed as a reference, since
  6376. `[bar]` is defined:
  6377. ```````````````````````````````` example
  6378. [foo][bar][baz]
  6379. [baz]: /url1
  6380. [bar]: /url2
  6381. .
  6382. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6383. ````````````````````````````````
  6384. Here `[foo]` is not parsed as a shortcut reference, because it
  6385. is followed by a link label (even though `[bar]` is not defined):
  6386. ```````````````````````````````` example
  6387. [foo][bar][baz]
  6388. [baz]: /url1
  6389. [foo]: /url2
  6390. .
  6391. <p>[foo]<a href="/url1">bar</a></p>
  6392. ````````````````````````````````
  6393. ## Images
  6394. Syntax for images is like the syntax for links, with one
  6395. difference. Instead of [link text], we have an
  6396. [image description](@). The rules for this are the
  6397. same as for [link text], except that (a) an
  6398. image description starts with `![` rather than `[`, and
  6399. (b) an image description may contain links.
  6400. An image description has inline elements
  6401. as its contents. When an image is rendered to HTML,
  6402. this is standardly used as the image's `alt` attribute.
  6403. ```````````````````````````````` example
  6404. ![foo](/url "title")
  6405. .
  6406. <p><img src="/url" alt="foo" title="title" /></p>
  6407. ````````````````````````````````
  6408. ```````````````````````````````` example
  6409. ![foo *bar*]
  6410. [foo *bar*]: train.jpg "train & tracks"
  6411. .
  6412. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6413. ````````````````````````````````
  6414. ```````````````````````````````` example
  6415. ![foo ![bar](/url)](/url2)
  6416. .
  6417. <p><img src="/url2" alt="foo bar" /></p>
  6418. ````````````````````````````````
  6419. ```````````````````````````````` example
  6420. ![foo [bar](/url)](/url2)
  6421. .
  6422. <p><img src="/url2" alt="foo bar" /></p>
  6423. ````````````````````````````````
  6424. Though this spec is concerned with parsing, not rendering, it is
  6425. recommended that in rendering to HTML, only the plain string content
  6426. of the [image description] be used. Note that in
  6427. the above example, the alt attribute's value is `foo bar`, not `foo
  6428. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6429. content is rendered, without formatting.
  6430. ```````````````````````````````` example
  6431. ![foo *bar*][]
  6432. [foo *bar*]: train.jpg "train & tracks"
  6433. .
  6434. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6435. ````````````````````````````````
  6436. ```````````````````````````````` example
  6437. ![foo *bar*][foobar]
  6438. [FOOBAR]: train.jpg "train & tracks"
  6439. .
  6440. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6441. ````````````````````````````````
  6442. ```````````````````````````````` example
  6443. ![foo](train.jpg)
  6444. .
  6445. <p><img src="train.jpg" alt="foo" /></p>
  6446. ````````````````````````````````
  6447. ```````````````````````````````` example
  6448. My ![foo bar](/path/to/train.jpg "title" )
  6449. .
  6450. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6451. ````````````````````````````````
  6452. ```````````````````````````````` example
  6453. ![foo](<url>)
  6454. .
  6455. <p><img src="url" alt="foo" /></p>
  6456. ````````````````````````````````
  6457. ```````````````````````````````` example
  6458. ![](/url)
  6459. .
  6460. <p><img src="/url" alt="" /></p>
  6461. ````````````````````````````````
  6462. Reference-style:
  6463. ```````````````````````````````` example
  6464. ![foo][bar]
  6465. [bar]: /url
  6466. .
  6467. <p><img src="/url" alt="foo" /></p>
  6468. ````````````````````````````````
  6469. ```````````````````````````````` example
  6470. ![foo][bar]
  6471. [BAR]: /url
  6472. .
  6473. <p><img src="/url" alt="foo" /></p>
  6474. ````````````````````````````````
  6475. Collapsed:
  6476. ```````````````````````````````` example
  6477. ![foo][]
  6478. [foo]: /url "title"
  6479. .
  6480. <p><img src="/url" alt="foo" title="title" /></p>
  6481. ````````````````````````````````
  6482. ```````````````````````````````` example
  6483. ![*foo* bar][]
  6484. [*foo* bar]: /url "title"
  6485. .
  6486. <p><img src="/url" alt="foo bar" title="title" /></p>
  6487. ````````````````````````````````
  6488. The labels are case-insensitive:
  6489. ```````````````````````````````` example
  6490. ![Foo][]
  6491. [foo]: /url "title"
  6492. .
  6493. <p><img src="/url" alt="Foo" title="title" /></p>
  6494. ````````````````````````````````
  6495. As with reference links, [whitespace] is not allowed
  6496. between the two sets of brackets:
  6497. ```````````````````````````````` example
  6498. ![foo]
  6499. []
  6500. [foo]: /url "title"
  6501. .
  6502. <p><img src="/url" alt="foo" title="title" />
  6503. []</p>
  6504. ````````````````````````````````
  6505. Shortcut:
  6506. ```````````````````````````````` example
  6507. ![foo]
  6508. [foo]: /url "title"
  6509. .
  6510. <p><img src="/url" alt="foo" title="title" /></p>
  6511. ````````````````````````````````
  6512. ```````````````````````````````` example
  6513. ![*foo* bar]
  6514. [*foo* bar]: /url "title"
  6515. .
  6516. <p><img src="/url" alt="foo bar" title="title" /></p>
  6517. ````````````````````````````````
  6518. Note that link labels cannot contain unescaped brackets:
  6519. ```````````````````````````````` example
  6520. ![[foo]]
  6521. [[foo]]: /url "title"
  6522. .
  6523. <p>![[foo]]</p>
  6524. <p>[[foo]]: /url &quot;title&quot;</p>
  6525. ````````````````````````````````
  6526. The link labels are case-insensitive:
  6527. ```````````````````````````````` example
  6528. ![Foo]
  6529. [foo]: /url "title"
  6530. .
  6531. <p><img src="/url" alt="Foo" title="title" /></p>
  6532. ````````````````````````````````
  6533. If you just want a literal `!` followed by bracketed text, you can
  6534. backslash-escape the opening `[`:
  6535. ```````````````````````````````` example
  6536. !\[foo]
  6537. [foo]: /url "title"
  6538. .
  6539. <p>![foo]</p>
  6540. ````````````````````````````````
  6541. If you want a link after a literal `!`, backslash-escape the
  6542. `!`:
  6543. ```````````````````````````````` example
  6544. \![foo]
  6545. [foo]: /url "title"
  6546. .
  6547. <p>!<a href="/url" title="title">foo</a></p>
  6548. ````````````````````````````````
  6549. ## Autolinks
  6550. [Autolink](@)s are absolute URIs and email addresses inside
  6551. `<` and `>`. They are parsed as links, with the URL or email address
  6552. as the link label.
  6553. A [URI autolink](@) consists of `<`, followed by an
  6554. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6555. a link to the URI, with the URI as the link's label.
  6556. An [absolute URI](@),
  6557. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6558. followed by zero or more characters other than ASCII
  6559. [whitespace] and control characters, `<`, and `>`. If
  6560. the URI includes these characters, they must be percent-encoded
  6561. (e.g. `%20` for a space).
  6562. For purposes of this spec, a [scheme](@) is any sequence
  6563. of 2--32 characters beginning with an ASCII letter and followed
  6564. by any combination of ASCII letters, digits, or the symbols plus
  6565. ("+"), period ("."), or hyphen ("-").
  6566. Here are some valid autolinks:
  6567. ```````````````````````````````` example
  6568. <http://foo.bar.baz>
  6569. .
  6570. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6571. ````````````````````````````````
  6572. ```````````````````````````````` example
  6573. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6574. .
  6575. <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>
  6576. ````````````````````````````````
  6577. ```````````````````````````````` example
  6578. <irc://foo.bar:2233/baz>
  6579. .
  6580. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6581. ````````````````````````````````
  6582. Uppercase is also fine:
  6583. ```````````````````````````````` example
  6584. <MAILTO:FOO@BAR.BAZ>
  6585. .
  6586. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6587. ````````````````````````````````
  6588. Note that many strings that count as [absolute URIs] for
  6589. purposes of this spec are not valid URIs, because their
  6590. schemes are not registered or because of other problems
  6591. with their syntax:
  6592. ```````````````````````````````` example
  6593. <a+b+c:d>
  6594. .
  6595. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6596. ````````````````````````````````
  6597. ```````````````````````````````` example
  6598. <made-up-scheme://foo,bar>
  6599. .
  6600. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6601. ````````````````````````````````
  6602. ```````````````````````````````` example
  6603. <http://../>
  6604. .
  6605. <p><a href="http://../">http://../</a></p>
  6606. ````````````````````````````````
  6607. ```````````````````````````````` example
  6608. <localhost:5001/foo>
  6609. .
  6610. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6611. ````````````````````````````````
  6612. Spaces are not allowed in autolinks:
  6613. ```````````````````````````````` example
  6614. <http://foo.bar/baz bim>
  6615. .
  6616. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6617. ````````````````````````````````
  6618. Backslash-escapes do not work inside autolinks:
  6619. ```````````````````````````````` example
  6620. <http://example.com/\[\>
  6621. .
  6622. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6623. ````````````````````````````````
  6624. An [email autolink](@)
  6625. consists of `<`, followed by an [email address],
  6626. followed by `>`. The link's label is the email address,
  6627. and the URL is `mailto:` followed by the email address.
  6628. An [email address](@),
  6629. for these purposes, is anything that matches
  6630. the [non-normative regex from the HTML5
  6631. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6632. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6633. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6634. Examples of email autolinks:
  6635. ```````````````````````````````` example
  6636. <foo@bar.example.com>
  6637. .
  6638. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6639. ````````````````````````````````
  6640. ```````````````````````````````` example
  6641. <foo+special@Bar.baz-bar0.com>
  6642. .
  6643. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6644. ````````````````````````````````
  6645. Backslash-escapes do not work inside email autolinks:
  6646. ```````````````````````````````` example
  6647. <foo\+@bar.example.com>
  6648. .
  6649. <p>&lt;foo+@bar.example.com&gt;</p>
  6650. ````````````````````````````````
  6651. These are not autolinks:
  6652. ```````````````````````````````` example
  6653. <>
  6654. .
  6655. <p>&lt;&gt;</p>
  6656. ````````````````````````````````
  6657. ```````````````````````````````` example
  6658. < http://foo.bar >
  6659. .
  6660. <p>&lt; http://foo.bar &gt;</p>
  6661. ````````````````````````````````
  6662. ```````````````````````````````` example
  6663. <m:abc>
  6664. .
  6665. <p>&lt;m:abc&gt;</p>
  6666. ````````````````````````````````
  6667. ```````````````````````````````` example
  6668. <foo.bar.baz>
  6669. .
  6670. <p>&lt;foo.bar.baz&gt;</p>
  6671. ````````````````````````````````
  6672. ```````````````````````````````` example
  6673. http://example.com
  6674. .
  6675. <p>http://example.com</p>
  6676. ````````````````````````````````
  6677. ```````````````````````````````` example
  6678. foo@bar.example.com
  6679. .
  6680. <p>foo@bar.example.com</p>
  6681. ````````````````````````````````
  6682. ## Raw HTML
  6683. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6684. raw HTML tag and will be rendered in HTML without escaping.
  6685. Tag and attribute names are not limited to current HTML tags,
  6686. so custom tags (and even, say, DocBook tags) may be used.
  6687. Here is the grammar for tags:
  6688. A [tag name](@) consists of an ASCII letter
  6689. followed by zero or more ASCII letters, digits, or
  6690. hyphens (`-`).
  6691. An [attribute](@) consists of [whitespace],
  6692. an [attribute name], and an optional
  6693. [attribute value specification].
  6694. An [attribute name](@)
  6695. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6696. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6697. specification restricted to ASCII. HTML5 is laxer.)
  6698. An [attribute value specification](@)
  6699. consists of optional [whitespace],
  6700. a `=` character, optional [whitespace], and an [attribute
  6701. value].
  6702. An [attribute value](@)
  6703. consists of an [unquoted attribute value],
  6704. a [single-quoted attribute value], or a [double-quoted attribute value].
  6705. An [unquoted attribute value](@)
  6706. is a nonempty string of characters not
  6707. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6708. A [single-quoted attribute value](@)
  6709. consists of `'`, zero or more
  6710. characters not including `'`, and a final `'`.
  6711. A [double-quoted attribute value](@)
  6712. consists of `"`, zero or more
  6713. characters not including `"`, and a final `"`.
  6714. An [open tag](@) consists of a `<` character, a [tag name],
  6715. zero or more [attributes], optional [whitespace], an optional `/`
  6716. character, and a `>` character.
  6717. A [closing tag](@) consists of the string `</`, a
  6718. [tag name], optional [whitespace], and the character `>`.
  6719. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6720. where *text* does not start with `>` or `->`, does not end with `-`,
  6721. and does not contain `--`. (See the
  6722. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6723. A [processing instruction](@)
  6724. consists of the string `<?`, a string
  6725. of characters not including the string `?>`, and the string
  6726. `?>`.
  6727. A [declaration](@) consists of the
  6728. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6729. [whitespace], a string of characters not including the
  6730. character `>`, and the character `>`.
  6731. A [CDATA section](@) consists of
  6732. the string `<![CDATA[`, a string of characters not including the string
  6733. `]]>`, and the string `]]>`.
  6734. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6735. an [HTML comment], a [processing instruction], a [declaration],
  6736. or a [CDATA section].
  6737. Here are some simple open tags:
  6738. ```````````````````````````````` example
  6739. <a><bab><c2c>
  6740. .
  6741. <p><a><bab><c2c></p>
  6742. ````````````````````````````````
  6743. Empty elements:
  6744. ```````````````````````````````` example
  6745. <a/><b2/>
  6746. .
  6747. <p><a/><b2/></p>
  6748. ````````````````````````````````
  6749. [Whitespace] is allowed:
  6750. ```````````````````````````````` example
  6751. <a /><b2
  6752. data="foo" >
  6753. .
  6754. <p><a /><b2
  6755. data="foo" ></p>
  6756. ````````````````````````````````
  6757. With attributes:
  6758. ```````````````````````````````` example
  6759. <a foo="bar" bam = 'baz <em>"</em>'
  6760. _boolean zoop:33=zoop:33 />
  6761. .
  6762. <p><a foo="bar" bam = 'baz <em>"</em>'
  6763. _boolean zoop:33=zoop:33 /></p>
  6764. ````````````````````````````````
  6765. Custom tag names can be used:
  6766. ```````````````````````````````` example
  6767. Foo <responsive-image src="foo.jpg" />
  6768. .
  6769. <p>Foo <responsive-image src="foo.jpg" /></p>
  6770. ````````````````````````````````
  6771. Illegal tag names, not parsed as HTML:
  6772. ```````````````````````````````` example
  6773. <33> <__>
  6774. .
  6775. <p>&lt;33&gt; &lt;__&gt;</p>
  6776. ````````````````````````````````
  6777. Illegal attribute names:
  6778. ```````````````````````````````` example
  6779. <a h*#ref="hi">
  6780. .
  6781. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6782. ````````````````````````````````
  6783. Illegal attribute values:
  6784. ```````````````````````````````` example
  6785. <a href="hi'> <a href=hi'>
  6786. .
  6787. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6788. ````````````````````````````````
  6789. Illegal [whitespace]:
  6790. ```````````````````````````````` example
  6791. < a><
  6792. foo><bar/ >
  6793. <foo bar=baz
  6794. bim!bop />
  6795. .
  6796. <p>&lt; a&gt;&lt;
  6797. foo&gt;&lt;bar/ &gt;
  6798. &lt;foo bar=baz
  6799. bim!bop /&gt;</p>
  6800. ````````````````````````````````
  6801. Missing [whitespace]:
  6802. ```````````````````````````````` example
  6803. <a href='bar'title=title>
  6804. .
  6805. <p>&lt;a href='bar'title=title&gt;</p>
  6806. ````````````````````````````````
  6807. Closing tags:
  6808. ```````````````````````````````` example
  6809. </a></foo >
  6810. .
  6811. <p></a></foo ></p>
  6812. ````````````````````````````````
  6813. Illegal attributes in closing tag:
  6814. ```````````````````````````````` example
  6815. </a href="foo">
  6816. .
  6817. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6818. ````````````````````````````````
  6819. Comments:
  6820. ```````````````````````````````` example
  6821. foo <!-- this is a
  6822. comment - with hyphen -->
  6823. .
  6824. <p>foo <!-- this is a
  6825. comment - with hyphen --></p>
  6826. ````````````````````````````````
  6827. ```````````````````````````````` example
  6828. foo <!-- not a comment -- two hyphens -->
  6829. .
  6830. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6831. ````````````````````````````````
  6832. Not comments:
  6833. ```````````````````````````````` example
  6834. foo <!--> foo -->
  6835. foo <!-- foo--->
  6836. .
  6837. <p>foo &lt;!--&gt; foo --&gt;</p>
  6838. <p>foo &lt;!-- foo---&gt;</p>
  6839. ````````````````````````````````
  6840. Processing instructions:
  6841. ```````````````````````````````` example
  6842. foo <?php echo $a; ?>
  6843. .
  6844. <p>foo <?php echo $a; ?></p>
  6845. ````````````````````````````````
  6846. Declarations:
  6847. ```````````````````````````````` example
  6848. foo <!ELEMENT br EMPTY>
  6849. .
  6850. <p>foo <!ELEMENT br EMPTY></p>
  6851. ````````````````````````````````
  6852. CDATA sections:
  6853. ```````````````````````````````` example
  6854. foo <![CDATA[>&<]]>
  6855. .
  6856. <p>foo <![CDATA[>&<]]></p>
  6857. ````````````````````````````````
  6858. Entity and numeric character references are preserved in HTML
  6859. attributes:
  6860. ```````````````````````````````` example
  6861. foo <a href="&ouml;">
  6862. .
  6863. <p>foo <a href="&ouml;"></p>
  6864. ````````````````````````````````
  6865. Backslash escapes do not work in HTML attributes:
  6866. ```````````````````````````````` example
  6867. foo <a href="\*">
  6868. .
  6869. <p>foo <a href="\*"></p>
  6870. ````````````````````````````````
  6871. ```````````````````````````````` example
  6872. <a href="\"">
  6873. .
  6874. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6875. ````````````````````````````````
  6876. ## Hard line breaks
  6877. A line break (not in a code span or HTML tag) that is preceded
  6878. by two or more spaces and does not occur at the end of a block
  6879. is parsed as a [hard line break](@) (rendered
  6880. in HTML as a `<br />` tag):
  6881. ```````````````````````````````` example
  6882. foo
  6883. baz
  6884. .
  6885. <p>foo<br />
  6886. baz</p>
  6887. ````````````````````````````````
  6888. For a more visible alternative, a backslash before the
  6889. [line ending] may be used instead of two spaces:
  6890. ```````````````````````````````` example
  6891. foo\
  6892. baz
  6893. .
  6894. <p>foo<br />
  6895. baz</p>
  6896. ````````````````````````````````
  6897. More than two spaces can be used:
  6898. ```````````````````````````````` example
  6899. foo
  6900. baz
  6901. .
  6902. <p>foo<br />
  6903. baz</p>
  6904. ````````````````````````````````
  6905. Leading spaces at the beginning of the next line are ignored:
  6906. ```````````````````````````````` example
  6907. foo
  6908. bar
  6909. .
  6910. <p>foo<br />
  6911. bar</p>
  6912. ````````````````````````````````
  6913. ```````````````````````````````` example
  6914. foo\
  6915. bar
  6916. .
  6917. <p>foo<br />
  6918. bar</p>
  6919. ````````````````````````````````
  6920. Line breaks can occur inside emphasis, links, and other constructs
  6921. that allow inline content:
  6922. ```````````````````````````````` example
  6923. *foo
  6924. bar*
  6925. .
  6926. <p><em>foo<br />
  6927. bar</em></p>
  6928. ````````````````````````````````
  6929. ```````````````````````````````` example
  6930. *foo\
  6931. bar*
  6932. .
  6933. <p><em>foo<br />
  6934. bar</em></p>
  6935. ````````````````````````````````
  6936. Line breaks do not occur inside code spans
  6937. ```````````````````````````````` example
  6938. `code
  6939. span`
  6940. .
  6941. <p><code>code span</code></p>
  6942. ````````````````````````````````
  6943. ```````````````````````````````` example
  6944. `code\
  6945. span`
  6946. .
  6947. <p><code>code\ span</code></p>
  6948. ````````````````````````````````
  6949. or HTML tags:
  6950. ```````````````````````````````` example
  6951. <a href="foo
  6952. bar">
  6953. .
  6954. <p><a href="foo
  6955. bar"></p>
  6956. ````````````````````````````````
  6957. ```````````````````````````````` example
  6958. <a href="foo\
  6959. bar">
  6960. .
  6961. <p><a href="foo\
  6962. bar"></p>
  6963. ````````````````````````````````
  6964. Hard line breaks are for separating inline content within a block.
  6965. Neither syntax for hard line breaks works at the end of a paragraph or
  6966. other block element:
  6967. ```````````````````````````````` example
  6968. foo\
  6969. .
  6970. <p>foo\</p>
  6971. ````````````````````````````````
  6972. ```````````````````````````````` example
  6973. foo
  6974. .
  6975. <p>foo</p>
  6976. ````````````````````````````````
  6977. ```````````````````````````````` example
  6978. ### foo\
  6979. .
  6980. <h3>foo\</h3>
  6981. ````````````````````````````````
  6982. ```````````````````````````````` example
  6983. ### foo
  6984. .
  6985. <h3>foo</h3>
  6986. ````````````````````````````````
  6987. ## Soft line breaks
  6988. A regular line break (not in a code span or HTML tag) that is not
  6989. preceded by two or more spaces or a backslash is parsed as a
  6990. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6991. [line ending] or as a space. The result will be the same in
  6992. browsers. In the examples here, a [line ending] will be used.)
  6993. ```````````````````````````````` example
  6994. foo
  6995. baz
  6996. .
  6997. <p>foo
  6998. baz</p>
  6999. ````````````````````````````````
  7000. Spaces at the end of the line and beginning of the next line are
  7001. removed:
  7002. ```````````````````````````````` example
  7003. foo
  7004. baz
  7005. .
  7006. <p>foo
  7007. baz</p>
  7008. ````````````````````````````````
  7009. A conforming parser may render a soft line break in HTML either as a
  7010. line break or as a space.
  7011. A renderer may also provide an option to render soft line breaks
  7012. as hard line breaks.
  7013. ## Textual content
  7014. Any characters not given an interpretation by the above rules will
  7015. be parsed as plain textual content.
  7016. ```````````````````````````````` example
  7017. hello $.;'there
  7018. .
  7019. <p>hello $.;'there</p>
  7020. ````````````````````````````````
  7021. ```````````````````````````````` example
  7022. Foo χρῆν
  7023. .
  7024. <p>Foo χρῆν</p>
  7025. ````````````````````````````````
  7026. Internal spaces are preserved verbatim:
  7027. ```````````````````````````````` example
  7028. Multiple spaces
  7029. .
  7030. <p>Multiple spaces</p>
  7031. ````````````````````````````````
  7032. <!-- END TESTS -->
  7033. # Appendix: A parsing strategy
  7034. In this appendix we describe some features of the parsing strategy
  7035. used in the CommonMark reference implementations.
  7036. ## Overview
  7037. Parsing has two phases:
  7038. 1. In the first phase, lines of input are consumed and the block
  7039. structure of the document---its division into paragraphs, block quotes,
  7040. list items, and so on---is constructed. Text is assigned to these
  7041. blocks but not parsed. Link reference definitions are parsed and a
  7042. map of links is constructed.
  7043. 2. In the second phase, the raw text contents of paragraphs and headings
  7044. are parsed into sequences of Markdown inline elements (strings,
  7045. code spans, links, emphasis, and so on), using the map of link
  7046. references constructed in phase 1.
  7047. At each point in processing, the document is represented as a tree of
  7048. **blocks**. The root of the tree is a `document` block. The `document`
  7049. may have any number of other blocks as **children**. These children
  7050. may, in turn, have other blocks as children. The last child of a block
  7051. is normally considered **open**, meaning that subsequent lines of input
  7052. can alter its contents. (Blocks that are not open are **closed**.)
  7053. Here, for example, is a possible document tree, with the open blocks
  7054. marked by arrows:
  7055. ``` tree
  7056. -> document
  7057. -> block_quote
  7058. paragraph
  7059. "Lorem ipsum dolor\nsit amet."
  7060. -> list (type=bullet tight=true bullet_char=-)
  7061. list_item
  7062. paragraph
  7063. "Qui *quodsi iracundia*"
  7064. -> list_item
  7065. -> paragraph
  7066. "aliquando id"
  7067. ```
  7068. ## Phase 1: block structure
  7069. Each line that is processed has an effect on this tree. The line is
  7070. analyzed and, depending on its contents, the document may be altered
  7071. in one or more of the following ways:
  7072. 1. One or more open blocks may be closed.
  7073. 2. One or more new blocks may be created as children of the
  7074. last open block.
  7075. 3. Text may be added to the last (deepest) open block remaining
  7076. on the tree.
  7077. Once a line has been incorporated into the tree in this way,
  7078. it can be discarded, so input can be read in a stream.
  7079. For each line, we follow this procedure:
  7080. 1. First we iterate through the open blocks, starting with the
  7081. root document, and descending through last children down to the last
  7082. open block. Each block imposes a condition that the line must satisfy
  7083. if the block is to remain open. For example, a block quote requires a
  7084. `>` character. A paragraph requires a non-blank line.
  7085. In this phase we may match all or just some of the open
  7086. blocks. But we cannot close unmatched blocks yet, because we may have a
  7087. [lazy continuation line].
  7088. 2. Next, after consuming the continuation markers for existing
  7089. blocks, we look for new block starts (e.g. `>` for a block quote).
  7090. If we encounter a new block start, we close any blocks unmatched
  7091. in step 1 before creating the new block as a child of the last
  7092. matched block.
  7093. 3. Finally, we look at the remainder of the line (after block
  7094. markers like `>`, list markers, and indentation have been consumed).
  7095. This is text that can be incorporated into the last open
  7096. block (a paragraph, code block, heading, or raw HTML).
  7097. Setext headings are formed when we see a line of a paragraph
  7098. that is a [setext heading underline].
  7099. Reference link definitions are detected when a paragraph is closed;
  7100. the accumulated text lines are parsed to see if they begin with
  7101. one or more reference link definitions. Any remainder becomes a
  7102. normal paragraph.
  7103. We can see how this works by considering how the tree above is
  7104. generated by four lines of Markdown:
  7105. ``` markdown
  7106. > Lorem ipsum dolor
  7107. sit amet.
  7108. > - Qui *quodsi iracundia*
  7109. > - aliquando id
  7110. ```
  7111. At the outset, our document model is just
  7112. ``` tree
  7113. -> document
  7114. ```
  7115. The first line of our text,
  7116. ``` markdown
  7117. > Lorem ipsum dolor
  7118. ```
  7119. causes a `block_quote` block to be created as a child of our
  7120. open `document` block, and a `paragraph` block as a child of
  7121. the `block_quote`. Then the text is added to the last open
  7122. block, the `paragraph`:
  7123. ``` tree
  7124. -> document
  7125. -> block_quote
  7126. -> paragraph
  7127. "Lorem ipsum dolor"
  7128. ```
  7129. The next line,
  7130. ``` markdown
  7131. sit amet.
  7132. ```
  7133. is a "lazy continuation" of the open `paragraph`, so it gets added
  7134. to the paragraph's text:
  7135. ``` tree
  7136. -> document
  7137. -> block_quote
  7138. -> paragraph
  7139. "Lorem ipsum dolor\nsit amet."
  7140. ```
  7141. The third line,
  7142. ``` markdown
  7143. > - Qui *quodsi iracundia*
  7144. ```
  7145. causes the `paragraph` block to be closed, and a new `list` block
  7146. opened as a child of the `block_quote`. A `list_item` is also
  7147. added as a child of the `list`, and a `paragraph` as a child of
  7148. the `list_item`. The text is then added to the new `paragraph`:
  7149. ``` tree
  7150. -> document
  7151. -> block_quote
  7152. paragraph
  7153. "Lorem ipsum dolor\nsit amet."
  7154. -> list (type=bullet tight=true bullet_char=-)
  7155. -> list_item
  7156. -> paragraph
  7157. "Qui *quodsi iracundia*"
  7158. ```
  7159. The fourth line,
  7160. ``` markdown
  7161. > - aliquando id
  7162. ```
  7163. causes the `list_item` (and its child the `paragraph`) to be closed,
  7164. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7165. is added as a child of the new `list_item`, to contain the text.
  7166. We thus obtain the final tree:
  7167. ``` tree
  7168. -> document
  7169. -> block_quote
  7170. paragraph
  7171. "Lorem ipsum dolor\nsit amet."
  7172. -> list (type=bullet tight=true bullet_char=-)
  7173. list_item
  7174. paragraph
  7175. "Qui *quodsi iracundia*"
  7176. -> list_item
  7177. -> paragraph
  7178. "aliquando id"
  7179. ```
  7180. ## Phase 2: inline structure
  7181. Once all of the input has been parsed, all open blocks are closed.
  7182. We then "walk the tree," visiting every node, and parse raw
  7183. string contents of paragraphs and headings as inlines. At this
  7184. point we have seen all the link reference definitions, so we can
  7185. resolve reference links as we go.
  7186. ``` tree
  7187. document
  7188. block_quote
  7189. paragraph
  7190. str "Lorem ipsum dolor"
  7191. softbreak
  7192. str "sit amet."
  7193. list (type=bullet tight=true bullet_char=-)
  7194. list_item
  7195. paragraph
  7196. str "Qui "
  7197. emph
  7198. str "quodsi iracundia"
  7199. list_item
  7200. paragraph
  7201. str "aliquando id"
  7202. ```
  7203. Notice how the [line ending] in the first paragraph has
  7204. been parsed as a `softbreak`, and the asterisks in the first list item
  7205. have become an `emph`.
  7206. ### An algorithm for parsing nested emphasis and links
  7207. By far the trickiest part of inline parsing is handling emphasis,
  7208. strong emphasis, links, and images. This is done using the following
  7209. algorithm.
  7210. When we're parsing inlines and we hit either
  7211. - a run of `*` or `_` characters, or
  7212. - a `[` or `![`
  7213. we insert a text node with these symbols as its literal content, and we
  7214. add a pointer to this text node to the [delimiter stack](@).
  7215. The [delimiter stack] is a doubly linked list. Each
  7216. element contains a pointer to a text node, plus information about
  7217. - the type of delimiter (`[`, `![`, `*`, `_`)
  7218. - the number of delimiters,
  7219. - whether the delimiter is "active" (all are active to start), and
  7220. - whether the delimiter is a potential opener, a potential closer,
  7221. or both (which depends on what sort of characters precede
  7222. and follow the delimiters).
  7223. When we hit a `]` character, we call the *look for link or image*
  7224. procedure (see below).
  7225. When we hit the end of the input, we call the *process emphasis*
  7226. procedure (see below), with `stack_bottom` = NULL.
  7227. #### *look for link or image*
  7228. Starting at the top of the delimiter stack, we look backwards
  7229. through the stack for an opening `[` or `![` delimiter.
  7230. - If we don't find one, we return a literal text node `]`.
  7231. - If we do find one, but it's not *active*, we remove the inactive
  7232. delimiter from the stack, and return a literal text node `]`.
  7233. - If we find one and it's active, then we parse ahead to see if
  7234. we have an inline link/image, reference link/image, compact reference
  7235. link/image, or shortcut reference link/image.
  7236. + If we don't, then we remove the opening delimiter from the
  7237. delimiter stack and return a literal text node `]`.
  7238. + If we do, then
  7239. * We return a link or image node whose children are the inlines
  7240. after the text node pointed to by the opening delimiter.
  7241. * We run *process emphasis* on these inlines, with the `[` opener
  7242. as `stack_bottom`.
  7243. * We remove the opening delimiter.
  7244. * If we have a link (and not an image), we also set all
  7245. `[` delimiters before the opening delimiter to *inactive*. (This
  7246. will prevent us from getting links within links.)
  7247. #### *process emphasis*
  7248. Parameter `stack_bottom` sets a lower bound to how far we
  7249. descend in the [delimiter stack]. If it is NULL, we can
  7250. go all the way to the bottom. Otherwise, we stop before
  7251. visiting `stack_bottom`.
  7252. Let `current_position` point to the element on the [delimiter stack]
  7253. just above `stack_bottom` (or the first element if `stack_bottom`
  7254. is NULL).
  7255. We keep track of the `openers_bottom` for each delimiter
  7256. type (`*`, `_`) and each length of the closing delimiter run
  7257. (modulo 3). Initialize this to `stack_bottom`.
  7258. Then we repeat the following until we run out of potential
  7259. closers:
  7260. - Move `current_position` forward in the delimiter stack (if needed)
  7261. until we find the first potential closer with delimiter `*` or `_`.
  7262. (This will be the potential closer closest
  7263. to the beginning of the input -- the first one in parse order.)
  7264. - Now, look back in the stack (staying above `stack_bottom` and
  7265. the `openers_bottom` for this delimiter type) for the
  7266. first matching potential opener ("matching" means same delimiter).
  7267. - If one is found:
  7268. + Figure out whether we have emphasis or strong emphasis:
  7269. if both closer and opener spans have length >= 2, we have
  7270. strong, otherwise regular.
  7271. + Insert an emph or strong emph node accordingly, after
  7272. the text node corresponding to the opener.
  7273. + Remove any delimiters between the opener and closer from
  7274. the delimiter stack.
  7275. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7276. from the opening and closing text nodes. If they become empty
  7277. as a result, remove them and remove the corresponding element
  7278. of the delimiter stack. If the closing node is removed, reset
  7279. `current_position` to the next element in the stack.
  7280. - If none is found:
  7281. + Set `openers_bottom` to the element before `current_position`.
  7282. (We know that there are no openers for this kind of closer up to and
  7283. including this point, so this puts a lower bound on future searches.)
  7284. + If the closer at `current_position` is not a potential opener,
  7285. remove it from the delimiter stack (since we know it can't
  7286. be a closer either).
  7287. + Advance `current_position` to the next element in the stack.
  7288. After we're done, we remove all delimiters above `stack_bottom` from the
  7289. delimiter stack.