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