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