aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 41629384dcafa6017353fa7411cbc3e362b72d42 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a github wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  252. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  253. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  254. `{`, `|`, `}`, or `~` (U+007B–007E).
  255. A [punctuation character](@) is an [ASCII
  256. punctuation character] or anything in
  257. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  258. ## Tabs
  259. Tabs in lines are not expanded to [spaces]. However,
  260. in contexts where whitespace helps to define block structure,
  261. tabs behave as if they were replaced by spaces with a tab stop
  262. of 4 characters.
  263. Thus, for example, a tab can be used instead of four spaces
  264. in an indented code block. (Note, however, that internal
  265. tabs are passed through as literal tabs, not expanded to
  266. spaces.)
  267. ```````````````````````````````` example
  268. →foo→baz→→bim
  269. .
  270. <pre><code>foo→baz→→bim
  271. </code></pre>
  272. ````````````````````````````````
  273. ```````````````````````````````` example
  274. →foo→baz→→bim
  275. .
  276. <pre><code>foo→baz→→bim
  277. </code></pre>
  278. ````````````````````````````````
  279. ```````````````````````````````` example
  280. a→a
  281. ὐ→a
  282. .
  283. <pre><code>a→a
  284. ὐ→a
  285. </code></pre>
  286. ````````````````````````````````
  287. In the following example, a continuation paragraph of a list
  288. item is indented with a tab; this has exactly the same effect
  289. as indentation with four spaces would:
  290. ```````````````````````````````` example
  291. - foo
  292. →bar
  293. .
  294. <ul>
  295. <li>
  296. <p>foo</p>
  297. <p>bar</p>
  298. </li>
  299. </ul>
  300. ````````````````````````````````
  301. ```````````````````````````````` example
  302. - foo
  303. →→bar
  304. .
  305. <ul>
  306. <li>
  307. <p>foo</p>
  308. <pre><code> bar
  309. </code></pre>
  310. </li>
  311. </ul>
  312. ````````````````````````````````
  313. Normally the `>` that begins a block quote may be followed
  314. optionally by a space, which is not considered part of the
  315. content. In the following case `>` is followed by a tab,
  316. which is treated as if it were expanded into three spaces.
  317. Since one of these spaces is considered part of the
  318. delimiter, `foo` is considered to be indented six spaces
  319. inside the block quote context, so we get an indented
  320. code block starting with two spaces.
  321. ```````````````````````````````` example
  322. >→→foo
  323. .
  324. <blockquote>
  325. <pre><code> foo
  326. </code></pre>
  327. </blockquote>
  328. ````````````````````````````````
  329. ```````````````````````````````` example
  330. -→→foo
  331. .
  332. <ul>
  333. <li>
  334. <pre><code> foo
  335. </code></pre>
  336. </li>
  337. </ul>
  338. ````````````````````````````````
  339. ```````````````````````````````` example
  340. foo
  341. →bar
  342. .
  343. <pre><code>foo
  344. bar
  345. </code></pre>
  346. ````````````````````````````````
  347. ```````````````````````````````` example
  348. - foo
  349. - bar
  350. → - baz
  351. .
  352. <ul>
  353. <li>foo
  354. <ul>
  355. <li>bar
  356. <ul>
  357. <li>baz</li>
  358. </ul>
  359. </li>
  360. </ul>
  361. </li>
  362. </ul>
  363. ````````````````````````````````
  364. ```````````````````````````````` example
  365. #→Foo
  366. .
  367. <h1>Foo</h1>
  368. ````````````````````````````````
  369. ```````````````````````````````` example
  370. *→*→*→
  371. .
  372. <hr />
  373. ````````````````````````````````
  374. ## Insecure characters
  375. For security reasons, the Unicode character `U+0000` must be replaced
  376. with the REPLACEMENT CHARACTER (`U+FFFD`).
  377. # Blocks and inlines
  378. We can think of a document as a sequence of
  379. [blocks](@)---structural elements like paragraphs, block
  380. quotations, lists, headings, rules, and code blocks. Some blocks (like
  381. block quotes and list items) contain other blocks; others (like
  382. headings and paragraphs) contain [inline](@) content---text,
  383. links, emphasized text, images, code spans, and so on.
  384. ## Precedence
  385. Indicators of block structure always take precedence over indicators
  386. of inline structure. So, for example, the following is a list with
  387. two items, not a list with one item containing a code span:
  388. ```````````````````````````````` example
  389. - `one
  390. - two`
  391. .
  392. <ul>
  393. <li>`one</li>
  394. <li>two`</li>
  395. </ul>
  396. ````````````````````````````````
  397. This means that parsing can proceed in two steps: first, the block
  398. structure of the document can be discerned; second, text lines inside
  399. paragraphs, headings, and other block constructs can be parsed for inline
  400. structure. The second step requires information about link reference
  401. definitions that will be available only at the end of the first
  402. step. Note that the first step requires processing lines in sequence,
  403. but the second can be parallelized, since the inline parsing of
  404. one block element does not affect the inline parsing of any other.
  405. ## Container blocks and leaf blocks
  406. We can divide blocks into two types:
  407. [container blocks](@),
  408. which can contain other blocks, and [leaf blocks](@),
  409. which cannot.
  410. # Leaf blocks
  411. This section describes the different kinds of leaf block that make up a
  412. Markdown document.
  413. ## Thematic breaks
  414. A line consisting of 0-3 spaces of indentation, followed by a sequence
  415. of three or more matching `-`, `_`, or `*` characters, each followed
  416. optionally by any number of spaces or tabs, forms a
  417. [thematic break](@).
  418. ```````````````````````````````` example
  419. ***
  420. ---
  421. ___
  422. .
  423. <hr />
  424. <hr />
  425. <hr />
  426. ````````````````````````````````
  427. Wrong characters:
  428. ```````````````````````````````` example
  429. +++
  430. .
  431. <p>+++</p>
  432. ````````````````````````````````
  433. ```````````````````````````````` example
  434. ===
  435. .
  436. <p>===</p>
  437. ````````````````````````````````
  438. Not enough characters:
  439. ```````````````````````````````` example
  440. --
  441. **
  442. __
  443. .
  444. <p>--
  445. **
  446. __</p>
  447. ````````````````````````````````
  448. One to three spaces indent are allowed:
  449. ```````````````````````````````` example
  450. ***
  451. ***
  452. ***
  453. .
  454. <hr />
  455. <hr />
  456. <hr />
  457. ````````````````````````````````
  458. Four spaces is too many:
  459. ```````````````````````````````` example
  460. ***
  461. .
  462. <pre><code>***
  463. </code></pre>
  464. ````````````````````````````````
  465. ```````````````````````````````` example
  466. Foo
  467. ***
  468. .
  469. <p>Foo
  470. ***</p>
  471. ````````````````````````````````
  472. More than three characters may be used:
  473. ```````````````````````````````` example
  474. _____________________________________
  475. .
  476. <hr />
  477. ````````````````````````````````
  478. Spaces are allowed between the characters:
  479. ```````````````````````````````` example
  480. - - -
  481. .
  482. <hr />
  483. ````````````````````````````````
  484. ```````````````````````````````` example
  485. ** * ** * ** * **
  486. .
  487. <hr />
  488. ````````````````````````````````
  489. ```````````````````````````````` example
  490. - - - -
  491. .
  492. <hr />
  493. ````````````````````````````````
  494. Spaces are allowed at the end:
  495. ```````````````````````````````` example
  496. - - - -
  497. .
  498. <hr />
  499. ````````````````````````````````
  500. However, no other characters may occur in the line:
  501. ```````````````````````````````` example
  502. _ _ _ _ a
  503. a------
  504. ---a---
  505. .
  506. <p>_ _ _ _ a</p>
  507. <p>a------</p>
  508. <p>---a---</p>
  509. ````````````````````````````````
  510. It is required that all of the [non-whitespace characters] be the same.
  511. So, this is not a thematic break:
  512. ```````````````````````````````` example
  513. *-*
  514. .
  515. <p><em>-</em></p>
  516. ````````````````````````````````
  517. Thematic breaks do not need blank lines before or after:
  518. ```````````````````````````````` example
  519. - foo
  520. ***
  521. - bar
  522. .
  523. <ul>
  524. <li>foo</li>
  525. </ul>
  526. <hr />
  527. <ul>
  528. <li>bar</li>
  529. </ul>
  530. ````````````````````````````````
  531. Thematic breaks can interrupt a paragraph:
  532. ```````````````````````````````` example
  533. Foo
  534. ***
  535. bar
  536. .
  537. <p>Foo</p>
  538. <hr />
  539. <p>bar</p>
  540. ````````````````````````````````
  541. If a line of dashes that meets the above conditions for being a
  542. thematic break could also be interpreted as the underline of a [setext
  543. heading], the interpretation as a
  544. [setext heading] takes precedence. Thus, for example,
  545. this is a setext heading, not a paragraph followed by a thematic break:
  546. ```````````````````````````````` example
  547. Foo
  548. ---
  549. bar
  550. .
  551. <h2>Foo</h2>
  552. <p>bar</p>
  553. ````````````````````````````````
  554. When both a thematic break and a list item are possible
  555. interpretations of a line, the thematic break takes precedence:
  556. ```````````````````````````````` example
  557. * Foo
  558. * * *
  559. * Bar
  560. .
  561. <ul>
  562. <li>Foo</li>
  563. </ul>
  564. <hr />
  565. <ul>
  566. <li>Bar</li>
  567. </ul>
  568. ````````````````````````````````
  569. If you want a thematic break in a list item, use a different bullet:
  570. ```````````````````````````````` example
  571. - Foo
  572. - * * *
  573. .
  574. <ul>
  575. <li>Foo</li>
  576. <li>
  577. <hr />
  578. </li>
  579. </ul>
  580. ````````````````````````````````
  581. ## ATX headings
  582. An [ATX heading](@)
  583. consists of a string of characters, parsed as inline content, between an
  584. opening sequence of 1--6 unescaped `#` characters and an optional
  585. closing sequence of any number of unescaped `#` characters.
  586. The opening sequence of `#` characters must be followed by a
  587. [space] or by the end of line. The optional closing sequence of `#`s must be
  588. preceded by a [space] and may be followed by spaces only. The opening
  589. `#` character may be indented 0-3 spaces. The raw contents of the
  590. heading are stripped of leading and trailing spaces before being parsed
  591. as inline content. The heading level is equal to the number of `#`
  592. characters in the opening sequence.
  593. Simple headings:
  594. ```````````````````````````````` example
  595. # foo
  596. ## foo
  597. ### foo
  598. #### foo
  599. ##### foo
  600. ###### foo
  601. .
  602. <h1>foo</h1>
  603. <h2>foo</h2>
  604. <h3>foo</h3>
  605. <h4>foo</h4>
  606. <h5>foo</h5>
  607. <h6>foo</h6>
  608. ````````````````````````````````
  609. More than six `#` characters is not a heading:
  610. ```````````````````````````````` example
  611. ####### foo
  612. .
  613. <p>####### foo</p>
  614. ````````````````````````````````
  615. At least one space is required between the `#` characters and the
  616. heading's contents, unless the heading is empty. Note that many
  617. implementations currently do not require the space. However, the
  618. space was required by the
  619. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  620. and it helps prevent things like the following from being parsed as
  621. headings:
  622. ```````````````````````````````` example
  623. #5 bolt
  624. #hashtag
  625. .
  626. <p>#5 bolt</p>
  627. <p>#hashtag</p>
  628. ````````````````````````````````
  629. This is not a heading, because the first `#` is escaped:
  630. ```````````````````````````````` example
  631. \## foo
  632. .
  633. <p>## foo</p>
  634. ````````````````````````````````
  635. Contents are parsed as inlines:
  636. ```````````````````````````````` example
  637. # foo *bar* \*baz\*
  638. .
  639. <h1>foo <em>bar</em> *baz*</h1>
  640. ````````````````````````````````
  641. Leading and trailing blanks are ignored in parsing inline content:
  642. ```````````````````````````````` example
  643. # foo
  644. .
  645. <h1>foo</h1>
  646. ````````````````````````````````
  647. One to three spaces indentation are allowed:
  648. ```````````````````````````````` example
  649. ### foo
  650. ## foo
  651. # foo
  652. .
  653. <h3>foo</h3>
  654. <h2>foo</h2>
  655. <h1>foo</h1>
  656. ````````````````````````````````
  657. Four spaces are too much:
  658. ```````````````````````````````` example
  659. # foo
  660. .
  661. <pre><code># foo
  662. </code></pre>
  663. ````````````````````````````````
  664. ```````````````````````````````` example
  665. foo
  666. # bar
  667. .
  668. <p>foo
  669. # bar</p>
  670. ````````````````````````````````
  671. A closing sequence of `#` characters is optional:
  672. ```````````````````````````````` example
  673. ## foo ##
  674. ### bar ###
  675. .
  676. <h2>foo</h2>
  677. <h3>bar</h3>
  678. ````````````````````````````````
  679. It need not be the same length as the opening sequence:
  680. ```````````````````````````````` example
  681. # foo ##################################
  682. ##### foo ##
  683. .
  684. <h1>foo</h1>
  685. <h5>foo</h5>
  686. ````````````````````````````````
  687. Spaces are allowed after the closing sequence:
  688. ```````````````````````````````` example
  689. ### foo ###
  690. .
  691. <h3>foo</h3>
  692. ````````````````````````````````
  693. A sequence of `#` characters with anything but [spaces] following it
  694. is not a closing sequence, but counts as part of the contents of the
  695. heading:
  696. ```````````````````````````````` example
  697. ### foo ### b
  698. .
  699. <h3>foo ### b</h3>
  700. ````````````````````````````````
  701. The closing sequence must be preceded by a space:
  702. ```````````````````````````````` example
  703. # foo#
  704. .
  705. <h1>foo#</h1>
  706. ````````````````````````````````
  707. Backslash-escaped `#` characters do not count as part
  708. of the closing sequence:
  709. ```````````````````````````````` example
  710. ### foo \###
  711. ## foo #\##
  712. # foo \#
  713. .
  714. <h3>foo ###</h3>
  715. <h2>foo ###</h2>
  716. <h1>foo #</h1>
  717. ````````````````````````````````
  718. ATX headings need not be separated from surrounding content by blank
  719. lines, and they can interrupt paragraphs:
  720. ```````````````````````````````` example
  721. ****
  722. ## foo
  723. ****
  724. .
  725. <hr />
  726. <h2>foo</h2>
  727. <hr />
  728. ````````````````````````````````
  729. ```````````````````````````````` example
  730. Foo bar
  731. # baz
  732. Bar foo
  733. .
  734. <p>Foo bar</p>
  735. <h1>baz</h1>
  736. <p>Bar foo</p>
  737. ````````````````````````````````
  738. ATX headings can be empty:
  739. ```````````````````````````````` example
  740. ##
  741. #
  742. ### ###
  743. .
  744. <h2></h2>
  745. <h1></h1>
  746. <h3></h3>
  747. ````````````````````````````````
  748. ## Setext headings
  749. A [setext heading](@) consists of one or more
  750. lines of text, each containing at least one [non-whitespace
  751. character], with no more than 3 spaces indentation, followed by
  752. a [setext heading underline]. The lines of text must be such
  753. that, were they not followed by the setext heading underline,
  754. they would be interpreted as a paragraph: they cannot be
  755. interpretable as a [code fence], [ATX heading][ATX headings],
  756. [block quote][block quotes], [thematic break][thematic breaks],
  757. [list item][list items], or [HTML block][HTML blocks].
  758. A [setext heading underline](@) is a sequence of
  759. `=` characters or a sequence of `-` characters, with no more than 3
  760. spaces indentation and any number of trailing spaces. If a line
  761. containing a single `-` can be interpreted as an
  762. empty [list items], it should be interpreted this way
  763. and not as a [setext heading underline].
  764. The heading is a level 1 heading if `=` characters are used in
  765. the [setext heading underline], and a level 2 heading if `-`
  766. characters are used. The contents of the heading are the result
  767. of parsing the preceding lines of text as CommonMark inline
  768. content.
  769. In general, a setext heading need not be preceded or followed by a
  770. blank line. However, it cannot interrupt a paragraph, so when a
  771. setext heading comes after a paragraph, a blank line is needed between
  772. them.
  773. Simple examples:
  774. ```````````````````````````````` example
  775. Foo *bar*
  776. =========
  777. Foo *bar*
  778. ---------
  779. .
  780. <h1>Foo <em>bar</em></h1>
  781. <h2>Foo <em>bar</em></h2>
  782. ````````````````````````````````
  783. The content of the header may span more than one line:
  784. ```````````````````````````````` example
  785. Foo *bar
  786. baz*
  787. ====
  788. .
  789. <h1>Foo <em>bar
  790. baz</em></h1>
  791. ````````````````````````````````
  792. The underlining can be any length:
  793. ```````````````````````````````` example
  794. Foo
  795. -------------------------
  796. Foo
  797. =
  798. .
  799. <h2>Foo</h2>
  800. <h1>Foo</h1>
  801. ````````````````````````````````
  802. The heading content can be indented up to three spaces, and need
  803. not line up with the underlining:
  804. ```````````````````````````````` example
  805. Foo
  806. ---
  807. Foo
  808. -----
  809. Foo
  810. ===
  811. .
  812. <h2>Foo</h2>
  813. <h2>Foo</h2>
  814. <h1>Foo</h1>
  815. ````````````````````````````````
  816. Four spaces indent is too much:
  817. ```````````````````````````````` example
  818. Foo
  819. ---
  820. Foo
  821. ---
  822. .
  823. <pre><code>Foo
  824. ---
  825. Foo
  826. </code></pre>
  827. <hr />
  828. ````````````````````````````````
  829. The setext heading underline can be indented up to three spaces, and
  830. may have trailing spaces:
  831. ```````````````````````````````` example
  832. Foo
  833. ----
  834. .
  835. <h2>Foo</h2>
  836. ````````````````````````````````
  837. Four spaces is too much:
  838. ```````````````````````````````` example
  839. Foo
  840. ---
  841. .
  842. <p>Foo
  843. ---</p>
  844. ````````````````````````````````
  845. The setext heading underline cannot contain internal spaces:
  846. ```````````````````````````````` example
  847. Foo
  848. = =
  849. Foo
  850. --- -
  851. .
  852. <p>Foo
  853. = =</p>
  854. <p>Foo</p>
  855. <hr />
  856. ````````````````````````````````
  857. Trailing spaces in the content line do not cause a line break:
  858. ```````````````````````````````` example
  859. Foo
  860. -----
  861. .
  862. <h2>Foo</h2>
  863. ````````````````````````````````
  864. Nor does a backslash at the end:
  865. ```````````````````````````````` example
  866. Foo\
  867. ----
  868. .
  869. <h2>Foo\</h2>
  870. ````````````````````````````````
  871. Since indicators of block structure take precedence over
  872. indicators of inline structure, the following are setext headings:
  873. ```````````````````````````````` example
  874. `Foo
  875. ----
  876. `
  877. <a title="a lot
  878. ---
  879. of dashes"/>
  880. .
  881. <h2>`Foo</h2>
  882. <p>`</p>
  883. <h2>&lt;a title=&quot;a lot</h2>
  884. <p>of dashes&quot;/&gt;</p>
  885. ````````````````````````````````
  886. The setext heading underline cannot be a [lazy continuation
  887. line] in a list item or block quote:
  888. ```````````````````````````````` example
  889. > Foo
  890. ---
  891. .
  892. <blockquote>
  893. <p>Foo</p>
  894. </blockquote>
  895. <hr />
  896. ````````````````````````````````
  897. ```````````````````````````````` example
  898. > foo
  899. bar
  900. ===
  901. .
  902. <blockquote>
  903. <p>foo
  904. bar
  905. ===</p>
  906. </blockquote>
  907. ````````````````````````````````
  908. ```````````````````````````````` example
  909. - Foo
  910. ---
  911. .
  912. <ul>
  913. <li>Foo</li>
  914. </ul>
  915. <hr />
  916. ````````````````````````````````
  917. A blank line is needed between a paragraph and a following
  918. setext heading, since otherwise the paragraph becomes part
  919. of the heading's content:
  920. ```````````````````````````````` example
  921. Foo
  922. Bar
  923. ---
  924. .
  925. <h2>Foo
  926. Bar</h2>
  927. ````````````````````````````````
  928. But in general a blank line is not required before or after
  929. setext headings:
  930. ```````````````````````````````` example
  931. ---
  932. Foo
  933. ---
  934. Bar
  935. ---
  936. Baz
  937. .
  938. <hr />
  939. <h2>Foo</h2>
  940. <h2>Bar</h2>
  941. <p>Baz</p>
  942. ````````````````````````````````
  943. Setext headings cannot be empty:
  944. ```````````````````````````````` example
  945. ====
  946. .
  947. <p>====</p>
  948. ````````````````````````````````
  949. Setext heading text lines must not be interpretable as block
  950. constructs other than paragraphs. So, the line of dashes
  951. in these examples gets interpreted as a thematic break:
  952. ```````````````````````````````` example
  953. ---
  954. ---
  955. .
  956. <hr />
  957. <hr />
  958. ````````````````````````````````
  959. ```````````````````````````````` example
  960. - foo
  961. -----
  962. .
  963. <ul>
  964. <li>foo</li>
  965. </ul>
  966. <hr />
  967. ````````````````````````````````
  968. ```````````````````````````````` example
  969. foo
  970. ---
  971. .
  972. <pre><code>foo
  973. </code></pre>
  974. <hr />
  975. ````````````````````````````````
  976. ```````````````````````````````` example
  977. > foo
  978. -----
  979. .
  980. <blockquote>
  981. <p>foo</p>
  982. </blockquote>
  983. <hr />
  984. ````````````````````````````````
  985. If you want a heading with `> foo` as its literal text, you can
  986. use backslash escapes:
  987. ```````````````````````````````` example
  988. \> foo
  989. ------
  990. .
  991. <h2>&gt; foo</h2>
  992. ````````````````````````````````
  993. **Compatibility note:** Most existing Markdown implementations
  994. do not allow the text of setext headings to span multiple lines.
  995. But there is no consensus about how to interpret
  996. ``` markdown
  997. Foo
  998. bar
  999. ---
  1000. baz
  1001. ```
  1002. One can find four different interpretations:
  1003. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1004. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1005. 3. paragraph "Foo bar --- baz"
  1006. 4. heading "Foo bar", paragraph "baz"
  1007. We find interpretation 4 most natural, and interpretation 4
  1008. increases the expressive power of CommonMark, by allowing
  1009. multiline headings. Authors who want interpretation 1 can
  1010. put a blank line after the first paragraph:
  1011. ```````````````````````````````` example
  1012. Foo
  1013. bar
  1014. ---
  1015. baz
  1016. .
  1017. <p>Foo</p>
  1018. <h2>bar</h2>
  1019. <p>baz</p>
  1020. ````````````````````````````````
  1021. Authors who want interpretation 2 can put blank lines around
  1022. the thematic break,
  1023. ```````````````````````````````` example
  1024. Foo
  1025. bar
  1026. ---
  1027. baz
  1028. .
  1029. <p>Foo
  1030. bar</p>
  1031. <hr />
  1032. <p>baz</p>
  1033. ````````````````````````````````
  1034. or use a thematic break that cannot count as a [setext heading
  1035. underline], such as
  1036. ```````````````````````````````` example
  1037. Foo
  1038. bar
  1039. * * *
  1040. baz
  1041. .
  1042. <p>Foo
  1043. bar</p>
  1044. <hr />
  1045. <p>baz</p>
  1046. ````````````````````````````````
  1047. Authors who want interpretation 3 can use backslash escapes:
  1048. ```````````````````````````````` example
  1049. Foo
  1050. bar
  1051. \---
  1052. baz
  1053. .
  1054. <p>Foo
  1055. bar
  1056. ---
  1057. baz</p>
  1058. ````````````````````````````````
  1059. ## Indented code blocks
  1060. An [indented code block](@) is composed of one or more
  1061. [indented chunks] separated by blank lines.
  1062. An [indented chunk](@) is a sequence of non-blank lines,
  1063. each indented four or more spaces. The contents of the code block are
  1064. the literal contents of the lines, including trailing
  1065. [line endings], minus four spaces of indentation.
  1066. An indented code block has no [info string].
  1067. An indented code block cannot interrupt a paragraph, so there must be
  1068. a blank line between a paragraph and a following indented code block.
  1069. (A blank line is not needed, however, between a code block and a following
  1070. paragraph.)
  1071. ```````````````````````````````` example
  1072. a simple
  1073. indented code block
  1074. .
  1075. <pre><code>a simple
  1076. indented code block
  1077. </code></pre>
  1078. ````````````````````````````````
  1079. If there is any ambiguity between an interpretation of indentation
  1080. as a code block and as indicating that material belongs to a [list
  1081. item][list items], the list item interpretation takes precedence:
  1082. ```````````````````````````````` example
  1083. - foo
  1084. bar
  1085. .
  1086. <ul>
  1087. <li>
  1088. <p>foo</p>
  1089. <p>bar</p>
  1090. </li>
  1091. </ul>
  1092. ````````````````````````````````
  1093. ```````````````````````````````` example
  1094. 1. foo
  1095. - bar
  1096. .
  1097. <ol>
  1098. <li>
  1099. <p>foo</p>
  1100. <ul>
  1101. <li>bar</li>
  1102. </ul>
  1103. </li>
  1104. </ol>
  1105. ````````````````````````````````
  1106. The contents of a code block are literal text, and do not get parsed
  1107. as Markdown:
  1108. ```````````````````````````````` example
  1109. <a/>
  1110. *hi*
  1111. - one
  1112. .
  1113. <pre><code>&lt;a/&gt;
  1114. *hi*
  1115. - one
  1116. </code></pre>
  1117. ````````````````````````````````
  1118. Here we have three chunks separated by blank lines:
  1119. ```````````````````````````````` example
  1120. chunk1
  1121. chunk2
  1122. chunk3
  1123. .
  1124. <pre><code>chunk1
  1125. chunk2
  1126. chunk3
  1127. </code></pre>
  1128. ````````````````````````````````
  1129. Any initial spaces beyond four will be included in the content, even
  1130. in interior blank lines:
  1131. ```````````````````````````````` example
  1132. chunk1
  1133. chunk2
  1134. .
  1135. <pre><code>chunk1
  1136. chunk2
  1137. </code></pre>
  1138. ````````````````````````````````
  1139. An indented code block cannot interrupt a paragraph. (This
  1140. allows hanging indents and the like.)
  1141. ```````````````````````````````` example
  1142. Foo
  1143. bar
  1144. .
  1145. <p>Foo
  1146. bar</p>
  1147. ````````````````````````````````
  1148. However, any non-blank line with fewer than four leading spaces ends
  1149. the code block immediately. So a paragraph may occur immediately
  1150. after indented code:
  1151. ```````````````````````````````` example
  1152. foo
  1153. bar
  1154. .
  1155. <pre><code>foo
  1156. </code></pre>
  1157. <p>bar</p>
  1158. ````````````````````````````````
  1159. And indented code can occur immediately before and after other kinds of
  1160. blocks:
  1161. ```````````````````````````````` example
  1162. # Heading
  1163. foo
  1164. Heading
  1165. ------
  1166. foo
  1167. ----
  1168. .
  1169. <h1>Heading</h1>
  1170. <pre><code>foo
  1171. </code></pre>
  1172. <h2>Heading</h2>
  1173. <pre><code>foo
  1174. </code></pre>
  1175. <hr />
  1176. ````````````````````````````````
  1177. The first line can be indented more than four spaces:
  1178. ```````````````````````````````` example
  1179. foo
  1180. bar
  1181. .
  1182. <pre><code> foo
  1183. bar
  1184. </code></pre>
  1185. ````````````````````````````````
  1186. Blank lines preceding or following an indented code block
  1187. are not included in it:
  1188. ```````````````````````````````` example
  1189. foo
  1190. .
  1191. <pre><code>foo
  1192. </code></pre>
  1193. ````````````````````````````````
  1194. Trailing spaces are included in the code block's content:
  1195. ```````````````````````````````` example
  1196. foo
  1197. .
  1198. <pre><code>foo
  1199. </code></pre>
  1200. ````````````````````````````````
  1201. ## Fenced code blocks
  1202. A [code fence](@) is a sequence
  1203. of at least three consecutive backtick characters (`` ` ``) or
  1204. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1205. A [fenced code block](@)
  1206. begins with a code fence, indented no more than three spaces.
  1207. The line with the opening code fence may optionally contain some text
  1208. following the code fence; this is trimmed of leading and trailing
  1209. whitespace and called the [info string](@). If the [info string] comes
  1210. after a backtick fence, it may not contain any backtick
  1211. characters. (The reason for this restriction is that otherwise
  1212. some inline code would be incorrectly interpreted as the
  1213. beginning of a fenced code block.)
  1214. The content of the code block consists of all subsequent lines, until
  1215. a closing [code fence] of the same type as the code block
  1216. began with (backticks or tildes), and with at least as many backticks
  1217. or tildes as the opening code fence. If the leading code fence is
  1218. indented N spaces, then up to N spaces of indentation are removed from
  1219. each line of the content (if present). (If a content line is not
  1220. indented, it is preserved unchanged. If it is indented less than N
  1221. spaces, all of the indentation is removed.)
  1222. The closing code fence may be indented up to three spaces, and may be
  1223. followed only by spaces, which are ignored. If the end of the
  1224. containing block (or document) is reached and no closing code fence
  1225. has been found, the code block contains all of the lines after the
  1226. opening code fence until the end of the containing block (or
  1227. document). (An alternative spec would require backtracking in the
  1228. event that a closing code fence is not found. But this makes parsing
  1229. much less efficient, and there seems to be no real down side to the
  1230. behavior described here.)
  1231. A fenced code block may interrupt a paragraph, and does not require
  1232. a blank line either before or after.
  1233. The content of a code fence is treated as literal text, not parsed
  1234. as inlines. The first word of the [info string] is typically used to
  1235. specify the language of the code sample, and rendered in the `class`
  1236. attribute of the `code` tag. However, this spec does not mandate any
  1237. particular treatment of the [info string].
  1238. Here is a simple example with backticks:
  1239. ```````````````````````````````` example
  1240. ```
  1241. <
  1242. >
  1243. ```
  1244. .
  1245. <pre><code>&lt;
  1246. &gt;
  1247. </code></pre>
  1248. ````````````````````````````````
  1249. With tildes:
  1250. ```````````````````````````````` example
  1251. ~~~
  1252. <
  1253. >
  1254. ~~~
  1255. .
  1256. <pre><code>&lt;
  1257. &gt;
  1258. </code></pre>
  1259. ````````````````````````````````
  1260. Fewer than three backticks is not enough:
  1261. ```````````````````````````````` example
  1262. ``
  1263. foo
  1264. ``
  1265. .
  1266. <p><code>foo</code></p>
  1267. ````````````````````````````````
  1268. The closing code fence must use the same character as the opening
  1269. fence:
  1270. ```````````````````````````````` example
  1271. ```
  1272. aaa
  1273. ~~~
  1274. ```
  1275. .
  1276. <pre><code>aaa
  1277. ~~~
  1278. </code></pre>
  1279. ````````````````````````````````
  1280. ```````````````````````````````` example
  1281. ~~~
  1282. aaa
  1283. ```
  1284. ~~~
  1285. .
  1286. <pre><code>aaa
  1287. ```
  1288. </code></pre>
  1289. ````````````````````````````````
  1290. The closing code fence must be at least as long as the opening fence:
  1291. ```````````````````````````````` example
  1292. ````
  1293. aaa
  1294. ```
  1295. ``````
  1296. .
  1297. <pre><code>aaa
  1298. ```
  1299. </code></pre>
  1300. ````````````````````````````````
  1301. ```````````````````````````````` example
  1302. ~~~~
  1303. aaa
  1304. ~~~
  1305. ~~~~
  1306. .
  1307. <pre><code>aaa
  1308. ~~~
  1309. </code></pre>
  1310. ````````````````````````````````
  1311. Unclosed code blocks are closed by the end of the document
  1312. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1313. ```````````````````````````````` example
  1314. ```
  1315. .
  1316. <pre><code></code></pre>
  1317. ````````````````````````````````
  1318. ```````````````````````````````` example
  1319. `````
  1320. ```
  1321. aaa
  1322. .
  1323. <pre><code>
  1324. ```
  1325. aaa
  1326. </code></pre>
  1327. ````````````````````````````````
  1328. ```````````````````````````````` example
  1329. > ```
  1330. > aaa
  1331. bbb
  1332. .
  1333. <blockquote>
  1334. <pre><code>aaa
  1335. </code></pre>
  1336. </blockquote>
  1337. <p>bbb</p>
  1338. ````````````````````````````````
  1339. A code block can have all empty lines as its content:
  1340. ```````````````````````````````` example
  1341. ```
  1342. ```
  1343. .
  1344. <pre><code>
  1345. </code></pre>
  1346. ````````````````````````````````
  1347. A code block can be empty:
  1348. ```````````````````````````````` example
  1349. ```
  1350. ```
  1351. .
  1352. <pre><code></code></pre>
  1353. ````````````````````````````````
  1354. Fences can be indented. If the opening fence is indented,
  1355. content lines will have equivalent opening indentation removed,
  1356. if present:
  1357. ```````````````````````````````` example
  1358. ```
  1359. aaa
  1360. aaa
  1361. ```
  1362. .
  1363. <pre><code>aaa
  1364. aaa
  1365. </code></pre>
  1366. ````````````````````````````````
  1367. ```````````````````````````````` example
  1368. ```
  1369. aaa
  1370. aaa
  1371. aaa
  1372. ```
  1373. .
  1374. <pre><code>aaa
  1375. aaa
  1376. aaa
  1377. </code></pre>
  1378. ````````````````````````````````
  1379. ```````````````````````````````` example
  1380. ```
  1381. aaa
  1382. aaa
  1383. aaa
  1384. ```
  1385. .
  1386. <pre><code>aaa
  1387. aaa
  1388. aaa
  1389. </code></pre>
  1390. ````````````````````````````````
  1391. Four spaces indentation produces an indented code block:
  1392. ```````````````````````````````` example
  1393. ```
  1394. aaa
  1395. ```
  1396. .
  1397. <pre><code>```
  1398. aaa
  1399. ```
  1400. </code></pre>
  1401. ````````````````````````````````
  1402. Closing fences may be indented by 0-3 spaces, and their indentation
  1403. need not match that of the opening fence:
  1404. ```````````````````````````````` example
  1405. ```
  1406. aaa
  1407. ```
  1408. .
  1409. <pre><code>aaa
  1410. </code></pre>
  1411. ````````````````````````````````
  1412. ```````````````````````````````` example
  1413. ```
  1414. aaa
  1415. ```
  1416. .
  1417. <pre><code>aaa
  1418. </code></pre>
  1419. ````````````````````````````````
  1420. This is not a closing fence, because it is indented 4 spaces:
  1421. ```````````````````````````````` example
  1422. ```
  1423. aaa
  1424. ```
  1425. .
  1426. <pre><code>aaa
  1427. ```
  1428. </code></pre>
  1429. ````````````````````````````````
  1430. Code fences (opening and closing) cannot contain internal spaces:
  1431. ```````````````````````````````` example
  1432. ``` ```
  1433. aaa
  1434. .
  1435. <p><code></code>
  1436. aaa</p>
  1437. ````````````````````````````````
  1438. ```````````````````````````````` example
  1439. ~~~~~~
  1440. aaa
  1441. ~~~ ~~
  1442. .
  1443. <pre><code>aaa
  1444. ~~~ ~~
  1445. </code></pre>
  1446. ````````````````````````````````
  1447. Fenced code blocks can interrupt paragraphs, and can be followed
  1448. directly by paragraphs, without a blank line between:
  1449. ```````````````````````````````` example
  1450. foo
  1451. ```
  1452. bar
  1453. ```
  1454. baz
  1455. .
  1456. <p>foo</p>
  1457. <pre><code>bar
  1458. </code></pre>
  1459. <p>baz</p>
  1460. ````````````````````````````````
  1461. Other blocks can also occur before and after fenced code blocks
  1462. without an intervening blank line:
  1463. ```````````````````````````````` example
  1464. foo
  1465. ---
  1466. ~~~
  1467. bar
  1468. ~~~
  1469. # baz
  1470. .
  1471. <h2>foo</h2>
  1472. <pre><code>bar
  1473. </code></pre>
  1474. <h1>baz</h1>
  1475. ````````````````````````````````
  1476. An [info string] can be provided after the opening code fence.
  1477. Although this spec doesn't mandate any particular treatment of
  1478. the info string, the first word is typically used to specify
  1479. the language of the code block. In HTML output, the language is
  1480. normally indicated by adding a class to the `code` element consisting
  1481. of `language-` followed by the language name.
  1482. ```````````````````````````````` example
  1483. ```ruby
  1484. def foo(x)
  1485. return 3
  1486. end
  1487. ```
  1488. .
  1489. <pre><code class="language-ruby">def foo(x)
  1490. return 3
  1491. end
  1492. </code></pre>
  1493. ````````````````````````````````
  1494. ```````````````````````````````` example
  1495. ~~~~ ruby startline=3 $%@#$
  1496. def foo(x)
  1497. return 3
  1498. end
  1499. ~~~~~~~
  1500. .
  1501. <pre><code class="language-ruby">def foo(x)
  1502. return 3
  1503. end
  1504. </code></pre>
  1505. ````````````````````````````````
  1506. ```````````````````````````````` example
  1507. ````;
  1508. ````
  1509. .
  1510. <pre><code class="language-;"></code></pre>
  1511. ````````````````````````````````
  1512. [Info strings] for backtick code blocks cannot contain backticks:
  1513. ```````````````````````````````` example
  1514. ``` aa ```
  1515. foo
  1516. .
  1517. <p><code>aa</code>
  1518. foo</p>
  1519. ````````````````````````````````
  1520. [Info strings] for tilde code blocks can contain backticks and tildes:
  1521. ```````````````````````````````` example
  1522. ~~~ aa ``` ~~~
  1523. foo
  1524. ~~~
  1525. .
  1526. <pre><code class="language-aa">foo
  1527. </code></pre>
  1528. ````````````````````````````````
  1529. Closing code fences cannot have [info strings]:
  1530. ```````````````````````````````` example
  1531. ```
  1532. ``` aaa
  1533. ```
  1534. .
  1535. <pre><code>``` aaa
  1536. </code></pre>
  1537. ````````````````````````````````
  1538. ## HTML blocks
  1539. An [HTML block](@) is a group of lines that is treated
  1540. as raw HTML (and will not be escaped in HTML output).
  1541. There are seven kinds of [HTML block], which can be defined by their
  1542. start and end conditions. The block begins with a line that meets a
  1543. [start condition](@) (after up to three spaces optional indentation).
  1544. It ends with the first subsequent line that meets a matching [end
  1545. condition](@), or the last line of the document, or the last line of
  1546. the [container block](#container-blocks) containing the current HTML
  1547. block, if no line is encountered that meets the [end condition]. If
  1548. the first line meets both the [start condition] and the [end
  1549. condition], the block will contain just that line.
  1550. 1. **Start condition:** line begins with the string `<script`,
  1551. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1552. the string `>`, or the end of the line.\
  1553. **End condition:** line contains an end tag
  1554. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1555. need not match the start tag).
  1556. 2. **Start condition:** line begins with the string `<!--`.\
  1557. **End condition:** line contains the string `-->`.
  1558. 3. **Start condition:** line begins with the string `<?`.\
  1559. **End condition:** line contains the string `?>`.
  1560. 4. **Start condition:** line begins with the string `<!`
  1561. followed by an uppercase ASCII letter.\
  1562. **End condition:** line contains the character `>`.
  1563. 5. **Start condition:** line begins with the string
  1564. `<![CDATA[`.\
  1565. **End condition:** line contains the string `]]>`.
  1566. 6. **Start condition:** line begins the string `<` or `</`
  1567. followed by one of the strings (case-insensitive) `address`,
  1568. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1569. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1570. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1571. `footer`, `form`, `frame`, `frameset`,
  1572. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1573. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1574. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1575. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1576. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1577. by [whitespace], the end of the line, the string `>`, or
  1578. the string `/>`.\
  1579. **End condition:** line is followed by a [blank line].
  1580. 7. **Start condition:** line begins with a complete [open tag]
  1581. (with any [tag name] other than `script`,
  1582. `style`, or `pre`) or a complete [closing tag],
  1583. followed only by [whitespace] or the end of the line.\
  1584. **End condition:** line is followed by a [blank line].
  1585. HTML blocks continue until they are closed by their appropriate
  1586. [end condition], or the last line of the document or other [container
  1587. block](#container-blocks). This means any HTML **within an HTML
  1588. block** that might otherwise be recognised as a start condition will
  1589. be ignored by the parser and passed through as-is, without changing
  1590. the parser's state.
  1591. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1592. the parser state; as the HTML block was started in by start condition 6, it
  1593. will end at any blank line. This can be surprising:
  1594. ```````````````````````````````` example
  1595. <table><tr><td>
  1596. <pre>
  1597. **Hello**,
  1598. _world_.
  1599. </pre>
  1600. </td></tr></table>
  1601. .
  1602. <table><tr><td>
  1603. <pre>
  1604. **Hello**,
  1605. <p><em>world</em>.
  1606. </pre></p>
  1607. </td></tr></table>
  1608. ````````````````````````````````
  1609. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1610. text remains verbatim — and regular parsing resumes, with a paragraph,
  1611. emphasised `world` and inline and block HTML following.
  1612. All types of [HTML blocks] except type 7 may interrupt
  1613. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1614. (This restriction is intended to prevent unwanted interpretation
  1615. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1616. Some simple examples follow. Here are some basic HTML blocks
  1617. of type 6:
  1618. ```````````````````````````````` example
  1619. <table>
  1620. <tr>
  1621. <td>
  1622. hi
  1623. </td>
  1624. </tr>
  1625. </table>
  1626. okay.
  1627. .
  1628. <table>
  1629. <tr>
  1630. <td>
  1631. hi
  1632. </td>
  1633. </tr>
  1634. </table>
  1635. <p>okay.</p>
  1636. ````````````````````````````````
  1637. ```````````````````````````````` example
  1638. <div>
  1639. *hello*
  1640. <foo><a>
  1641. .
  1642. <div>
  1643. *hello*
  1644. <foo><a>
  1645. ````````````````````````````````
  1646. A block can also start with a closing tag:
  1647. ```````````````````````````````` example
  1648. </div>
  1649. *foo*
  1650. .
  1651. </div>
  1652. *foo*
  1653. ````````````````````````````````
  1654. Here we have two HTML blocks with a Markdown paragraph between them:
  1655. ```````````````````````````````` example
  1656. <DIV CLASS="foo">
  1657. *Markdown*
  1658. </DIV>
  1659. .
  1660. <DIV CLASS="foo">
  1661. <p><em>Markdown</em></p>
  1662. </DIV>
  1663. ````````````````````````````````
  1664. The tag on the first line can be partial, as long
  1665. as it is split where there would be whitespace:
  1666. ```````````````````````````````` example
  1667. <div id="foo"
  1668. class="bar">
  1669. </div>
  1670. .
  1671. <div id="foo"
  1672. class="bar">
  1673. </div>
  1674. ````````````````````````````````
  1675. ```````````````````````````````` example
  1676. <div id="foo" class="bar
  1677. baz">
  1678. </div>
  1679. .
  1680. <div id="foo" class="bar
  1681. baz">
  1682. </div>
  1683. ````````````````````````````````
  1684. An open tag need not be closed:
  1685. ```````````````````````````````` example
  1686. <div>
  1687. *foo*
  1688. *bar*
  1689. .
  1690. <div>
  1691. *foo*
  1692. <p><em>bar</em></p>
  1693. ````````````````````````````````
  1694. A partial tag need not even be completed (garbage
  1695. in, garbage out):
  1696. ```````````````````````````````` example
  1697. <div id="foo"
  1698. *hi*
  1699. .
  1700. <div id="foo"
  1701. *hi*
  1702. ````````````````````````````````
  1703. ```````````````````````````````` example
  1704. <div class
  1705. foo
  1706. .
  1707. <div class
  1708. foo
  1709. ````````````````````````````````
  1710. The initial tag doesn't even need to be a valid
  1711. tag, as long as it starts like one:
  1712. ```````````````````````````````` example
  1713. <div *???-&&&-<---
  1714. *foo*
  1715. .
  1716. <div *???-&&&-<---
  1717. *foo*
  1718. ````````````````````````````````
  1719. In type 6 blocks, the initial tag need not be on a line by
  1720. itself:
  1721. ```````````````````````````````` example
  1722. <div><a href="bar">*foo*</a></div>
  1723. .
  1724. <div><a href="bar">*foo*</a></div>
  1725. ````````````````````````````````
  1726. ```````````````````````````````` example
  1727. <table><tr><td>
  1728. foo
  1729. </td></tr></table>
  1730. .
  1731. <table><tr><td>
  1732. foo
  1733. </td></tr></table>
  1734. ````````````````````````````````
  1735. Everything until the next blank line or end of document
  1736. gets included in the HTML block. So, in the following
  1737. example, what looks like a Markdown code block
  1738. is actually part of the HTML block, which continues until a blank
  1739. line or the end of the document is reached:
  1740. ```````````````````````````````` example
  1741. <div></div>
  1742. ``` c
  1743. int x = 33;
  1744. ```
  1745. .
  1746. <div></div>
  1747. ``` c
  1748. int x = 33;
  1749. ```
  1750. ````````````````````````````````
  1751. To start an [HTML block] with a tag that is *not* in the
  1752. list of block-level tags in (6), you must put the tag by
  1753. itself on the first line (and it must be complete):
  1754. ```````````````````````````````` example
  1755. <a href="foo">
  1756. *bar*
  1757. </a>
  1758. .
  1759. <a href="foo">
  1760. *bar*
  1761. </a>
  1762. ````````````````````````````````
  1763. In type 7 blocks, the [tag name] can be anything:
  1764. ```````````````````````````````` example
  1765. <Warning>
  1766. *bar*
  1767. </Warning>
  1768. .
  1769. <Warning>
  1770. *bar*
  1771. </Warning>
  1772. ````````````````````````````````
  1773. ```````````````````````````````` example
  1774. <i class="foo">
  1775. *bar*
  1776. </i>
  1777. .
  1778. <i class="foo">
  1779. *bar*
  1780. </i>
  1781. ````````````````````````````````
  1782. ```````````````````````````````` example
  1783. </ins>
  1784. *bar*
  1785. .
  1786. </ins>
  1787. *bar*
  1788. ````````````````````````````````
  1789. These rules are designed to allow us to work with tags that
  1790. can function as either block-level or inline-level tags.
  1791. The `<del>` tag is a nice example. We can surround content with
  1792. `<del>` tags in three different ways. In this case, we get a raw
  1793. HTML block, because the `<del>` tag is on a line by itself:
  1794. ```````````````````````````````` example
  1795. <del>
  1796. *foo*
  1797. </del>
  1798. .
  1799. <del>
  1800. *foo*
  1801. </del>
  1802. ````````````````````````````````
  1803. In this case, we get a raw HTML block that just includes
  1804. the `<del>` tag (because it ends with the following blank
  1805. line). So the contents get interpreted as CommonMark:
  1806. ```````````````````````````````` example
  1807. <del>
  1808. *foo*
  1809. </del>
  1810. .
  1811. <del>
  1812. <p><em>foo</em></p>
  1813. </del>
  1814. ````````````````````````````````
  1815. Finally, in this case, the `<del>` tags are interpreted
  1816. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1817. the tag is not on a line by itself, we get inline HTML
  1818. rather than an [HTML block].)
  1819. ```````````````````````````````` example
  1820. <del>*foo*</del>
  1821. .
  1822. <p><del><em>foo</em></del></p>
  1823. ````````````````````````````````
  1824. HTML tags designed to contain literal content
  1825. (`script`, `style`, `pre`), comments, processing instructions,
  1826. and declarations are treated somewhat differently.
  1827. Instead of ending at the first blank line, these blocks
  1828. end at the first line containing a corresponding end tag.
  1829. As a result, these blocks can contain blank lines:
  1830. A pre tag (type 1):
  1831. ```````````````````````````````` example
  1832. <pre language="haskell"><code>
  1833. import Text.HTML.TagSoup
  1834. main :: IO ()
  1835. main = print $ parseTags tags
  1836. </code></pre>
  1837. okay
  1838. .
  1839. <pre language="haskell"><code>
  1840. import Text.HTML.TagSoup
  1841. main :: IO ()
  1842. main = print $ parseTags tags
  1843. </code></pre>
  1844. <p>okay</p>
  1845. ````````````````````````````````
  1846. A script tag (type 1):
  1847. ```````````````````````````````` example
  1848. <script type="text/javascript">
  1849. // JavaScript example
  1850. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1851. </script>
  1852. okay
  1853. .
  1854. <script type="text/javascript">
  1855. // JavaScript example
  1856. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1857. </script>
  1858. <p>okay</p>
  1859. ````````````````````````````````
  1860. A style tag (type 1):
  1861. ```````````````````````````````` example
  1862. <style
  1863. type="text/css">
  1864. h1 {color:red;}
  1865. p {color:blue;}
  1866. </style>
  1867. okay
  1868. .
  1869. <style
  1870. type="text/css">
  1871. h1 {color:red;}
  1872. p {color:blue;}
  1873. </style>
  1874. <p>okay</p>
  1875. ````````````````````````````````
  1876. If there is no matching end tag, the block will end at the
  1877. end of the document (or the enclosing [block quote][block quotes]
  1878. or [list item][list items]):
  1879. ```````````````````````````````` example
  1880. <style
  1881. type="text/css">
  1882. foo
  1883. .
  1884. <style
  1885. type="text/css">
  1886. foo
  1887. ````````````````````````````````
  1888. ```````````````````````````````` example
  1889. > <div>
  1890. > foo
  1891. bar
  1892. .
  1893. <blockquote>
  1894. <div>
  1895. foo
  1896. </blockquote>
  1897. <p>bar</p>
  1898. ````````````````````````````````
  1899. ```````````````````````````````` example
  1900. - <div>
  1901. - foo
  1902. .
  1903. <ul>
  1904. <li>
  1905. <div>
  1906. </li>
  1907. <li>foo</li>
  1908. </ul>
  1909. ````````````````````````````````
  1910. The end tag can occur on the same line as the start tag:
  1911. ```````````````````````````````` example
  1912. <style>p{color:red;}</style>
  1913. *foo*
  1914. .
  1915. <style>p{color:red;}</style>
  1916. <p><em>foo</em></p>
  1917. ````````````````````````````````
  1918. ```````````````````````````````` example
  1919. <!-- foo -->*bar*
  1920. *baz*
  1921. .
  1922. <!-- foo -->*bar*
  1923. <p><em>baz</em></p>
  1924. ````````````````````````````````
  1925. Note that anything on the last line after the
  1926. end tag will be included in the [HTML block]:
  1927. ```````````````````````````````` example
  1928. <script>
  1929. foo
  1930. </script>1. *bar*
  1931. .
  1932. <script>
  1933. foo
  1934. </script>1. *bar*
  1935. ````````````````````````````````
  1936. A comment (type 2):
  1937. ```````````````````````````````` example
  1938. <!-- Foo
  1939. bar
  1940. baz -->
  1941. okay
  1942. .
  1943. <!-- Foo
  1944. bar
  1945. baz -->
  1946. <p>okay</p>
  1947. ````````````````````````````````
  1948. A processing instruction (type 3):
  1949. ```````````````````````````````` example
  1950. <?php
  1951. echo '>';
  1952. ?>
  1953. okay
  1954. .
  1955. <?php
  1956. echo '>';
  1957. ?>
  1958. <p>okay</p>
  1959. ````````````````````````````````
  1960. A declaration (type 4):
  1961. ```````````````````````````````` example
  1962. <!DOCTYPE html>
  1963. .
  1964. <!DOCTYPE html>
  1965. ````````````````````````````````
  1966. CDATA (type 5):
  1967. ```````````````````````````````` example
  1968. <![CDATA[
  1969. function matchwo(a,b)
  1970. {
  1971. if (a < b && a < 0) then {
  1972. return 1;
  1973. } else {
  1974. return 0;
  1975. }
  1976. }
  1977. ]]>
  1978. okay
  1979. .
  1980. <![CDATA[
  1981. function matchwo(a,b)
  1982. {
  1983. if (a < b && a < 0) then {
  1984. return 1;
  1985. } else {
  1986. return 0;
  1987. }
  1988. }
  1989. ]]>
  1990. <p>okay</p>
  1991. ````````````````````````````````
  1992. The opening tag can be indented 1-3 spaces, but not 4:
  1993. ```````````````````````````````` example
  1994. <!-- foo -->
  1995. <!-- foo -->
  1996. .
  1997. <!-- foo -->
  1998. <pre><code>&lt;!-- foo --&gt;
  1999. </code></pre>
  2000. ````````````````````````````````
  2001. ```````````````````````````````` example
  2002. <div>
  2003. <div>
  2004. .
  2005. <div>
  2006. <pre><code>&lt;div&gt;
  2007. </code></pre>
  2008. ````````````````````````````````
  2009. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2010. preceded by a blank line.
  2011. ```````````````````````````````` example
  2012. Foo
  2013. <div>
  2014. bar
  2015. </div>
  2016. .
  2017. <p>Foo</p>
  2018. <div>
  2019. bar
  2020. </div>
  2021. ````````````````````````````````
  2022. However, a following blank line is needed, except at the end of
  2023. a document, and except for blocks of types 1--5, [above][HTML
  2024. block]:
  2025. ```````````````````````````````` example
  2026. <div>
  2027. bar
  2028. </div>
  2029. *foo*
  2030. .
  2031. <div>
  2032. bar
  2033. </div>
  2034. *foo*
  2035. ````````````````````````````````
  2036. HTML blocks of type 7 cannot interrupt a paragraph:
  2037. ```````````````````````````````` example
  2038. Foo
  2039. <a href="bar">
  2040. baz
  2041. .
  2042. <p>Foo
  2043. <a href="bar">
  2044. baz</p>
  2045. ````````````````````````````````
  2046. This rule differs from John Gruber's original Markdown syntax
  2047. specification, which says:
  2048. > The only restrictions are that block-level HTML elements —
  2049. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2050. > surrounding content by blank lines, and the start and end tags of the
  2051. > block should not be indented with tabs or spaces.
  2052. In some ways Gruber's rule is more restrictive than the one given
  2053. here:
  2054. - It requires that an HTML block be preceded by a blank line.
  2055. - It does not allow the start tag to be indented.
  2056. - It requires a matching end tag, which it also does not allow to
  2057. be indented.
  2058. Most Markdown implementations (including some of Gruber's own) do not
  2059. respect all of these restrictions.
  2060. There is one respect, however, in which Gruber's rule is more liberal
  2061. than the one given here, since it allows blank lines to occur inside
  2062. an HTML block. There are two reasons for disallowing them here.
  2063. First, it removes the need to parse balanced tags, which is
  2064. expensive and can require backtracking from the end of the document
  2065. if no matching end tag is found. Second, it provides a very simple
  2066. and flexible way of including Markdown content inside HTML tags:
  2067. simply separate the Markdown from the HTML using blank lines:
  2068. Compare:
  2069. ```````````````````````````````` example
  2070. <div>
  2071. *Emphasized* text.
  2072. </div>
  2073. .
  2074. <div>
  2075. <p><em>Emphasized</em> text.</p>
  2076. </div>
  2077. ````````````````````````````````
  2078. ```````````````````````````````` example
  2079. <div>
  2080. *Emphasized* text.
  2081. </div>
  2082. .
  2083. <div>
  2084. *Emphasized* text.
  2085. </div>
  2086. ````````````````````````````````
  2087. Some Markdown implementations have adopted a convention of
  2088. interpreting content inside tags as text if the open tag has
  2089. the attribute `markdown=1`. The rule given above seems a simpler and
  2090. more elegant way of achieving the same expressive power, which is also
  2091. much simpler to parse.
  2092. The main potential drawback is that one can no longer paste HTML
  2093. blocks into Markdown documents with 100% reliability. However,
  2094. *in most cases* this will work fine, because the blank lines in
  2095. HTML are usually followed by HTML block tags. For example:
  2096. ```````````````````````````````` example
  2097. <table>
  2098. <tr>
  2099. <td>
  2100. Hi
  2101. </td>
  2102. </tr>
  2103. </table>
  2104. .
  2105. <table>
  2106. <tr>
  2107. <td>
  2108. Hi
  2109. </td>
  2110. </tr>
  2111. </table>
  2112. ````````````````````````````````
  2113. There are problems, however, if the inner tags are indented
  2114. *and* separated by spaces, as then they will be interpreted as
  2115. an indented code block:
  2116. ```````````````````````````````` example
  2117. <table>
  2118. <tr>
  2119. <td>
  2120. Hi
  2121. </td>
  2122. </tr>
  2123. </table>
  2124. .
  2125. <table>
  2126. <tr>
  2127. <pre><code>&lt;td&gt;
  2128. Hi
  2129. &lt;/td&gt;
  2130. </code></pre>
  2131. </tr>
  2132. </table>
  2133. ````````````````````````````````
  2134. Fortunately, blank lines are usually not necessary and can be
  2135. deleted. The exception is inside `<pre>` tags, but as described
  2136. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2137. *can* contain blank lines.
  2138. ## Link reference definitions
  2139. A [link reference definition](@)
  2140. consists of a [link label], indented up to three spaces, followed
  2141. by a colon (`:`), optional [whitespace] (including up to one
  2142. [line ending]), a [link destination],
  2143. optional [whitespace] (including up to one
  2144. [line ending]), and an optional [link
  2145. title], which if it is present must be separated
  2146. from the [link destination] by [whitespace].
  2147. No further [non-whitespace characters] may occur on the line.
  2148. A [link reference definition]
  2149. does not correspond to a structural element of a document. Instead, it
  2150. defines a label which can be used in [reference links]
  2151. and reference-style [images] elsewhere in the document. [Link
  2152. reference definitions] can come either before or after the links that use
  2153. them.
  2154. ```````````````````````````````` example
  2155. [foo]: /url "title"
  2156. [foo]
  2157. .
  2158. <p><a href="/url" title="title">foo</a></p>
  2159. ````````````````````````````````
  2160. ```````````````````````````````` example
  2161. [foo]:
  2162. /url
  2163. 'the title'
  2164. [foo]
  2165. .
  2166. <p><a href="/url" title="the title">foo</a></p>
  2167. ````````````````````````````````
  2168. ```````````````````````````````` example
  2169. [Foo*bar\]]:my_(url) 'title (with parens)'
  2170. [Foo*bar\]]
  2171. .
  2172. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2173. ````````````````````````````````
  2174. ```````````````````````````````` example
  2175. [Foo bar]:
  2176. <my url>
  2177. 'title'
  2178. [Foo bar]
  2179. .
  2180. <p><a href="my%20url" title="title">Foo bar</a></p>
  2181. ````````````````````````````````
  2182. The title may extend over multiple lines:
  2183. ```````````````````````````````` example
  2184. [foo]: /url '
  2185. title
  2186. line1
  2187. line2
  2188. '
  2189. [foo]
  2190. .
  2191. <p><a href="/url" title="
  2192. title
  2193. line1
  2194. line2
  2195. ">foo</a></p>
  2196. ````````````````````````````````
  2197. However, it may not contain a [blank line]:
  2198. ```````````````````````````````` example
  2199. [foo]: /url 'title
  2200. with blank line'
  2201. [foo]
  2202. .
  2203. <p>[foo]: /url 'title</p>
  2204. <p>with blank line'</p>
  2205. <p>[foo]</p>
  2206. ````````````````````````````````
  2207. The title may be omitted:
  2208. ```````````````````````````````` example
  2209. [foo]:
  2210. /url
  2211. [foo]
  2212. .
  2213. <p><a href="/url">foo</a></p>
  2214. ````````````````````````````````
  2215. The link destination may not be omitted:
  2216. ```````````````````````````````` example
  2217. [foo]:
  2218. [foo]
  2219. .
  2220. <p>[foo]:</p>
  2221. <p>[foo]</p>
  2222. ````````````````````````````````
  2223. The title must be separated from the link destination by
  2224. whitespace:
  2225. ```````````````````````````````` example
  2226. [foo]: <bar>(baz)
  2227. [foo]
  2228. .
  2229. <p>[foo]: <bar>(baz)</p>
  2230. <p>[foo]</p>
  2231. ````````````````````````````````
  2232. Both title and destination can contain backslash escapes
  2233. and literal backslashes:
  2234. ```````````````````````````````` example
  2235. [foo]: /url\bar\*baz "foo\"bar\baz"
  2236. [foo]
  2237. .
  2238. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2239. ````````````````````````````````
  2240. A link can come before its corresponding definition:
  2241. ```````````````````````````````` example
  2242. [foo]
  2243. [foo]: url
  2244. .
  2245. <p><a href="url">foo</a></p>
  2246. ````````````````````````````````
  2247. If there are several matching definitions, the first one takes
  2248. precedence:
  2249. ```````````````````````````````` example
  2250. [foo]
  2251. [foo]: first
  2252. [foo]: second
  2253. .
  2254. <p><a href="first">foo</a></p>
  2255. ````````````````````````````````
  2256. As noted in the section on [Links], matching of labels is
  2257. case-insensitive (see [matches]).
  2258. ```````````````````````````````` example
  2259. [FOO]: /url
  2260. [Foo]
  2261. .
  2262. <p><a href="/url">Foo</a></p>
  2263. ````````````````````````````````
  2264. ```````````````````````````````` example
  2265. [ΑΓΩ]: /φου
  2266. [αγω]
  2267. .
  2268. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2269. ````````````````````````````````
  2270. Here is a link reference definition with no corresponding link.
  2271. It contributes nothing to the document.
  2272. ```````````````````````````````` example
  2273. [foo]: /url
  2274. .
  2275. ````````````````````````````````
  2276. Here is another one:
  2277. ```````````````````````````````` example
  2278. [
  2279. foo
  2280. ]: /url
  2281. bar
  2282. .
  2283. <p>bar</p>
  2284. ````````````````````````````````
  2285. This is not a link reference definition, because there are
  2286. [non-whitespace characters] after the title:
  2287. ```````````````````````````````` example
  2288. [foo]: /url "title" ok
  2289. .
  2290. <p>[foo]: /url &quot;title&quot; ok</p>
  2291. ````````````````````````````````
  2292. This is a link reference definition, but it has no title:
  2293. ```````````````````````````````` example
  2294. [foo]: /url
  2295. "title" ok
  2296. .
  2297. <p>&quot;title&quot; ok</p>
  2298. ````````````````````````````````
  2299. This is not a link reference definition, because it is indented
  2300. four spaces:
  2301. ```````````````````````````````` example
  2302. [foo]: /url "title"
  2303. [foo]
  2304. .
  2305. <pre><code>[foo]: /url &quot;title&quot;
  2306. </code></pre>
  2307. <p>[foo]</p>
  2308. ````````````````````````````````
  2309. This is not a link reference definition, because it occurs inside
  2310. a code block:
  2311. ```````````````````````````````` example
  2312. ```
  2313. [foo]: /url
  2314. ```
  2315. [foo]
  2316. .
  2317. <pre><code>[foo]: /url
  2318. </code></pre>
  2319. <p>[foo]</p>
  2320. ````````````````````````````````
  2321. A [link reference definition] cannot interrupt a paragraph.
  2322. ```````````````````````````````` example
  2323. Foo
  2324. [bar]: /baz
  2325. [bar]
  2326. .
  2327. <p>Foo
  2328. [bar]: /baz</p>
  2329. <p>[bar]</p>
  2330. ````````````````````````````````
  2331. However, it can directly follow other block elements, such as headings
  2332. and thematic breaks, and it need not be followed by a blank line.
  2333. ```````````````````````````````` example
  2334. # [Foo]
  2335. [foo]: /url
  2336. > bar
  2337. .
  2338. <h1><a href="/url">Foo</a></h1>
  2339. <blockquote>
  2340. <p>bar</p>
  2341. </blockquote>
  2342. ````````````````````````````````
  2343. Several [link reference definitions]
  2344. can occur one after another, without intervening blank lines.
  2345. ```````````````````````````````` example
  2346. [foo]: /foo-url "foo"
  2347. [bar]: /bar-url
  2348. "bar"
  2349. [baz]: /baz-url
  2350. [foo],
  2351. [bar],
  2352. [baz]
  2353. .
  2354. <p><a href="/foo-url" title="foo">foo</a>,
  2355. <a href="/bar-url" title="bar">bar</a>,
  2356. <a href="/baz-url">baz</a></p>
  2357. ````````````````````````````````
  2358. [Link reference definitions] can occur
  2359. inside block containers, like lists and block quotations. They
  2360. affect the entire document, not just the container in which they
  2361. are defined:
  2362. ```````````````````````````````` example
  2363. [foo]
  2364. > [foo]: /url
  2365. .
  2366. <p><a href="/url">foo</a></p>
  2367. <blockquote>
  2368. </blockquote>
  2369. ````````````````````````````````
  2370. ## Paragraphs
  2371. A sequence of non-blank lines that cannot be interpreted as other
  2372. kinds of blocks forms a [paragraph](@).
  2373. The contents of the paragraph are the result of parsing the
  2374. paragraph's raw content as inlines. The paragraph's raw content
  2375. is formed by concatenating the lines and removing initial and final
  2376. [whitespace].
  2377. A simple example with two paragraphs:
  2378. ```````````````````````````````` example
  2379. aaa
  2380. bbb
  2381. .
  2382. <p>aaa</p>
  2383. <p>bbb</p>
  2384. ````````````````````````````````
  2385. Paragraphs can contain multiple lines, but no blank lines:
  2386. ```````````````````````````````` example
  2387. aaa
  2388. bbb
  2389. ccc
  2390. ddd
  2391. .
  2392. <p>aaa
  2393. bbb</p>
  2394. <p>ccc
  2395. ddd</p>
  2396. ````````````````````````````````
  2397. Multiple blank lines between paragraph have no effect:
  2398. ```````````````````````````````` example
  2399. aaa
  2400. bbb
  2401. .
  2402. <p>aaa</p>
  2403. <p>bbb</p>
  2404. ````````````````````````````````
  2405. Leading spaces are skipped:
  2406. ```````````````````````````````` example
  2407. aaa
  2408. bbb
  2409. .
  2410. <p>aaa
  2411. bbb</p>
  2412. ````````````````````````````````
  2413. Lines after the first may be indented any amount, since indented
  2414. code blocks cannot interrupt paragraphs.
  2415. ```````````````````````````````` example
  2416. aaa
  2417. bbb
  2418. ccc
  2419. .
  2420. <p>aaa
  2421. bbb
  2422. ccc</p>
  2423. ````````````````````````````````
  2424. However, the first line may be indented at most three spaces,
  2425. or an indented code block will be triggered:
  2426. ```````````````````````````````` example
  2427. aaa
  2428. bbb
  2429. .
  2430. <p>aaa
  2431. bbb</p>
  2432. ````````````````````````````````
  2433. ```````````````````````````````` example
  2434. aaa
  2435. bbb
  2436. .
  2437. <pre><code>aaa
  2438. </code></pre>
  2439. <p>bbb</p>
  2440. ````````````````````````````````
  2441. Final spaces are stripped before inline parsing, so a paragraph
  2442. that ends with two or more spaces will not end with a [hard line
  2443. break]:
  2444. ```````````````````````````````` example
  2445. aaa
  2446. bbb
  2447. .
  2448. <p>aaa<br />
  2449. bbb</p>
  2450. ````````````````````````````````
  2451. ## Blank lines
  2452. [Blank lines] between block-level elements are ignored,
  2453. except for the role they play in determining whether a [list]
  2454. is [tight] or [loose].
  2455. Blank lines at the beginning and end of the document are also ignored.
  2456. ```````````````````````````````` example
  2457. aaa
  2458. # aaa
  2459. .
  2460. <p>aaa</p>
  2461. <h1>aaa</h1>
  2462. ````````````````````````````````
  2463. # Container blocks
  2464. A [container block](#container-blocks) is a block that has other
  2465. blocks as its contents. There are two basic kinds of container blocks:
  2466. [block quotes] and [list items].
  2467. [Lists] are meta-containers for [list items].
  2468. We define the syntax for container blocks recursively. The general
  2469. form of the definition is:
  2470. > If X is a sequence of blocks, then the result of
  2471. > transforming X in such-and-such a way is a container of type Y
  2472. > with these blocks as its content.
  2473. So, we explain what counts as a block quote or list item by explaining
  2474. how these can be *generated* from their contents. This should suffice
  2475. to define the syntax, although it does not give a recipe for *parsing*
  2476. these constructions. (A recipe is provided below in the section entitled
  2477. [A parsing strategy](#appendix-a-parsing-strategy).)
  2478. ## Block quotes
  2479. A [block quote marker](@)
  2480. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2481. with a following space, or (b) a single character `>` not followed by a space.
  2482. The following rules define [block quotes]:
  2483. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2484. of blocks *Bs*, then the result of prepending a [block quote
  2485. marker] to the beginning of each line in *Ls*
  2486. is a [block quote](#block-quotes) containing *Bs*.
  2487. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2488. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2489. the initial [block quote marker] from one or
  2490. more lines in which the next [non-whitespace character] after the [block
  2491. quote marker] is [paragraph continuation
  2492. text] is a block quote with *Bs* as its content.
  2493. [Paragraph continuation text](@) is text
  2494. that will be parsed as part of the content of a paragraph, but does
  2495. not occur at the beginning of the paragraph.
  2496. 3. **Consecutiveness.** A document cannot contain two [block
  2497. quotes] in a row unless there is a [blank line] between them.
  2498. Nothing else counts as a [block quote](#block-quotes).
  2499. Here is a simple example:
  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. The spaces after the `>` characters can be omitted:
  2512. ```````````````````````````````` example
  2513. ># Foo
  2514. >bar
  2515. > baz
  2516. .
  2517. <blockquote>
  2518. <h1>Foo</h1>
  2519. <p>bar
  2520. baz</p>
  2521. </blockquote>
  2522. ````````````````````````````````
  2523. The `>` characters can be indented 1-3 spaces:
  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. Four spaces gives us a code block:
  2536. ```````````````````````````````` example
  2537. > # Foo
  2538. > bar
  2539. > baz
  2540. .
  2541. <pre><code>&gt; # Foo
  2542. &gt; bar
  2543. &gt; baz
  2544. </code></pre>
  2545. ````````````````````````````````
  2546. The Laziness clause allows us to omit the `>` before
  2547. [paragraph continuation text]:
  2548. ```````````````````````````````` example
  2549. > # Foo
  2550. > bar
  2551. baz
  2552. .
  2553. <blockquote>
  2554. <h1>Foo</h1>
  2555. <p>bar
  2556. baz</p>
  2557. </blockquote>
  2558. ````````````````````````````````
  2559. A block quote can contain some lazy and some non-lazy
  2560. continuation lines:
  2561. ```````````````````````````````` example
  2562. > bar
  2563. baz
  2564. > foo
  2565. .
  2566. <blockquote>
  2567. <p>bar
  2568. baz
  2569. foo</p>
  2570. </blockquote>
  2571. ````````````````````````````````
  2572. Laziness only applies to lines that would have been continuations of
  2573. paragraphs had they been prepended with [block quote markers].
  2574. For example, the `> ` cannot be omitted in the second line of
  2575. ``` markdown
  2576. > foo
  2577. > ---
  2578. ```
  2579. without changing the meaning:
  2580. ```````````````````````````````` example
  2581. > foo
  2582. ---
  2583. .
  2584. <blockquote>
  2585. <p>foo</p>
  2586. </blockquote>
  2587. <hr />
  2588. ````````````````````````````````
  2589. Similarly, if we omit the `> ` in the second line of
  2590. ``` markdown
  2591. > - foo
  2592. > - bar
  2593. ```
  2594. then the block quote ends after the first line:
  2595. ```````````````````````````````` example
  2596. > - foo
  2597. - bar
  2598. .
  2599. <blockquote>
  2600. <ul>
  2601. <li>foo</li>
  2602. </ul>
  2603. </blockquote>
  2604. <ul>
  2605. <li>bar</li>
  2606. </ul>
  2607. ````````````````````````````````
  2608. For the same reason, we can't omit the `> ` in front of
  2609. subsequent lines of an indented or fenced code block:
  2610. ```````````````````````````````` example
  2611. > foo
  2612. bar
  2613. .
  2614. <blockquote>
  2615. <pre><code>foo
  2616. </code></pre>
  2617. </blockquote>
  2618. <pre><code>bar
  2619. </code></pre>
  2620. ````````````````````````````````
  2621. ```````````````````````````````` example
  2622. > ```
  2623. foo
  2624. ```
  2625. .
  2626. <blockquote>
  2627. <pre><code></code></pre>
  2628. </blockquote>
  2629. <p>foo</p>
  2630. <pre><code></code></pre>
  2631. ````````````````````````````````
  2632. Note that in the following case, we have a [lazy
  2633. continuation line]:
  2634. ```````````````````````````````` example
  2635. > foo
  2636. - bar
  2637. .
  2638. <blockquote>
  2639. <p>foo
  2640. - bar</p>
  2641. </blockquote>
  2642. ````````````````````````````````
  2643. To see why, note that in
  2644. ```markdown
  2645. > foo
  2646. > - bar
  2647. ```
  2648. the `- bar` is indented too far to start a list, and can't
  2649. be an indented code block because indented code blocks cannot
  2650. interrupt paragraphs, so it is [paragraph continuation text].
  2651. A block quote can be empty:
  2652. ```````````````````````````````` example
  2653. >
  2654. .
  2655. <blockquote>
  2656. </blockquote>
  2657. ````````````````````````````````
  2658. ```````````````````````````````` example
  2659. >
  2660. >
  2661. >
  2662. .
  2663. <blockquote>
  2664. </blockquote>
  2665. ````````````````````````````````
  2666. A block quote can have initial or final blank lines:
  2667. ```````````````````````````````` example
  2668. >
  2669. > foo
  2670. >
  2671. .
  2672. <blockquote>
  2673. <p>foo</p>
  2674. </blockquote>
  2675. ````````````````````````````````
  2676. A blank line always separates block quotes:
  2677. ```````````````````````````````` example
  2678. > foo
  2679. > bar
  2680. .
  2681. <blockquote>
  2682. <p>foo</p>
  2683. </blockquote>
  2684. <blockquote>
  2685. <p>bar</p>
  2686. </blockquote>
  2687. ````````````````````````````````
  2688. (Most current Markdown implementations, including John Gruber's
  2689. original `Markdown.pl`, will parse this example as a single block quote
  2690. with two paragraphs. But it seems better to allow the author to decide
  2691. whether two block quotes or one are wanted.)
  2692. Consecutiveness means that if we put these block quotes together,
  2693. we get a single block quote:
  2694. ```````````````````````````````` example
  2695. > foo
  2696. > bar
  2697. .
  2698. <blockquote>
  2699. <p>foo
  2700. bar</p>
  2701. </blockquote>
  2702. ````````````````````````````````
  2703. To get a block quote with two paragraphs, use:
  2704. ```````````````````````````````` example
  2705. > foo
  2706. >
  2707. > bar
  2708. .
  2709. <blockquote>
  2710. <p>foo</p>
  2711. <p>bar</p>
  2712. </blockquote>
  2713. ````````````````````````````````
  2714. Block quotes can interrupt paragraphs:
  2715. ```````````````````````````````` example
  2716. foo
  2717. > bar
  2718. .
  2719. <p>foo</p>
  2720. <blockquote>
  2721. <p>bar</p>
  2722. </blockquote>
  2723. ````````````````````````````````
  2724. In general, blank lines are not needed before or after block
  2725. quotes:
  2726. ```````````````````````````````` example
  2727. > aaa
  2728. ***
  2729. > bbb
  2730. .
  2731. <blockquote>
  2732. <p>aaa</p>
  2733. </blockquote>
  2734. <hr />
  2735. <blockquote>
  2736. <p>bbb</p>
  2737. </blockquote>
  2738. ````````````````````````````````
  2739. However, because of laziness, a blank line is needed between
  2740. a block quote and a following paragraph:
  2741. ```````````````````````````````` example
  2742. > bar
  2743. baz
  2744. .
  2745. <blockquote>
  2746. <p>bar
  2747. baz</p>
  2748. </blockquote>
  2749. ````````````````````````````````
  2750. ```````````````````````````````` example
  2751. > bar
  2752. baz
  2753. .
  2754. <blockquote>
  2755. <p>bar</p>
  2756. </blockquote>
  2757. <p>baz</p>
  2758. ````````````````````````````````
  2759. ```````````````````````````````` example
  2760. > bar
  2761. >
  2762. baz
  2763. .
  2764. <blockquote>
  2765. <p>bar</p>
  2766. </blockquote>
  2767. <p>baz</p>
  2768. ````````````````````````````````
  2769. It is a consequence of the Laziness rule that any number
  2770. of initial `>`s may be omitted on a continuation line of a
  2771. nested block quote:
  2772. ```````````````````````````````` example
  2773. > > > foo
  2774. bar
  2775. .
  2776. <blockquote>
  2777. <blockquote>
  2778. <blockquote>
  2779. <p>foo
  2780. bar</p>
  2781. </blockquote>
  2782. </blockquote>
  2783. </blockquote>
  2784. ````````````````````````````````
  2785. ```````````````````````````````` example
  2786. >>> foo
  2787. > bar
  2788. >>baz
  2789. .
  2790. <blockquote>
  2791. <blockquote>
  2792. <blockquote>
  2793. <p>foo
  2794. bar
  2795. baz</p>
  2796. </blockquote>
  2797. </blockquote>
  2798. </blockquote>
  2799. ````````````````````````````````
  2800. When including an indented code block in a block quote,
  2801. remember that the [block quote marker] includes
  2802. both the `>` and a following space. So *five spaces* are needed after
  2803. the `>`:
  2804. ```````````````````````````````` example
  2805. > code
  2806. > not code
  2807. .
  2808. <blockquote>
  2809. <pre><code>code
  2810. </code></pre>
  2811. </blockquote>
  2812. <blockquote>
  2813. <p>not code</p>
  2814. </blockquote>
  2815. ````````````````````````````````
  2816. ## List items
  2817. A [list marker](@) is a
  2818. [bullet list marker] or an [ordered list marker].
  2819. A [bullet list marker](@)
  2820. is a `-`, `+`, or `*` character.
  2821. An [ordered list marker](@)
  2822. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2823. `.` character or a `)` character. (The reason for the length
  2824. limit is that with 10 digits we start seeing integer overflows
  2825. in some browsers.)
  2826. The following rules define [list items]:
  2827. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2828. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  2829. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2830. of prepending *M* and the following spaces to the first line of
  2831. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2832. list item with *Bs* as its contents. The type of the list item
  2833. (bullet or ordered) is determined by the type of its list marker.
  2834. If the list item is ordered, then it is also assigned a start
  2835. number, based on the ordered list marker.
  2836. Exceptions:
  2837. 1. When the first list item in a [list] interrupts
  2838. a paragraph---that is, when it starts on a line that would
  2839. otherwise count as [paragraph continuation text]---then (a)
  2840. the lines *Ls* must not begin with a blank line, and (b) if
  2841. the list item is ordered, the start number must be 1.
  2842. 2. If any line is a [thematic break][thematic breaks] then
  2843. that line is not a list item.
  2844. For example, let *Ls* be the lines
  2845. ```````````````````````````````` example
  2846. A paragraph
  2847. with two lines.
  2848. indented code
  2849. > A block quote.
  2850. .
  2851. <p>A paragraph
  2852. with two lines.</p>
  2853. <pre><code>indented code
  2854. </code></pre>
  2855. <blockquote>
  2856. <p>A block quote.</p>
  2857. </blockquote>
  2858. ````````````````````````````````
  2859. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2860. that the following is an ordered list item with start number 1,
  2861. and the same contents as *Ls*:
  2862. ```````````````````````````````` example
  2863. 1. A paragraph
  2864. with two lines.
  2865. indented code
  2866. > A block quote.
  2867. .
  2868. <ol>
  2869. <li>
  2870. <p>A paragraph
  2871. with two lines.</p>
  2872. <pre><code>indented code
  2873. </code></pre>
  2874. <blockquote>
  2875. <p>A block quote.</p>
  2876. </blockquote>
  2877. </li>
  2878. </ol>
  2879. ````````````````````````````````
  2880. The most important thing to notice is that the position of
  2881. the text after the list marker determines how much indentation
  2882. is needed in subsequent blocks in the list item. If the list
  2883. marker takes up two spaces, and there are three spaces between
  2884. the list marker and the next [non-whitespace character], then blocks
  2885. must be indented five spaces in order to fall under the list
  2886. item.
  2887. Here are some examples showing how far content must be indented to be
  2888. put under the list item:
  2889. ```````````````````````````````` example
  2890. - one
  2891. two
  2892. .
  2893. <ul>
  2894. <li>one</li>
  2895. </ul>
  2896. <p>two</p>
  2897. ````````````````````````````````
  2898. ```````````````````````````````` example
  2899. - one
  2900. two
  2901. .
  2902. <ul>
  2903. <li>
  2904. <p>one</p>
  2905. <p>two</p>
  2906. </li>
  2907. </ul>
  2908. ````````````````````````````````
  2909. ```````````````````````````````` example
  2910. - one
  2911. two
  2912. .
  2913. <ul>
  2914. <li>one</li>
  2915. </ul>
  2916. <pre><code> two
  2917. </code></pre>
  2918. ````````````````````````````````
  2919. ```````````````````````````````` example
  2920. - one
  2921. two
  2922. .
  2923. <ul>
  2924. <li>
  2925. <p>one</p>
  2926. <p>two</p>
  2927. </li>
  2928. </ul>
  2929. ````````````````````````````````
  2930. It is tempting to think of this in terms of columns: the continuation
  2931. blocks must be indented at least to the column of the first
  2932. [non-whitespace character] after the list marker. However, that is not quite right.
  2933. The spaces after the list marker determine how much relative indentation
  2934. is needed. Which column this indentation reaches will depend on
  2935. how the list item is embedded in other constructions, as shown by
  2936. this example:
  2937. ```````````````````````````````` example
  2938. > > 1. one
  2939. >>
  2940. >> two
  2941. .
  2942. <blockquote>
  2943. <blockquote>
  2944. <ol>
  2945. <li>
  2946. <p>one</p>
  2947. <p>two</p>
  2948. </li>
  2949. </ol>
  2950. </blockquote>
  2951. </blockquote>
  2952. ````````````````````````````````
  2953. Here `two` occurs in the same column as the list marker `1.`,
  2954. but is actually contained in the list item, because there is
  2955. sufficient indentation after the last containing blockquote marker.
  2956. The converse is also possible. In the following example, the word `two`
  2957. occurs far to the right of the initial text of the list item, `one`, but
  2958. it is not considered part of the list item, because it is not indented
  2959. far enough past the blockquote marker:
  2960. ```````````````````````````````` example
  2961. >>- one
  2962. >>
  2963. > > two
  2964. .
  2965. <blockquote>
  2966. <blockquote>
  2967. <ul>
  2968. <li>one</li>
  2969. </ul>
  2970. <p>two</p>
  2971. </blockquote>
  2972. </blockquote>
  2973. ````````````````````````````````
  2974. Note that at least one space is needed between the list marker and
  2975. any following content, so these are not list items:
  2976. ```````````````````````````````` example
  2977. -one
  2978. 2.two
  2979. .
  2980. <p>-one</p>
  2981. <p>2.two</p>
  2982. ````````````````````````````````
  2983. A list item may contain blocks that are separated by more than
  2984. one blank line.
  2985. ```````````````````````````````` example
  2986. - foo
  2987. bar
  2988. .
  2989. <ul>
  2990. <li>
  2991. <p>foo</p>
  2992. <p>bar</p>
  2993. </li>
  2994. </ul>
  2995. ````````````````````````````````
  2996. A list item may contain any kind of block:
  2997. ```````````````````````````````` example
  2998. 1. foo
  2999. ```
  3000. bar
  3001. ```
  3002. baz
  3003. > bam
  3004. .
  3005. <ol>
  3006. <li>
  3007. <p>foo</p>
  3008. <pre><code>bar
  3009. </code></pre>
  3010. <p>baz</p>
  3011. <blockquote>
  3012. <p>bam</p>
  3013. </blockquote>
  3014. </li>
  3015. </ol>
  3016. ````````````````````````````````
  3017. A list item that contains an indented code block will preserve
  3018. empty lines within the code block verbatim.
  3019. ```````````````````````````````` example
  3020. - Foo
  3021. bar
  3022. baz
  3023. .
  3024. <ul>
  3025. <li>
  3026. <p>Foo</p>
  3027. <pre><code>bar
  3028. baz
  3029. </code></pre>
  3030. </li>
  3031. </ul>
  3032. ````````````````````````````````
  3033. Note that ordered list start numbers must be nine digits or less:
  3034. ```````````````````````````````` example
  3035. 123456789. ok
  3036. .
  3037. <ol start="123456789">
  3038. <li>ok</li>
  3039. </ol>
  3040. ````````````````````````````````
  3041. ```````````````````````````````` example
  3042. 1234567890. not ok
  3043. .
  3044. <p>1234567890. not ok</p>
  3045. ````````````````````````````````
  3046. A start number may begin with 0s:
  3047. ```````````````````````````````` example
  3048. 0. ok
  3049. .
  3050. <ol start="0">
  3051. <li>ok</li>
  3052. </ol>
  3053. ````````````````````````````````
  3054. ```````````````````````````````` example
  3055. 003. ok
  3056. .
  3057. <ol start="3">
  3058. <li>ok</li>
  3059. </ol>
  3060. ````````````````````````````````
  3061. A start number may not be negative:
  3062. ```````````````````````````````` example
  3063. -1. not ok
  3064. .
  3065. <p>-1. not ok</p>
  3066. ````````````````````````````````
  3067. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3068. constitute a sequence of blocks *Bs* starting with an indented code
  3069. block, and *M* is a list marker of width *W* followed by
  3070. one space, then the result of prepending *M* and the following
  3071. space to the first line of *Ls*, and indenting subsequent lines of
  3072. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3073. If a line is empty, then it need not be indented. The type of the
  3074. list item (bullet or ordered) is determined by the type of its list
  3075. marker. If the list item is ordered, then it is also assigned a
  3076. start number, based on the ordered list marker.
  3077. An indented code block will have to be indented four spaces beyond
  3078. the edge of the region where text will be included in the list item.
  3079. In the following case that is 6 spaces:
  3080. ```````````````````````````````` example
  3081. - foo
  3082. bar
  3083. .
  3084. <ul>
  3085. <li>
  3086. <p>foo</p>
  3087. <pre><code>bar
  3088. </code></pre>
  3089. </li>
  3090. </ul>
  3091. ````````````````````````````````
  3092. And in this case it is 11 spaces:
  3093. ```````````````````````````````` example
  3094. 10. foo
  3095. bar
  3096. .
  3097. <ol start="10">
  3098. <li>
  3099. <p>foo</p>
  3100. <pre><code>bar
  3101. </code></pre>
  3102. </li>
  3103. </ol>
  3104. ````````````````````````````````
  3105. If the *first* block in the list item is an indented code block,
  3106. then by rule #2, the contents must be indented *one* space after the
  3107. list marker:
  3108. ```````````````````````````````` example
  3109. indented code
  3110. paragraph
  3111. more code
  3112. .
  3113. <pre><code>indented code
  3114. </code></pre>
  3115. <p>paragraph</p>
  3116. <pre><code>more code
  3117. </code></pre>
  3118. ````````````````````````````````
  3119. ```````````````````````````````` example
  3120. 1. indented code
  3121. paragraph
  3122. more code
  3123. .
  3124. <ol>
  3125. <li>
  3126. <pre><code>indented code
  3127. </code></pre>
  3128. <p>paragraph</p>
  3129. <pre><code>more code
  3130. </code></pre>
  3131. </li>
  3132. </ol>
  3133. ````````````````````````````````
  3134. Note that an additional space indent is interpreted as space
  3135. inside the code block:
  3136. ```````````````````````````````` example
  3137. 1. indented code
  3138. paragraph
  3139. more code
  3140. .
  3141. <ol>
  3142. <li>
  3143. <pre><code> indented code
  3144. </code></pre>
  3145. <p>paragraph</p>
  3146. <pre><code>more code
  3147. </code></pre>
  3148. </li>
  3149. </ol>
  3150. ````````````````````````````````
  3151. Note that rules #1 and #2 only apply to two cases: (a) cases
  3152. in which the lines to be included in a list item begin with a
  3153. [non-whitespace character], and (b) cases in which
  3154. they begin with an indented code
  3155. block. In a case like the following, where the first block begins with
  3156. a three-space indent, the rules do not allow us to form a list item by
  3157. indenting the whole thing and prepending a list marker:
  3158. ```````````````````````````````` example
  3159. foo
  3160. bar
  3161. .
  3162. <p>foo</p>
  3163. <p>bar</p>
  3164. ````````````````````````````````
  3165. ```````````````````````````````` example
  3166. - foo
  3167. bar
  3168. .
  3169. <ul>
  3170. <li>foo</li>
  3171. </ul>
  3172. <p>bar</p>
  3173. ````````````````````````````````
  3174. This is not a significant restriction, because when a block begins
  3175. with 1-3 spaces indent, the indentation can always be removed without
  3176. a change in interpretation, allowing rule #1 to be applied. So, in
  3177. the above case:
  3178. ```````````````````````````````` example
  3179. - foo
  3180. bar
  3181. .
  3182. <ul>
  3183. <li>
  3184. <p>foo</p>
  3185. <p>bar</p>
  3186. </li>
  3187. </ul>
  3188. ````````````````````````````````
  3189. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3190. starting with a single [blank line] constitute a (possibly empty)
  3191. sequence of blocks *Bs*, not separated from each other by more than
  3192. one blank line, and *M* is a list marker of width *W*,
  3193. then the result of prepending *M* to the first line of *Ls*, and
  3194. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3195. item with *Bs* as its contents.
  3196. If a line is empty, then it need not be indented. The type of the
  3197. list item (bullet or ordered) is determined by the type of its list
  3198. marker. If the list item is ordered, then it is also assigned a
  3199. start number, based on the ordered list marker.
  3200. Here are some list items that start with a blank line but are not empty:
  3201. ```````````````````````````````` example
  3202. -
  3203. foo
  3204. -
  3205. ```
  3206. bar
  3207. ```
  3208. -
  3209. baz
  3210. .
  3211. <ul>
  3212. <li>foo</li>
  3213. <li>
  3214. <pre><code>bar
  3215. </code></pre>
  3216. </li>
  3217. <li>
  3218. <pre><code>baz
  3219. </code></pre>
  3220. </li>
  3221. </ul>
  3222. ````````````````````````````````
  3223. When the list item starts with a blank line, the number of spaces
  3224. following the list marker doesn't change the required indentation:
  3225. ```````````````````````````````` example
  3226. -
  3227. foo
  3228. .
  3229. <ul>
  3230. <li>foo</li>
  3231. </ul>
  3232. ````````````````````````````````
  3233. A list item can begin with at most one blank line.
  3234. In the following example, `foo` is not part of the list
  3235. item:
  3236. ```````````````````````````````` example
  3237. -
  3238. foo
  3239. .
  3240. <ul>
  3241. <li></li>
  3242. </ul>
  3243. <p>foo</p>
  3244. ````````````````````````````````
  3245. Here is an empty bullet list item:
  3246. ```````````````````````````````` example
  3247. - foo
  3248. -
  3249. - bar
  3250. .
  3251. <ul>
  3252. <li>foo</li>
  3253. <li></li>
  3254. <li>bar</li>
  3255. </ul>
  3256. ````````````````````````````````
  3257. It does not matter whether there are spaces following the [list marker]:
  3258. ```````````````````````````````` example
  3259. - foo
  3260. -
  3261. - bar
  3262. .
  3263. <ul>
  3264. <li>foo</li>
  3265. <li></li>
  3266. <li>bar</li>
  3267. </ul>
  3268. ````````````````````````````````
  3269. Here is an empty ordered list item:
  3270. ```````````````````````````````` example
  3271. 1. foo
  3272. 2.
  3273. 3. bar
  3274. .
  3275. <ol>
  3276. <li>foo</li>
  3277. <li></li>
  3278. <li>bar</li>
  3279. </ol>
  3280. ````````````````````````````````
  3281. A list may start or end with an empty list item:
  3282. ```````````````````````````````` example
  3283. *
  3284. .
  3285. <ul>
  3286. <li></li>
  3287. </ul>
  3288. ````````````````````````````````
  3289. However, an empty list item cannot interrupt a paragraph:
  3290. ```````````````````````````````` example
  3291. foo
  3292. *
  3293. foo
  3294. 1.
  3295. .
  3296. <p>foo
  3297. *</p>
  3298. <p>foo
  3299. 1.</p>
  3300. ````````````````````````````````
  3301. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3302. according to rule #1, #2, or #3, then the result of indenting each line
  3303. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3304. list item with the same contents and attributes. If a line is
  3305. empty, then it need not be indented.
  3306. Indented one space:
  3307. ```````````````````````````````` example
  3308. 1. A paragraph
  3309. with two lines.
  3310. indented code
  3311. > A block quote.
  3312. .
  3313. <ol>
  3314. <li>
  3315. <p>A paragraph
  3316. with two lines.</p>
  3317. <pre><code>indented code
  3318. </code></pre>
  3319. <blockquote>
  3320. <p>A block quote.</p>
  3321. </blockquote>
  3322. </li>
  3323. </ol>
  3324. ````````````````````````````````
  3325. Indented two spaces:
  3326. ```````````````````````````````` example
  3327. 1. A paragraph
  3328. with two lines.
  3329. indented code
  3330. > A block quote.
  3331. .
  3332. <ol>
  3333. <li>
  3334. <p>A paragraph
  3335. with two lines.</p>
  3336. <pre><code>indented code
  3337. </code></pre>
  3338. <blockquote>
  3339. <p>A block quote.</p>
  3340. </blockquote>
  3341. </li>
  3342. </ol>
  3343. ````````````````````````````````
  3344. Indented three spaces:
  3345. ```````````````````````````````` example
  3346. 1. A paragraph
  3347. with two lines.
  3348. indented code
  3349. > A block quote.
  3350. .
  3351. <ol>
  3352. <li>
  3353. <p>A paragraph
  3354. with two lines.</p>
  3355. <pre><code>indented code
  3356. </code></pre>
  3357. <blockquote>
  3358. <p>A block quote.</p>
  3359. </blockquote>
  3360. </li>
  3361. </ol>
  3362. ````````````````````````````````
  3363. Four spaces indent gives a code block:
  3364. ```````````````````````````````` example
  3365. 1. A paragraph
  3366. with two lines.
  3367. indented code
  3368. > A block quote.
  3369. .
  3370. <pre><code>1. A paragraph
  3371. with two lines.
  3372. indented code
  3373. &gt; A block quote.
  3374. </code></pre>
  3375. ````````````````````````````````
  3376. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3377. item](#list-items) with contents *Bs*, then the result of deleting
  3378. some or all of the indentation from one or more lines in which the
  3379. next [non-whitespace character] after the indentation is
  3380. [paragraph continuation text] is a
  3381. list item with the same contents and attributes. The unindented
  3382. lines are called
  3383. [lazy continuation line](@)s.
  3384. Here is an example with [lazy continuation lines]:
  3385. ```````````````````````````````` example
  3386. 1. A paragraph
  3387. with two lines.
  3388. indented code
  3389. > A block quote.
  3390. .
  3391. <ol>
  3392. <li>
  3393. <p>A paragraph
  3394. with two lines.</p>
  3395. <pre><code>indented code
  3396. </code></pre>
  3397. <blockquote>
  3398. <p>A block quote.</p>
  3399. </blockquote>
  3400. </li>
  3401. </ol>
  3402. ````````````````````````````````
  3403. Indentation can be partially deleted:
  3404. ```````````````````````````````` example
  3405. 1. A paragraph
  3406. with two lines.
  3407. .
  3408. <ol>
  3409. <li>A paragraph
  3410. with two lines.</li>
  3411. </ol>
  3412. ````````````````````````````````
  3413. These examples show how laziness can work in nested structures:
  3414. ```````````````````````````````` example
  3415. > 1. > Blockquote
  3416. continued here.
  3417. .
  3418. <blockquote>
  3419. <ol>
  3420. <li>
  3421. <blockquote>
  3422. <p>Blockquote
  3423. continued here.</p>
  3424. </blockquote>
  3425. </li>
  3426. </ol>
  3427. </blockquote>
  3428. ````````````````````````````````
  3429. ```````````````````````````````` example
  3430. > 1. > Blockquote
  3431. > continued here.
  3432. .
  3433. <blockquote>
  3434. <ol>
  3435. <li>
  3436. <blockquote>
  3437. <p>Blockquote
  3438. continued here.</p>
  3439. </blockquote>
  3440. </li>
  3441. </ol>
  3442. </blockquote>
  3443. ````````````````````````````````
  3444. 6. **That's all.** Nothing that is not counted as a list item by rules
  3445. #1--5 counts as a [list item](#list-items).
  3446. The rules for sublists follow from the general rules
  3447. [above][List items]. A sublist must be indented the same number
  3448. of spaces a paragraph would need to be in order to be included
  3449. in the list item.
  3450. So, in this case we need two spaces indent:
  3451. ```````````````````````````````` example
  3452. - foo
  3453. - bar
  3454. - baz
  3455. - boo
  3456. .
  3457. <ul>
  3458. <li>foo
  3459. <ul>
  3460. <li>bar
  3461. <ul>
  3462. <li>baz
  3463. <ul>
  3464. <li>boo</li>
  3465. </ul>
  3466. </li>
  3467. </ul>
  3468. </li>
  3469. </ul>
  3470. </li>
  3471. </ul>
  3472. ````````````````````````````````
  3473. One is not enough:
  3474. ```````````````````````````````` example
  3475. - foo
  3476. - bar
  3477. - baz
  3478. - boo
  3479. .
  3480. <ul>
  3481. <li>foo</li>
  3482. <li>bar</li>
  3483. <li>baz</li>
  3484. <li>boo</li>
  3485. </ul>
  3486. ````````````````````````````````
  3487. Here we need four, because the list marker is wider:
  3488. ```````````````````````````````` example
  3489. 10) foo
  3490. - bar
  3491. .
  3492. <ol start="10">
  3493. <li>foo
  3494. <ul>
  3495. <li>bar</li>
  3496. </ul>
  3497. </li>
  3498. </ol>
  3499. ````````````````````````````````
  3500. Three is not enough:
  3501. ```````````````````````````````` example
  3502. 10) foo
  3503. - bar
  3504. .
  3505. <ol start="10">
  3506. <li>foo</li>
  3507. </ol>
  3508. <ul>
  3509. <li>bar</li>
  3510. </ul>
  3511. ````````````````````````````````
  3512. A list may be the first block in a list item:
  3513. ```````````````````````````````` example
  3514. - - foo
  3515. .
  3516. <ul>
  3517. <li>
  3518. <ul>
  3519. <li>foo</li>
  3520. </ul>
  3521. </li>
  3522. </ul>
  3523. ````````````````````````````````
  3524. ```````````````````````````````` example
  3525. 1. - 2. foo
  3526. .
  3527. <ol>
  3528. <li>
  3529. <ul>
  3530. <li>
  3531. <ol start="2">
  3532. <li>foo</li>
  3533. </ol>
  3534. </li>
  3535. </ul>
  3536. </li>
  3537. </ol>
  3538. ````````````````````````````````
  3539. A list item can contain a heading:
  3540. ```````````````````````````````` example
  3541. - # Foo
  3542. - Bar
  3543. ---
  3544. baz
  3545. .
  3546. <ul>
  3547. <li>
  3548. <h1>Foo</h1>
  3549. </li>
  3550. <li>
  3551. <h2>Bar</h2>
  3552. baz</li>
  3553. </ul>
  3554. ````````````````````````````````
  3555. ### Motivation
  3556. John Gruber's Markdown spec says the following about list items:
  3557. 1. "List markers typically start at the left margin, but may be indented
  3558. by up to three spaces. List markers must be followed by one or more
  3559. spaces or a tab."
  3560. 2. "To make lists look nice, you can wrap items with hanging indents....
  3561. But if you don't want to, you don't have to."
  3562. 3. "List items may consist of multiple paragraphs. Each subsequent
  3563. paragraph in a list item must be indented by either 4 spaces or one
  3564. tab."
  3565. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3566. but here again, Markdown will allow you to be lazy."
  3567. 5. "To put a blockquote within a list item, the blockquote's `>`
  3568. delimiters need to be indented."
  3569. 6. "To put a code block within a list item, the code block needs to be
  3570. indented twice — 8 spaces or two tabs."
  3571. These rules specify that a paragraph under a list item must be indented
  3572. four spaces (presumably, from the left margin, rather than the start of
  3573. the list marker, but this is not said), and that code under a list item
  3574. must be indented eight spaces instead of the usual four. They also say
  3575. that a block quote must be indented, but not by how much; however, the
  3576. example given has four spaces indentation. Although nothing is said
  3577. about other kinds of block-level content, it is certainly reasonable to
  3578. infer that *all* block elements under a list item, including other
  3579. lists, must be indented four spaces. This principle has been called the
  3580. *four-space rule*.
  3581. The four-space rule is clear and principled, and if the reference
  3582. implementation `Markdown.pl` had followed it, it probably would have
  3583. become the standard. However, `Markdown.pl` allowed paragraphs and
  3584. sublists to start with only two spaces indentation, at least on the
  3585. outer level. Worse, its behavior was inconsistent: a sublist of an
  3586. outer-level list needed two spaces indentation, but a sublist of this
  3587. sublist needed three spaces. It is not surprising, then, that different
  3588. implementations of Markdown have developed very different rules for
  3589. determining what comes under a list item. (Pandoc and python-Markdown,
  3590. for example, stuck with Gruber's syntax description and the four-space
  3591. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3592. followed `Markdown.pl`'s behavior more closely.)
  3593. Unfortunately, given the divergences between implementations, there
  3594. is no way to give a spec for list items that will be guaranteed not
  3595. to break any existing documents. However, the spec given here should
  3596. correctly handle lists formatted with either the four-space rule or
  3597. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3598. in a way that is natural for a human to read.
  3599. The strategy here is to let the width and indentation of the list marker
  3600. determine the indentation necessary for blocks to fall under the list
  3601. item, rather than having a fixed and arbitrary number. The writer can
  3602. think of the body of the list item as a unit which gets indented to the
  3603. right enough to fit the list marker (and any indentation on the list
  3604. marker). (The laziness rule, #5, then allows continuation lines to be
  3605. unindented if needed.)
  3606. This rule is superior, we claim, to any rule requiring a fixed level of
  3607. indentation from the margin. The four-space rule is clear but
  3608. unnatural. It is quite unintuitive that
  3609. ``` markdown
  3610. - foo
  3611. bar
  3612. - baz
  3613. ```
  3614. should be parsed as two lists with an intervening paragraph,
  3615. ``` html
  3616. <ul>
  3617. <li>foo</li>
  3618. </ul>
  3619. <p>bar</p>
  3620. <ul>
  3621. <li>baz</li>
  3622. </ul>
  3623. ```
  3624. as the four-space rule demands, rather than a single list,
  3625. ``` html
  3626. <ul>
  3627. <li>
  3628. <p>foo</p>
  3629. <p>bar</p>
  3630. <ul>
  3631. <li>baz</li>
  3632. </ul>
  3633. </li>
  3634. </ul>
  3635. ```
  3636. The choice of four spaces is arbitrary. It can be learned, but it is
  3637. not likely to be guessed, and it trips up beginners regularly.
  3638. Would it help to adopt a two-space rule? The problem is that such
  3639. a rule, together with the rule allowing 1--3 spaces indentation of the
  3640. initial list marker, allows text that is indented *less than* the
  3641. original list marker to be included in the list item. For example,
  3642. `Markdown.pl` parses
  3643. ``` markdown
  3644. - one
  3645. two
  3646. ```
  3647. as a single list item, with `two` a continuation paragraph:
  3648. ``` html
  3649. <ul>
  3650. <li>
  3651. <p>one</p>
  3652. <p>two</p>
  3653. </li>
  3654. </ul>
  3655. ```
  3656. and similarly
  3657. ``` markdown
  3658. > - one
  3659. >
  3660. > two
  3661. ```
  3662. as
  3663. ``` html
  3664. <blockquote>
  3665. <ul>
  3666. <li>
  3667. <p>one</p>
  3668. <p>two</p>
  3669. </li>
  3670. </ul>
  3671. </blockquote>
  3672. ```
  3673. This is extremely unintuitive.
  3674. Rather than requiring a fixed indent from the margin, we could require
  3675. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3676. may itself be indented). This proposal would remove the last anomaly
  3677. discussed. Unlike the spec presented above, it would count the following
  3678. as a list item with a subparagraph, even though the paragraph `bar`
  3679. is not indented as far as the first paragraph `foo`:
  3680. ``` markdown
  3681. 10. foo
  3682. bar
  3683. ```
  3684. Arguably this text does read like a list item with `bar` as a subparagraph,
  3685. which may count in favor of the proposal. However, on this proposal indented
  3686. code would have to be indented six spaces after the list marker. And this
  3687. would break a lot of existing Markdown, which has the pattern:
  3688. ``` markdown
  3689. 1. foo
  3690. indented code
  3691. ```
  3692. where the code is indented eight spaces. The spec above, by contrast, will
  3693. parse this text as expected, since the code block's indentation is measured
  3694. from the beginning of `foo`.
  3695. The one case that needs special treatment is a list item that *starts*
  3696. with indented code. How much indentation is required in that case, since
  3697. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3698. that in such cases, we require one space indentation from the list marker
  3699. (and then the normal four spaces for the indented code). This will match the
  3700. four-space rule in cases where the list marker plus its initial indentation
  3701. takes four spaces (a common case), but diverge in other cases.
  3702. ## Lists
  3703. A [list](@) is a sequence of one or more
  3704. list items [of the same type]. The list items
  3705. may be separated by any number of blank lines.
  3706. Two list items are [of the same type](@)
  3707. if they begin with a [list marker] of the same type.
  3708. Two list markers are of the
  3709. same type if (a) they are bullet list markers using the same character
  3710. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3711. delimiter (either `.` or `)`).
  3712. A list is an [ordered list](@)
  3713. if its constituent list items begin with
  3714. [ordered list markers], and a
  3715. [bullet list](@) if its constituent list
  3716. items begin with [bullet list markers].
  3717. The [start number](@)
  3718. of an [ordered list] is determined by the list number of
  3719. its initial list item. The numbers of subsequent list items are
  3720. disregarded.
  3721. A list is [loose](@) if any of its constituent
  3722. list items are separated by blank lines, or if any of its constituent
  3723. list items directly contain two block-level elements with a blank line
  3724. between them. Otherwise a list is [tight](@).
  3725. (The difference in HTML output is that paragraphs in a loose list are
  3726. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3727. Changing the bullet or ordered list delimiter starts a new list:
  3728. ```````````````````````````````` example
  3729. - foo
  3730. - bar
  3731. + baz
  3732. .
  3733. <ul>
  3734. <li>foo</li>
  3735. <li>bar</li>
  3736. </ul>
  3737. <ul>
  3738. <li>baz</li>
  3739. </ul>
  3740. ````````````````````````````````
  3741. ```````````````````````````````` example
  3742. 1. foo
  3743. 2. bar
  3744. 3) baz
  3745. .
  3746. <ol>
  3747. <li>foo</li>
  3748. <li>bar</li>
  3749. </ol>
  3750. <ol start="3">
  3751. <li>baz</li>
  3752. </ol>
  3753. ````````````````````````````````
  3754. In CommonMark, a list can interrupt a paragraph. That is,
  3755. no blank line is needed to separate a paragraph from a following
  3756. list:
  3757. ```````````````````````````````` example
  3758. Foo
  3759. - bar
  3760. - baz
  3761. .
  3762. <p>Foo</p>
  3763. <ul>
  3764. <li>bar</li>
  3765. <li>baz</li>
  3766. </ul>
  3767. ````````````````````````````````
  3768. `Markdown.pl` does not allow this, through fear of triggering a list
  3769. via a numeral in a hard-wrapped line:
  3770. ``` markdown
  3771. The number of windows in my house is
  3772. 14. The number of doors is 6.
  3773. ```
  3774. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3775. interrupt a paragraph, even though the same considerations might
  3776. apply.
  3777. In CommonMark, we do allow lists to interrupt paragraphs, for
  3778. two reasons. First, it is natural and not uncommon for people
  3779. to start lists without blank lines:
  3780. ``` markdown
  3781. I need to buy
  3782. - new shoes
  3783. - a coat
  3784. - a plane ticket
  3785. ```
  3786. Second, we are attracted to a
  3787. > [principle of uniformity](@):
  3788. > if a chunk of text has a certain
  3789. > meaning, it will continue to have the same meaning when put into a
  3790. > container block (such as a list item or blockquote).
  3791. (Indeed, the spec for [list items] and [block quotes] presupposes
  3792. this principle.) This principle implies that if
  3793. ``` markdown
  3794. * I need to buy
  3795. - new shoes
  3796. - a coat
  3797. - a plane ticket
  3798. ```
  3799. is a list item containing a paragraph followed by a nested sublist,
  3800. as all Markdown implementations agree it is (though the paragraph
  3801. may be rendered without `<p>` tags, since the list is "tight"),
  3802. then
  3803. ``` markdown
  3804. I need to buy
  3805. - new shoes
  3806. - a coat
  3807. - a plane ticket
  3808. ```
  3809. by itself should be a paragraph followed by a nested sublist.
  3810. Since it is well established Markdown practice to allow lists to
  3811. interrupt paragraphs inside list items, the [principle of
  3812. uniformity] requires us to allow this outside list items as
  3813. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3814. takes a different approach, requiring blank lines before lists
  3815. even inside other list items.)
  3816. In order to solve of unwanted lists in paragraphs with
  3817. hard-wrapped numerals, we allow only lists starting with `1` to
  3818. interrupt paragraphs. Thus,
  3819. ```````````````````````````````` example
  3820. The number of windows in my house is
  3821. 14. The number of doors is 6.
  3822. .
  3823. <p>The number of windows in my house is
  3824. 14. The number of doors is 6.</p>
  3825. ````````````````````````````````
  3826. We may still get an unintended result in cases like
  3827. ```````````````````````````````` example
  3828. The number of windows in my house is
  3829. 1. The number of doors is 6.
  3830. .
  3831. <p>The number of windows in my house is</p>
  3832. <ol>
  3833. <li>The number of doors is 6.</li>
  3834. </ol>
  3835. ````````````````````````````````
  3836. but this rule should prevent most spurious list captures.
  3837. There can be any number of blank lines between items:
  3838. ```````````````````````````````` example
  3839. - foo
  3840. - bar
  3841. - baz
  3842. .
  3843. <ul>
  3844. <li>
  3845. <p>foo</p>
  3846. </li>
  3847. <li>
  3848. <p>bar</p>
  3849. </li>
  3850. <li>
  3851. <p>baz</p>
  3852. </li>
  3853. </ul>
  3854. ````````````````````````````````
  3855. ```````````````````````````````` example
  3856. - foo
  3857. - bar
  3858. - baz
  3859. bim
  3860. .
  3861. <ul>
  3862. <li>foo
  3863. <ul>
  3864. <li>bar
  3865. <ul>
  3866. <li>
  3867. <p>baz</p>
  3868. <p>bim</p>
  3869. </li>
  3870. </ul>
  3871. </li>
  3872. </ul>
  3873. </li>
  3874. </ul>
  3875. ````````````````````````````````
  3876. To separate consecutive lists of the same type, or to separate a
  3877. list from an indented code block that would otherwise be parsed
  3878. as a subparagraph of the final list item, you can insert a blank HTML
  3879. comment:
  3880. ```````````````````````````````` example
  3881. - foo
  3882. - bar
  3883. <!-- -->
  3884. - baz
  3885. - bim
  3886. .
  3887. <ul>
  3888. <li>foo</li>
  3889. <li>bar</li>
  3890. </ul>
  3891. <!-- -->
  3892. <ul>
  3893. <li>baz</li>
  3894. <li>bim</li>
  3895. </ul>
  3896. ````````````````````````````````
  3897. ```````````````````````````````` example
  3898. - foo
  3899. notcode
  3900. - foo
  3901. <!-- -->
  3902. code
  3903. .
  3904. <ul>
  3905. <li>
  3906. <p>foo</p>
  3907. <p>notcode</p>
  3908. </li>
  3909. <li>
  3910. <p>foo</p>
  3911. </li>
  3912. </ul>
  3913. <!-- -->
  3914. <pre><code>code
  3915. </code></pre>
  3916. ````````````````````````````````
  3917. List items need not be indented to the same level. The following
  3918. list items will be treated as items at the same list level,
  3919. since none is indented enough to belong to the previous list
  3920. item:
  3921. ```````````````````````````````` example
  3922. - a
  3923. - b
  3924. - c
  3925. - d
  3926. - e
  3927. - f
  3928. - g
  3929. .
  3930. <ul>
  3931. <li>a</li>
  3932. <li>b</li>
  3933. <li>c</li>
  3934. <li>d</li>
  3935. <li>e</li>
  3936. <li>f</li>
  3937. <li>g</li>
  3938. </ul>
  3939. ````````````````````````````````
  3940. ```````````````````````````````` example
  3941. 1. a
  3942. 2. b
  3943. 3. c
  3944. .
  3945. <ol>
  3946. <li>
  3947. <p>a</p>
  3948. </li>
  3949. <li>
  3950. <p>b</p>
  3951. </li>
  3952. <li>
  3953. <p>c</p>
  3954. </li>
  3955. </ol>
  3956. ````````````````````````````````
  3957. Note, however, that list items may not be indented more than
  3958. three spaces. Here `- e` is treated as a paragraph continuation
  3959. line, because it is indented more than three spaces:
  3960. ```````````````````````````````` example
  3961. - a
  3962. - b
  3963. - c
  3964. - d
  3965. - e
  3966. .
  3967. <ul>
  3968. <li>a</li>
  3969. <li>b</li>
  3970. <li>c</li>
  3971. <li>d
  3972. - e</li>
  3973. </ul>
  3974. ````````````````````````````````
  3975. And here, `3. c` is treated as in indented code block,
  3976. because it is indented four spaces and preceded by a
  3977. blank line.
  3978. ```````````````````````````````` example
  3979. 1. a
  3980. 2. b
  3981. 3. c
  3982. .
  3983. <ol>
  3984. <li>
  3985. <p>a</p>
  3986. </li>
  3987. <li>
  3988. <p>b</p>
  3989. </li>
  3990. </ol>
  3991. <pre><code>3. c
  3992. </code></pre>
  3993. ````````````````````````````````
  3994. This is a loose list, because there is a blank line between
  3995. two of the list items:
  3996. ```````````````````````````````` example
  3997. - a
  3998. - b
  3999. - c
  4000. .
  4001. <ul>
  4002. <li>
  4003. <p>a</p>
  4004. </li>
  4005. <li>
  4006. <p>b</p>
  4007. </li>
  4008. <li>
  4009. <p>c</p>
  4010. </li>
  4011. </ul>
  4012. ````````````````````````````````
  4013. So is this, with a empty second item:
  4014. ```````````````````````````````` example
  4015. * a
  4016. *
  4017. * c
  4018. .
  4019. <ul>
  4020. <li>
  4021. <p>a</p>
  4022. </li>
  4023. <li></li>
  4024. <li>
  4025. <p>c</p>
  4026. </li>
  4027. </ul>
  4028. ````````````````````````````````
  4029. These are loose lists, even though there is no space between the items,
  4030. because one of the items directly contains two block-level elements
  4031. with a blank line between them:
  4032. ```````````````````````````````` example
  4033. - a
  4034. - b
  4035. c
  4036. - d
  4037. .
  4038. <ul>
  4039. <li>
  4040. <p>a</p>
  4041. </li>
  4042. <li>
  4043. <p>b</p>
  4044. <p>c</p>
  4045. </li>
  4046. <li>
  4047. <p>d</p>
  4048. </li>
  4049. </ul>
  4050. ````````````````````````````````
  4051. ```````````````````````````````` example
  4052. - a
  4053. - b
  4054. [ref]: /url
  4055. - d
  4056. .
  4057. <ul>
  4058. <li>
  4059. <p>a</p>
  4060. </li>
  4061. <li>
  4062. <p>b</p>
  4063. </li>
  4064. <li>
  4065. <p>d</p>
  4066. </li>
  4067. </ul>
  4068. ````````````````````````````````
  4069. This is a tight list, because the blank lines are in a code block:
  4070. ```````````````````````````````` example
  4071. - a
  4072. - ```
  4073. b
  4074. ```
  4075. - c
  4076. .
  4077. <ul>
  4078. <li>a</li>
  4079. <li>
  4080. <pre><code>b
  4081. </code></pre>
  4082. </li>
  4083. <li>c</li>
  4084. </ul>
  4085. ````````````````````````````````
  4086. This is a tight list, because the blank line is between two
  4087. paragraphs of a sublist. So the sublist is loose while
  4088. the outer list is tight:
  4089. ```````````````````````````````` example
  4090. - a
  4091. - b
  4092. c
  4093. - d
  4094. .
  4095. <ul>
  4096. <li>a
  4097. <ul>
  4098. <li>
  4099. <p>b</p>
  4100. <p>c</p>
  4101. </li>
  4102. </ul>
  4103. </li>
  4104. <li>d</li>
  4105. </ul>
  4106. ````````````````````````````````
  4107. This is a tight list, because the blank line is inside the
  4108. block quote:
  4109. ```````````````````````````````` example
  4110. * a
  4111. > b
  4112. >
  4113. * c
  4114. .
  4115. <ul>
  4116. <li>a
  4117. <blockquote>
  4118. <p>b</p>
  4119. </blockquote>
  4120. </li>
  4121. <li>c</li>
  4122. </ul>
  4123. ````````````````````````````````
  4124. This list is tight, because the consecutive block elements
  4125. are not separated by blank lines:
  4126. ```````````````````````````````` example
  4127. - a
  4128. > b
  4129. ```
  4130. c
  4131. ```
  4132. - d
  4133. .
  4134. <ul>
  4135. <li>a
  4136. <blockquote>
  4137. <p>b</p>
  4138. </blockquote>
  4139. <pre><code>c
  4140. </code></pre>
  4141. </li>
  4142. <li>d</li>
  4143. </ul>
  4144. ````````````````````````````````
  4145. A single-paragraph list is tight:
  4146. ```````````````````````````````` example
  4147. - a
  4148. .
  4149. <ul>
  4150. <li>a</li>
  4151. </ul>
  4152. ````````````````````````````````
  4153. ```````````````````````````````` example
  4154. - a
  4155. - b
  4156. .
  4157. <ul>
  4158. <li>a
  4159. <ul>
  4160. <li>b</li>
  4161. </ul>
  4162. </li>
  4163. </ul>
  4164. ````````````````````````````````
  4165. This list is loose, because of the blank line between the
  4166. two block elements in the list item:
  4167. ```````````````````````````````` example
  4168. 1. ```
  4169. foo
  4170. ```
  4171. bar
  4172. .
  4173. <ol>
  4174. <li>
  4175. <pre><code>foo
  4176. </code></pre>
  4177. <p>bar</p>
  4178. </li>
  4179. </ol>
  4180. ````````````````````````````````
  4181. Here the outer list is loose, the inner list tight:
  4182. ```````````````````````````````` example
  4183. * foo
  4184. * bar
  4185. baz
  4186. .
  4187. <ul>
  4188. <li>
  4189. <p>foo</p>
  4190. <ul>
  4191. <li>bar</li>
  4192. </ul>
  4193. <p>baz</p>
  4194. </li>
  4195. </ul>
  4196. ````````````````````````````````
  4197. ```````````````````````````````` example
  4198. - a
  4199. - b
  4200. - c
  4201. - d
  4202. - e
  4203. - f
  4204. .
  4205. <ul>
  4206. <li>
  4207. <p>a</p>
  4208. <ul>
  4209. <li>b</li>
  4210. <li>c</li>
  4211. </ul>
  4212. </li>
  4213. <li>
  4214. <p>d</p>
  4215. <ul>
  4216. <li>e</li>
  4217. <li>f</li>
  4218. </ul>
  4219. </li>
  4220. </ul>
  4221. ````````````````````````````````
  4222. # Inlines
  4223. Inlines are parsed sequentially from the beginning of the character
  4224. stream to the end (left to right, in left-to-right languages).
  4225. Thus, for example, in
  4226. ```````````````````````````````` example
  4227. `hi`lo`
  4228. .
  4229. <p><code>hi</code>lo`</p>
  4230. ````````````````````````````````
  4231. `hi` is parsed as code, leaving the backtick at the end as a literal
  4232. backtick.
  4233. ## Backslash escapes
  4234. Any ASCII punctuation character may be backslash-escaped:
  4235. ```````````````````````````````` example
  4236. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4237. .
  4238. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4239. ````````````````````````````````
  4240. Backslashes before other characters are treated as literal
  4241. backslashes:
  4242. ```````````````````````````````` example
  4243. \→\A\a\ \3\φ\«
  4244. .
  4245. <p>\→\A\a\ \3\φ\«</p>
  4246. ````````````````````````````````
  4247. Escaped characters are treated as regular characters and do
  4248. not have their usual Markdown meanings:
  4249. ```````````````````````````````` example
  4250. \*not emphasized*
  4251. \<br/> not a tag
  4252. \[not a link](/foo)
  4253. \`not code`
  4254. 1\. not a list
  4255. \* not a list
  4256. \# not a heading
  4257. \[foo]: /url "not a reference"
  4258. .
  4259. <p>*not emphasized*
  4260. &lt;br/&gt; not a tag
  4261. [not a link](/foo)
  4262. `not code`
  4263. 1. not a list
  4264. * not a list
  4265. # not a heading
  4266. [foo]: /url &quot;not a reference&quot;</p>
  4267. ````````````````````````````````
  4268. If a backslash is itself escaped, the following character is not:
  4269. ```````````````````````````````` example
  4270. \\*emphasis*
  4271. .
  4272. <p>\<em>emphasis</em></p>
  4273. ````````````````````````````````
  4274. A backslash at the end of the line is a [hard line break]:
  4275. ```````````````````````````````` example
  4276. foo\
  4277. bar
  4278. .
  4279. <p>foo<br />
  4280. bar</p>
  4281. ````````````````````````````````
  4282. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4283. raw HTML:
  4284. ```````````````````````````````` example
  4285. `` \[\` ``
  4286. .
  4287. <p><code>\[\`</code></p>
  4288. ````````````````````````````````
  4289. ```````````````````````````````` example
  4290. \[\]
  4291. .
  4292. <pre><code>\[\]
  4293. </code></pre>
  4294. ````````````````````````````````
  4295. ```````````````````````````````` example
  4296. ~~~
  4297. \[\]
  4298. ~~~
  4299. .
  4300. <pre><code>\[\]
  4301. </code></pre>
  4302. ````````````````````````````````
  4303. ```````````````````````````````` example
  4304. <http://example.com?find=\*>
  4305. .
  4306. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4307. ````````````````````````````````
  4308. ```````````````````````````````` example
  4309. <a href="/bar\/)">
  4310. .
  4311. <a href="/bar\/)">
  4312. ````````````````````````````````
  4313. But they work in all other contexts, including URLs and link titles,
  4314. link references, and [info strings] in [fenced code blocks]:
  4315. ```````````````````````````````` example
  4316. [foo](/bar\* "ti\*tle")
  4317. .
  4318. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4319. ````````````````````````````````
  4320. ```````````````````````````````` example
  4321. [foo]
  4322. [foo]: /bar\* "ti\*tle"
  4323. .
  4324. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4325. ````````````````````````````````
  4326. ```````````````````````````````` example
  4327. ``` foo\+bar
  4328. foo
  4329. ```
  4330. .
  4331. <pre><code class="language-foo+bar">foo
  4332. </code></pre>
  4333. ````````````````````````````````
  4334. ## Entity and numeric character references
  4335. All valid HTML entity references and numeric character
  4336. references, except those occurring in code blocks and code spans,
  4337. are recognized as such and treated as equivalent to the
  4338. corresponding Unicode characters. Conforming CommonMark parsers
  4339. need not store information about whether a particular character
  4340. was represented in the source using a Unicode character or
  4341. an entity reference.
  4342. [Entity references](@) consist of `&` + any of the valid
  4343. HTML5 entity names + `;`. The
  4344. document <https://html.spec.whatwg.org/multipage/entities.json>
  4345. is used as an authoritative source for the valid entity
  4346. references and their corresponding code points.
  4347. ```````````````````````````````` example
  4348. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4349. &frac34; &HilbertSpace; &DifferentialD;
  4350. &ClockwiseContourIntegral; &ngE;
  4351. .
  4352. <p>  &amp; © Æ Ď
  4353. ¾ ℋ ⅆ
  4354. ∲ ≧̸</p>
  4355. ````````````````````````````````
  4356. [Decimal numeric character
  4357. references](@)
  4358. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4359. numeric character reference is parsed as the corresponding
  4360. Unicode character. Invalid Unicode code points will be replaced by
  4361. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4362. the code point `U+0000` will also be replaced by `U+FFFD`.
  4363. ```````````````````````````````` example
  4364. &#35; &#1234; &#992; &#0;
  4365. .
  4366. <p># Ӓ Ϡ �</p>
  4367. ````````````````````````````````
  4368. [Hexadecimal numeric character
  4369. references](@) consist of `&#` +
  4370. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4371. They too are parsed as the corresponding Unicode character (this
  4372. time specified with a hexadecimal numeral instead of decimal).
  4373. ```````````````````````````````` example
  4374. &#X22; &#XD06; &#xcab;
  4375. .
  4376. <p>&quot; ആ ಫ</p>
  4377. ````````````````````````````````
  4378. Here are some nonentities:
  4379. ```````````````````````````````` example
  4380. &nbsp &x; &#; &#x;
  4381. &#987654321;
  4382. &#abcdef0;
  4383. &ThisIsNotDefined; &hi?;
  4384. .
  4385. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4386. &amp;#987654321;
  4387. &amp;#abcdef0;
  4388. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4389. ````````````````````````````````
  4390. Although HTML5 does accept some entity references
  4391. without a trailing semicolon (such as `&copy`), these are not
  4392. recognized here, because it makes the grammar too ambiguous:
  4393. ```````````````````````````````` example
  4394. &copy
  4395. .
  4396. <p>&amp;copy</p>
  4397. ````````````````````````````````
  4398. Strings that are not on the list of HTML5 named entities are not
  4399. recognized as entity references either:
  4400. ```````````````````````````````` example
  4401. &MadeUpEntity;
  4402. .
  4403. <p>&amp;MadeUpEntity;</p>
  4404. ````````````````````````````````
  4405. Entity and numeric character references are recognized in any
  4406. context besides code spans or code blocks, including
  4407. URLs, [link titles], and [fenced code block][] [info strings]:
  4408. ```````````````````````````````` example
  4409. <a href="&ouml;&ouml;.html">
  4410. .
  4411. <a href="&ouml;&ouml;.html">
  4412. ````````````````````````````````
  4413. ```````````````````````````````` example
  4414. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4415. .
  4416. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4417. ````````````````````````````````
  4418. ```````````````````````````````` example
  4419. [foo]
  4420. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4421. .
  4422. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4423. ````````````````````````````````
  4424. ```````````````````````````````` example
  4425. ``` f&ouml;&ouml;
  4426. foo
  4427. ```
  4428. .
  4429. <pre><code class="language-föö">foo
  4430. </code></pre>
  4431. ````````````````````````````````
  4432. Entity and numeric character references are treated as literal
  4433. text in code spans and code blocks:
  4434. ```````````````````````````````` example
  4435. `f&ouml;&ouml;`
  4436. .
  4437. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4438. ````````````````````````````````
  4439. ```````````````````````````````` example
  4440. f&ouml;f&ouml;
  4441. .
  4442. <pre><code>f&amp;ouml;f&amp;ouml;
  4443. </code></pre>
  4444. ````````````````````````````````
  4445. ## Code spans
  4446. A [backtick string](@)
  4447. is a string of one or more backtick characters (`` ` ``) that is neither
  4448. preceded nor followed by a backtick.
  4449. A [code span](@) begins with a backtick string and ends with
  4450. a backtick string of equal length. The contents of the code span are
  4451. the characters between the two backtick strings, normalized in the
  4452. following ways:
  4453. - First, [line endings] are converted to [spaces].
  4454. - If the resulting string both begins *and* ends with a [space]
  4455. character, a single [space] character is removed from the
  4456. front and back. This allows you to include code that begins
  4457. or ends with backtick characters, which must be separated by
  4458. whitespace from the opening or closing backtick strings.
  4459. This is a simple code span:
  4460. ```````````````````````````````` example
  4461. `foo`
  4462. .
  4463. <p><code>foo</code></p>
  4464. ````````````````````````````````
  4465. Here two backticks are used, because the code contains a backtick.
  4466. This example also illustrates stripping of a single leading and
  4467. trailing space:
  4468. ```````````````````````````````` example
  4469. `` foo ` bar ``
  4470. .
  4471. <p><code>foo ` bar</code></p>
  4472. ````````````````````````````````
  4473. This example shows the motivation for stripping leading and trailing
  4474. spaces:
  4475. ```````````````````````````````` example
  4476. ` `` `
  4477. .
  4478. <p><code>``</code></p>
  4479. ````````````````````````````````
  4480. Note that only *one* space is stripped:
  4481. ```````````````````````````````` example
  4482. ` `` `
  4483. .
  4484. <p><code> `` </code></p>
  4485. ````````````````````````````````
  4486. The stripping only happens if the space is on both
  4487. sides of the string:
  4488. ```````````````````````````````` example
  4489. ` a`
  4490. .
  4491. <p><code> a</code></p>
  4492. ````````````````````````````````
  4493. Only [spaces], and not [unicode whitespace] in general, are
  4494. stripped in this way:
  4495. ```````````````````````````````` example
  4496. ` b `
  4497. .
  4498. <p><code> b </code></p>
  4499. ````````````````````````````````
  4500. [Line endings] are treated like spaces:
  4501. ```````````````````````````````` example
  4502. ``
  4503. foo
  4504. bar
  4505. baz
  4506. ``
  4507. .
  4508. <p><code>foo bar baz</code></p>
  4509. ````````````````````````````````
  4510. ```````````````````````````````` example
  4511. ``
  4512. foo
  4513. ``
  4514. .
  4515. <p><code>foo </code></p>
  4516. ````````````````````````````````
  4517. Interior spaces are not collapsed:
  4518. ```````````````````````````````` example
  4519. `foo bar
  4520. baz`
  4521. .
  4522. <p><code>foo bar baz</code></p>
  4523. ````````````````````````````````
  4524. Note that browsers will typically collapse consecutive spaces
  4525. when rendering `<code>` elements, so it is recommended that
  4526. the following CSS be used:
  4527. code{white-space: pre-wrap;}
  4528. Note that backslash escapes do not work in code spans. All backslashes
  4529. are treated literally:
  4530. ```````````````````````````````` example
  4531. `foo\`bar`
  4532. .
  4533. <p><code>foo\</code>bar`</p>
  4534. ````````````````````````````````
  4535. Backslash escapes are never needed, because one can always choose a
  4536. string of *n* backtick characters as delimiters, where the code does
  4537. not contain any strings of exactly *n* backtick characters.
  4538. ```````````````````````````````` example
  4539. ``foo`bar``
  4540. .
  4541. <p><code>foo`bar</code></p>
  4542. ````````````````````````````````
  4543. ```````````````````````````````` example
  4544. ` foo `` bar `
  4545. .
  4546. <p><code>foo `` bar</code></p>
  4547. ````````````````````````````````
  4548. Code span backticks have higher precedence than any other inline
  4549. constructs except HTML tags and autolinks. Thus, for example, this is
  4550. not parsed as emphasized text, since the second `*` is part of a code
  4551. span:
  4552. ```````````````````````````````` example
  4553. *foo`*`
  4554. .
  4555. <p>*foo<code>*</code></p>
  4556. ````````````````````````````````
  4557. And this is not parsed as a link:
  4558. ```````````````````````````````` example
  4559. [not a `link](/foo`)
  4560. .
  4561. <p>[not a <code>link](/foo</code>)</p>
  4562. ````````````````````````````````
  4563. Code spans, HTML tags, and autolinks have the same precedence.
  4564. Thus, this is code:
  4565. ```````````````````````````````` example
  4566. `<a href="`">`
  4567. .
  4568. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4569. ````````````````````````````````
  4570. But this is an HTML tag:
  4571. ```````````````````````````````` example
  4572. <a href="`">`
  4573. .
  4574. <p><a href="`">`</p>
  4575. ````````````````````````````````
  4576. And this is code:
  4577. ```````````````````````````````` example
  4578. `<http://foo.bar.`baz>`
  4579. .
  4580. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4581. ````````````````````````````````
  4582. But this is an autolink:
  4583. ```````````````````````````````` example
  4584. <http://foo.bar.`baz>`
  4585. .
  4586. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4587. ````````````````````````````````
  4588. When a backtick string is not closed by a matching backtick string,
  4589. we just have literal backticks:
  4590. ```````````````````````````````` example
  4591. ```foo``
  4592. .
  4593. <p>```foo``</p>
  4594. ````````````````````````````````
  4595. ```````````````````````````````` example
  4596. `foo
  4597. .
  4598. <p>`foo</p>
  4599. ````````````````````````````````
  4600. The following case also illustrates the need for opening and
  4601. closing backtick strings to be equal in length:
  4602. ```````````````````````````````` example
  4603. `foo``bar``
  4604. .
  4605. <p>`foo<code>bar</code></p>
  4606. ````````````````````````````````
  4607. ## Emphasis and strong emphasis
  4608. John Gruber's original [Markdown syntax
  4609. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4610. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4611. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4612. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4613. > tag.
  4614. This is enough for most users, but these rules leave much undecided,
  4615. especially when it comes to nested emphasis. The original
  4616. `Markdown.pl` test suite makes it clear that triple `***` and
  4617. `___` delimiters can be used for strong emphasis, and most
  4618. implementations have also allowed the following patterns:
  4619. ``` markdown
  4620. ***strong emph***
  4621. ***strong** in emph*
  4622. ***emph* in strong**
  4623. **in strong *emph***
  4624. *in emph **strong***
  4625. ```
  4626. The following patterns are less widely supported, but the intent
  4627. is clear and they are useful (especially in contexts like bibliography
  4628. entries):
  4629. ``` markdown
  4630. *emph *with emph* in it*
  4631. **strong **with strong** in it**
  4632. ```
  4633. Many implementations have also restricted intraword emphasis to
  4634. the `*` forms, to avoid unwanted emphasis in words containing
  4635. internal underscores. (It is best practice to put these in code
  4636. spans, but users often do not.)
  4637. ``` markdown
  4638. internal emphasis: foo*bar*baz
  4639. no emphasis: foo_bar_baz
  4640. ```
  4641. The rules given below capture all of these patterns, while allowing
  4642. for efficient parsing strategies that do not backtrack.
  4643. First, some definitions. A [delimiter run](@) is either
  4644. a sequence of one or more `*` characters that is not preceded or
  4645. followed by a non-backslash-escaped `*` character, or a sequence
  4646. of one or more `_` characters that is not preceded or followed by
  4647. a non-backslash-escaped `_` character.
  4648. A [left-flanking delimiter run](@) is
  4649. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4650. and either (2a) not followed by a [punctuation character], or
  4651. (2b) followed by a [punctuation character] and
  4652. preceded by [Unicode whitespace] or a [punctuation character].
  4653. For purposes of this definition, the beginning and the end of
  4654. the line count as Unicode whitespace.
  4655. A [right-flanking delimiter run](@) is
  4656. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4657. and either (2a) not preceded by a [punctuation character], or
  4658. (2b) preceded by a [punctuation character] and
  4659. followed by [Unicode whitespace] or a [punctuation character].
  4660. For purposes of this definition, the beginning and the end of
  4661. the line count as Unicode whitespace.
  4662. Here are some examples of delimiter runs.
  4663. - left-flanking but not right-flanking:
  4664. ```
  4665. ***abc
  4666. _abc
  4667. **"abc"
  4668. _"abc"
  4669. ```
  4670. - right-flanking but not left-flanking:
  4671. ```
  4672. abc***
  4673. abc_
  4674. "abc"**
  4675. "abc"_
  4676. ```
  4677. - Both left and right-flanking:
  4678. ```
  4679. abc***def
  4680. "abc"_"def"
  4681. ```
  4682. - Neither left nor right-flanking:
  4683. ```
  4684. abc *** def
  4685. a _ b
  4686. ```
  4687. (The idea of distinguishing left-flanking and right-flanking
  4688. delimiter runs based on the character before and the character
  4689. after comes from Roopesh Chander's
  4690. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4691. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4692. run," and its rules for distinguishing left- and right-flanking runs
  4693. are a bit more complex than the ones given here.)
  4694. The following rules define emphasis and strong emphasis:
  4695. 1. A single `*` character [can open emphasis](@)
  4696. iff (if and only if) it is part of a [left-flanking delimiter run].
  4697. 2. A single `_` character [can open emphasis] iff
  4698. it is part of a [left-flanking delimiter run]
  4699. and either (a) not part of a [right-flanking delimiter run]
  4700. or (b) part of a [right-flanking delimiter run]
  4701. preceded by punctuation.
  4702. 3. A single `*` character [can close emphasis](@)
  4703. iff it is part of a [right-flanking delimiter run].
  4704. 4. A single `_` character [can close emphasis] iff
  4705. it is part of a [right-flanking delimiter run]
  4706. and either (a) not part of a [left-flanking delimiter run]
  4707. or (b) part of a [left-flanking delimiter run]
  4708. followed by punctuation.
  4709. 5. A double `**` [can open strong emphasis](@)
  4710. iff it is part of a [left-flanking delimiter run].
  4711. 6. A double `__` [can open strong emphasis] iff
  4712. it is part of a [left-flanking delimiter run]
  4713. and either (a) not part of a [right-flanking delimiter run]
  4714. or (b) part of a [right-flanking delimiter run]
  4715. preceded by punctuation.
  4716. 7. A double `**` [can close strong emphasis](@)
  4717. iff it is part of a [right-flanking delimiter run].
  4718. 8. A double `__` [can close strong emphasis] iff
  4719. it is part of a [right-flanking delimiter run]
  4720. and either (a) not part of a [left-flanking delimiter run]
  4721. or (b) part of a [left-flanking delimiter run]
  4722. followed by punctuation.
  4723. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4724. with a delimiter that [can close emphasis], and that uses the same
  4725. character (`_` or `*`) as the opening delimiter. The
  4726. opening and closing delimiters must belong to separate
  4727. [delimiter runs]. If one of the delimiters can both
  4728. open and close emphasis, then the sum of the lengths of the
  4729. delimiter runs containing the opening and closing delimiters
  4730. must not be a multiple of 3 unless both lengths are
  4731. multiples of 3.
  4732. 10. Strong emphasis begins with a delimiter that
  4733. [can open strong emphasis] and ends with a delimiter that
  4734. [can close strong emphasis], and that uses the same character
  4735. (`_` or `*`) as the opening delimiter. The
  4736. opening and closing delimiters must belong to separate
  4737. [delimiter runs]. If one of the delimiters can both open
  4738. and close strong emphasis, then the sum of the lengths of
  4739. the delimiter runs containing the opening and closing
  4740. delimiters must not be a multiple of 3 unless both lengths
  4741. are multiples of 3.
  4742. 11. A literal `*` character cannot occur at the beginning or end of
  4743. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4744. is backslash-escaped.
  4745. 12. A literal `_` character cannot occur at the beginning or end of
  4746. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4747. is backslash-escaped.
  4748. Where rules 1--12 above are compatible with multiple parsings,
  4749. the following principles resolve ambiguity:
  4750. 13. The number of nestings should be minimized. Thus, for example,
  4751. an interpretation `<strong>...</strong>` is always preferred to
  4752. `<em><em>...</em></em>`.
  4753. 14. An interpretation `<em><strong>...</strong></em>` is always
  4754. preferred to `<strong><em>...</em></strong>`.
  4755. 15. When two potential emphasis or strong emphasis spans overlap,
  4756. so that the second begins before the first ends and ends after
  4757. the first ends, the first takes precedence. Thus, for example,
  4758. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4759. than `*foo <em>bar* baz</em>`.
  4760. 16. When there are two potential emphasis or strong emphasis spans
  4761. with the same closing delimiter, the shorter one (the one that
  4762. opens later) takes precedence. Thus, for example,
  4763. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4764. rather than `<strong>foo **bar baz</strong>`.
  4765. 17. Inline code spans, links, images, and HTML tags group more tightly
  4766. than emphasis. So, when there is a choice between an interpretation
  4767. that contains one of these elements and one that does not, the
  4768. former always wins. Thus, for example, `*[foo*](bar)` is
  4769. parsed as `*<a href="bar">foo*</a>` rather than as
  4770. `<em>[foo</em>](bar)`.
  4771. These rules can be illustrated through a series of examples.
  4772. Rule 1:
  4773. ```````````````````````````````` example
  4774. *foo bar*
  4775. .
  4776. <p><em>foo bar</em></p>
  4777. ````````````````````````````````
  4778. This is not emphasis, because the opening `*` is followed by
  4779. whitespace, and hence not part of a [left-flanking delimiter run]:
  4780. ```````````````````````````````` example
  4781. a * foo bar*
  4782. .
  4783. <p>a * foo bar*</p>
  4784. ````````````````````````````````
  4785. This is not emphasis, because the opening `*` is preceded
  4786. by an alphanumeric and followed by punctuation, and hence
  4787. not part of a [left-flanking delimiter run]:
  4788. ```````````````````````````````` example
  4789. a*"foo"*
  4790. .
  4791. <p>a*&quot;foo&quot;*</p>
  4792. ````````````````````````````````
  4793. Unicode nonbreaking spaces count as whitespace, too:
  4794. ```````````````````````````````` example
  4795. * a *
  4796. .
  4797. <p>* a *</p>
  4798. ````````````````````````````````
  4799. Intraword emphasis with `*` is permitted:
  4800. ```````````````````````````````` example
  4801. foo*bar*
  4802. .
  4803. <p>foo<em>bar</em></p>
  4804. ````````````````````````````````
  4805. ```````````````````````````````` example
  4806. 5*6*78
  4807. .
  4808. <p>5<em>6</em>78</p>
  4809. ````````````````````````````````
  4810. Rule 2:
  4811. ```````````````````````````````` example
  4812. _foo bar_
  4813. .
  4814. <p><em>foo bar</em></p>
  4815. ````````````````````````````````
  4816. This is not emphasis, because the opening `_` is followed by
  4817. whitespace:
  4818. ```````````````````````````````` example
  4819. _ foo bar_
  4820. .
  4821. <p>_ foo bar_</p>
  4822. ````````````````````````````````
  4823. This is not emphasis, because the opening `_` is preceded
  4824. by an alphanumeric and followed by punctuation:
  4825. ```````````````````````````````` example
  4826. a_"foo"_
  4827. .
  4828. <p>a_&quot;foo&quot;_</p>
  4829. ````````````````````````````````
  4830. Emphasis with `_` is not allowed inside words:
  4831. ```````````````````````````````` example
  4832. foo_bar_
  4833. .
  4834. <p>foo_bar_</p>
  4835. ````````````````````````````````
  4836. ```````````````````````````````` example
  4837. 5_6_78
  4838. .
  4839. <p>5_6_78</p>
  4840. ````````````````````````````````
  4841. ```````````````````````````````` example
  4842. пристаням_стремятся_
  4843. .
  4844. <p>пристаням_стремятся_</p>
  4845. ````````````````````````````````
  4846. Here `_` does not generate emphasis, because the first delimiter run
  4847. is right-flanking and the second left-flanking:
  4848. ```````````````````````````````` example
  4849. aa_"bb"_cc
  4850. .
  4851. <p>aa_&quot;bb&quot;_cc</p>
  4852. ````````````````````````````````
  4853. This is emphasis, even though the opening delimiter is
  4854. both left- and right-flanking, because it is preceded by
  4855. punctuation:
  4856. ```````````````````````````````` example
  4857. foo-_(bar)_
  4858. .
  4859. <p>foo-<em>(bar)</em></p>
  4860. ````````````````````````````````
  4861. Rule 3:
  4862. This is not emphasis, because the closing delimiter does
  4863. not match the opening delimiter:
  4864. ```````````````````````````````` example
  4865. _foo*
  4866. .
  4867. <p>_foo*</p>
  4868. ````````````````````````````````
  4869. This is not emphasis, because the closing `*` is preceded by
  4870. whitespace:
  4871. ```````````````````````````````` example
  4872. *foo bar *
  4873. .
  4874. <p>*foo bar *</p>
  4875. ````````````````````````````````
  4876. A newline also counts as whitespace:
  4877. ```````````````````````````````` example
  4878. *foo bar
  4879. *
  4880. .
  4881. <p>*foo bar
  4882. *</p>
  4883. ````````````````````````````````
  4884. This is not emphasis, because the second `*` is
  4885. preceded by punctuation and followed by an alphanumeric
  4886. (hence it is not part of a [right-flanking delimiter run]:
  4887. ```````````````````````````````` example
  4888. *(*foo)
  4889. .
  4890. <p>*(*foo)</p>
  4891. ````````````````````````````````
  4892. The point of this restriction is more easily appreciated
  4893. with this example:
  4894. ```````````````````````````````` example
  4895. *(*foo*)*
  4896. .
  4897. <p><em>(<em>foo</em>)</em></p>
  4898. ````````````````````````````````
  4899. Intraword emphasis with `*` is allowed:
  4900. ```````````````````````````````` example
  4901. *foo*bar
  4902. .
  4903. <p><em>foo</em>bar</p>
  4904. ````````````````````````````````
  4905. Rule 4:
  4906. This is not emphasis, because the closing `_` is preceded by
  4907. whitespace:
  4908. ```````````````````````````````` example
  4909. _foo bar _
  4910. .
  4911. <p>_foo bar _</p>
  4912. ````````````````````````````````
  4913. This is not emphasis, because the second `_` is
  4914. preceded by punctuation and followed by an alphanumeric:
  4915. ```````````````````````````````` example
  4916. _(_foo)
  4917. .
  4918. <p>_(_foo)</p>
  4919. ````````````````````````````````
  4920. This is emphasis within emphasis:
  4921. ```````````````````````````````` example
  4922. _(_foo_)_
  4923. .
  4924. <p><em>(<em>foo</em>)</em></p>
  4925. ````````````````````````````````
  4926. Intraword emphasis is disallowed for `_`:
  4927. ```````````````````````````````` example
  4928. _foo_bar
  4929. .
  4930. <p>_foo_bar</p>
  4931. ````````````````````````````````
  4932. ```````````````````````````````` example
  4933. _пристаням_стремятся
  4934. .
  4935. <p>_пристаням_стремятся</p>
  4936. ````````````````````````````````
  4937. ```````````````````````````````` example
  4938. _foo_bar_baz_
  4939. .
  4940. <p><em>foo_bar_baz</em></p>
  4941. ````````````````````````````````
  4942. This is emphasis, even though the closing delimiter is
  4943. both left- and right-flanking, because it is followed by
  4944. punctuation:
  4945. ```````````````````````````````` example
  4946. _(bar)_.
  4947. .
  4948. <p><em>(bar)</em>.</p>
  4949. ````````````````````````````````
  4950. Rule 5:
  4951. ```````````````````````````````` example
  4952. **foo bar**
  4953. .
  4954. <p><strong>foo bar</strong></p>
  4955. ````````````````````````````````
  4956. This is not strong emphasis, because the opening delimiter is
  4957. followed by whitespace:
  4958. ```````````````````````````````` example
  4959. ** foo bar**
  4960. .
  4961. <p>** foo bar**</p>
  4962. ````````````````````````````````
  4963. This is not strong emphasis, because the opening `**` is preceded
  4964. by an alphanumeric and followed by punctuation, and hence
  4965. not part of a [left-flanking delimiter run]:
  4966. ```````````````````````````````` example
  4967. a**"foo"**
  4968. .
  4969. <p>a**&quot;foo&quot;**</p>
  4970. ````````````````````````````````
  4971. Intraword strong emphasis with `**` is permitted:
  4972. ```````````````````````````````` example
  4973. foo**bar**
  4974. .
  4975. <p>foo<strong>bar</strong></p>
  4976. ````````````````````````````````
  4977. Rule 6:
  4978. ```````````````````````````````` example
  4979. __foo bar__
  4980. .
  4981. <p><strong>foo bar</strong></p>
  4982. ````````````````````````````````
  4983. This is not strong emphasis, because the opening delimiter is
  4984. followed by whitespace:
  4985. ```````````````````````````````` example
  4986. __ foo bar__
  4987. .
  4988. <p>__ foo bar__</p>
  4989. ````````````````````````````````
  4990. A newline counts as whitespace:
  4991. ```````````````````````````````` example
  4992. __
  4993. foo bar__
  4994. .
  4995. <p>__
  4996. foo bar__</p>
  4997. ````````````````````````````````
  4998. This is not strong emphasis, because the opening `__` is preceded
  4999. by an alphanumeric and followed by punctuation:
  5000. ```````````````````````````````` example
  5001. a__"foo"__
  5002. .
  5003. <p>a__&quot;foo&quot;__</p>
  5004. ````````````````````````````````
  5005. Intraword strong emphasis is forbidden with `__`:
  5006. ```````````````````````````````` example
  5007. foo__bar__
  5008. .
  5009. <p>foo__bar__</p>
  5010. ````````````````````````````````
  5011. ```````````````````````````````` example
  5012. 5__6__78
  5013. .
  5014. <p>5__6__78</p>
  5015. ````````````````````````````````
  5016. ```````````````````````````````` example
  5017. пристаням__стремятся__
  5018. .
  5019. <p>пристаням__стремятся__</p>
  5020. ````````````````````````````````
  5021. ```````````````````````````````` example
  5022. __foo, __bar__, baz__
  5023. .
  5024. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5025. ````````````````````````````````
  5026. This is strong emphasis, even though the opening delimiter is
  5027. both left- and right-flanking, because it is preceded by
  5028. punctuation:
  5029. ```````````````````````````````` example
  5030. foo-__(bar)__
  5031. .
  5032. <p>foo-<strong>(bar)</strong></p>
  5033. ````````````````````````````````
  5034. Rule 7:
  5035. This is not strong emphasis, because the closing delimiter is preceded
  5036. by whitespace:
  5037. ```````````````````````````````` example
  5038. **foo bar **
  5039. .
  5040. <p>**foo bar **</p>
  5041. ````````````````````````````````
  5042. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5043. Rule 11.)
  5044. This is not strong emphasis, because the second `**` is
  5045. preceded by punctuation and followed by an alphanumeric:
  5046. ```````````````````````````````` example
  5047. **(**foo)
  5048. .
  5049. <p>**(**foo)</p>
  5050. ````````````````````````````````
  5051. The point of this restriction is more easily appreciated
  5052. with these examples:
  5053. ```````````````````````````````` example
  5054. *(**foo**)*
  5055. .
  5056. <p><em>(<strong>foo</strong>)</em></p>
  5057. ````````````````````````````````
  5058. ```````````````````````````````` example
  5059. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5060. *Asclepias physocarpa*)**
  5061. .
  5062. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5063. <em>Asclepias physocarpa</em>)</strong></p>
  5064. ````````````````````````````````
  5065. ```````````````````````````````` example
  5066. **foo "*bar*" foo**
  5067. .
  5068. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5069. ````````````````````````````````
  5070. Intraword emphasis:
  5071. ```````````````````````````````` example
  5072. **foo**bar
  5073. .
  5074. <p><strong>foo</strong>bar</p>
  5075. ````````````````````````````````
  5076. Rule 8:
  5077. This is not strong emphasis, because the closing delimiter is
  5078. preceded by whitespace:
  5079. ```````````````````````````````` example
  5080. __foo bar __
  5081. .
  5082. <p>__foo bar __</p>
  5083. ````````````````````````````````
  5084. This is not strong emphasis, because the second `__` is
  5085. preceded by punctuation and followed by an alphanumeric:
  5086. ```````````````````````````````` example
  5087. __(__foo)
  5088. .
  5089. <p>__(__foo)</p>
  5090. ````````````````````````````````
  5091. The point of this restriction is more easily appreciated
  5092. with this example:
  5093. ```````````````````````````````` example
  5094. _(__foo__)_
  5095. .
  5096. <p><em>(<strong>foo</strong>)</em></p>
  5097. ````````````````````````````````
  5098. Intraword strong emphasis is forbidden with `__`:
  5099. ```````````````````````````````` example
  5100. __foo__bar
  5101. .
  5102. <p>__foo__bar</p>
  5103. ````````````````````````````````
  5104. ```````````````````````````````` example
  5105. __пристаням__стремятся
  5106. .
  5107. <p>__пристаням__стремятся</p>
  5108. ````````````````````````````````
  5109. ```````````````````````````````` example
  5110. __foo__bar__baz__
  5111. .
  5112. <p><strong>foo__bar__baz</strong></p>
  5113. ````````````````````````````````
  5114. This is strong emphasis, even though the closing delimiter is
  5115. both left- and right-flanking, because it is followed by
  5116. punctuation:
  5117. ```````````````````````````````` example
  5118. __(bar)__.
  5119. .
  5120. <p><strong>(bar)</strong>.</p>
  5121. ````````````````````````````````
  5122. Rule 9:
  5123. Any nonempty sequence of inline elements can be the contents of an
  5124. emphasized span.
  5125. ```````````````````````````````` example
  5126. *foo [bar](/url)*
  5127. .
  5128. <p><em>foo <a href="/url">bar</a></em></p>
  5129. ````````````````````````````````
  5130. ```````````````````````````````` example
  5131. *foo
  5132. bar*
  5133. .
  5134. <p><em>foo
  5135. bar</em></p>
  5136. ````````````````````````````````
  5137. In particular, emphasis and strong emphasis can be nested
  5138. inside emphasis:
  5139. ```````````````````````````````` example
  5140. _foo __bar__ baz_
  5141. .
  5142. <p><em>foo <strong>bar</strong> baz</em></p>
  5143. ````````````````````````````````
  5144. ```````````````````````````````` example
  5145. _foo _bar_ baz_
  5146. .
  5147. <p><em>foo <em>bar</em> baz</em></p>
  5148. ````````````````````````````````
  5149. ```````````````````````````````` example
  5150. __foo_ bar_
  5151. .
  5152. <p><em><em>foo</em> bar</em></p>
  5153. ````````````````````````````````
  5154. ```````````````````````````````` example
  5155. *foo *bar**
  5156. .
  5157. <p><em>foo <em>bar</em></em></p>
  5158. ````````````````````````````````
  5159. ```````````````````````````````` example
  5160. *foo **bar** baz*
  5161. .
  5162. <p><em>foo <strong>bar</strong> baz</em></p>
  5163. ````````````````````````````````
  5164. ```````````````````````````````` example
  5165. *foo**bar**baz*
  5166. .
  5167. <p><em>foo<strong>bar</strong>baz</em></p>
  5168. ````````````````````````````````
  5169. Note that in the preceding case, the interpretation
  5170. ``` markdown
  5171. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5172. ```
  5173. is precluded by the condition that a delimiter that
  5174. can both open and close (like the `*` after `foo`)
  5175. cannot form emphasis if the sum of the lengths of
  5176. the delimiter runs containing the opening and
  5177. closing delimiters is a multiple of 3 unless
  5178. both lengths are multiples of 3.
  5179. For the same reason, we don't get two consecutive
  5180. emphasis sections in this example:
  5181. ```````````````````````````````` example
  5182. *foo**bar*
  5183. .
  5184. <p><em>foo**bar</em></p>
  5185. ````````````````````````````````
  5186. The same condition ensures that the following
  5187. cases are all strong emphasis nested inside
  5188. emphasis, even when the interior spaces are
  5189. omitted:
  5190. ```````````````````````````````` example
  5191. ***foo** bar*
  5192. .
  5193. <p><em><strong>foo</strong> bar</em></p>
  5194. ````````````````````````````````
  5195. ```````````````````````````````` example
  5196. *foo **bar***
  5197. .
  5198. <p><em>foo <strong>bar</strong></em></p>
  5199. ````````````````````````````````
  5200. ```````````````````````````````` example
  5201. *foo**bar***
  5202. .
  5203. <p><em>foo<strong>bar</strong></em></p>
  5204. ````````````````````````````````
  5205. When the lengths of the interior closing and opening
  5206. delimiter runs are *both* multiples of 3, though,
  5207. they can match to create emphasis:
  5208. ```````````````````````````````` example
  5209. foo***bar***baz
  5210. .
  5211. <p>foo<em><strong>bar</strong></em>baz</p>
  5212. ````````````````````````````````
  5213. ```````````````````````````````` example
  5214. foo******bar*********baz
  5215. .
  5216. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5217. ````````````````````````````````
  5218. Indefinite levels of nesting are possible:
  5219. ```````````````````````````````` example
  5220. *foo **bar *baz* bim** bop*
  5221. .
  5222. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5223. ````````````````````````````````
  5224. ```````````````````````````````` example
  5225. *foo [*bar*](/url)*
  5226. .
  5227. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5228. ````````````````````````````````
  5229. There can be no empty emphasis or strong emphasis:
  5230. ```````````````````````````````` example
  5231. ** is not an empty emphasis
  5232. .
  5233. <p>** is not an empty emphasis</p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. **** is not an empty strong emphasis
  5237. .
  5238. <p>**** is not an empty strong emphasis</p>
  5239. ````````````````````````````````
  5240. Rule 10:
  5241. Any nonempty sequence of inline elements can be the contents of an
  5242. strongly emphasized span.
  5243. ```````````````````````````````` example
  5244. **foo [bar](/url)**
  5245. .
  5246. <p><strong>foo <a href="/url">bar</a></strong></p>
  5247. ````````````````````````````````
  5248. ```````````````````````````````` example
  5249. **foo
  5250. bar**
  5251. .
  5252. <p><strong>foo
  5253. bar</strong></p>
  5254. ````````````````````````````````
  5255. In particular, emphasis and strong emphasis can be nested
  5256. inside strong emphasis:
  5257. ```````````````````````````````` example
  5258. __foo _bar_ baz__
  5259. .
  5260. <p><strong>foo <em>bar</em> baz</strong></p>
  5261. ````````````````````````````````
  5262. ```````````````````````````````` example
  5263. __foo __bar__ baz__
  5264. .
  5265. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5266. ````````````````````````````````
  5267. ```````````````````````````````` example
  5268. ____foo__ bar__
  5269. .
  5270. <p><strong><strong>foo</strong> bar</strong></p>
  5271. ````````````````````````````````
  5272. ```````````````````````````````` example
  5273. **foo **bar****
  5274. .
  5275. <p><strong>foo <strong>bar</strong></strong></p>
  5276. ````````````````````````````````
  5277. ```````````````````````````````` example
  5278. **foo *bar* baz**
  5279. .
  5280. <p><strong>foo <em>bar</em> baz</strong></p>
  5281. ````````````````````````````````
  5282. ```````````````````````````````` example
  5283. **foo*bar*baz**
  5284. .
  5285. <p><strong>foo<em>bar</em>baz</strong></p>
  5286. ````````````````````````````````
  5287. ```````````````````````````````` example
  5288. ***foo* bar**
  5289. .
  5290. <p><strong><em>foo</em> bar</strong></p>
  5291. ````````````````````````````````
  5292. ```````````````````````````````` example
  5293. **foo *bar***
  5294. .
  5295. <p><strong>foo <em>bar</em></strong></p>
  5296. ````````````````````````````````
  5297. Indefinite levels of nesting are possible:
  5298. ```````````````````````````````` example
  5299. **foo *bar **baz**
  5300. bim* bop**
  5301. .
  5302. <p><strong>foo <em>bar <strong>baz</strong>
  5303. bim</em> bop</strong></p>
  5304. ````````````````````````````````
  5305. ```````````````````````````````` example
  5306. **foo [*bar*](/url)**
  5307. .
  5308. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5309. ````````````````````````````````
  5310. There can be no empty emphasis or strong emphasis:
  5311. ```````````````````````````````` example
  5312. __ is not an empty emphasis
  5313. .
  5314. <p>__ is not an empty emphasis</p>
  5315. ````````````````````````````````
  5316. ```````````````````````````````` example
  5317. ____ is not an empty strong emphasis
  5318. .
  5319. <p>____ is not an empty strong emphasis</p>
  5320. ````````````````````````````````
  5321. Rule 11:
  5322. ```````````````````````````````` example
  5323. foo ***
  5324. .
  5325. <p>foo ***</p>
  5326. ````````````````````````````````
  5327. ```````````````````````````````` example
  5328. foo *\**
  5329. .
  5330. <p>foo <em>*</em></p>
  5331. ````````````````````````````````
  5332. ```````````````````````````````` example
  5333. foo *_*
  5334. .
  5335. <p>foo <em>_</em></p>
  5336. ````````````````````````````````
  5337. ```````````````````````````````` example
  5338. foo *****
  5339. .
  5340. <p>foo *****</p>
  5341. ````````````````````````````````
  5342. ```````````````````````````````` example
  5343. foo **\***
  5344. .
  5345. <p>foo <strong>*</strong></p>
  5346. ````````````````````````````````
  5347. ```````````````````````````````` example
  5348. foo **_**
  5349. .
  5350. <p>foo <strong>_</strong></p>
  5351. ````````````````````````````````
  5352. Note that when delimiters do not match evenly, Rule 11 determines
  5353. that the excess literal `*` characters will appear outside of the
  5354. emphasis, rather than inside it:
  5355. ```````````````````````````````` example
  5356. **foo*
  5357. .
  5358. <p>*<em>foo</em></p>
  5359. ````````````````````````````````
  5360. ```````````````````````````````` example
  5361. *foo**
  5362. .
  5363. <p><em>foo</em>*</p>
  5364. ````````````````````````````````
  5365. ```````````````````````````````` example
  5366. ***foo**
  5367. .
  5368. <p>*<strong>foo</strong></p>
  5369. ````````````````````````````````
  5370. ```````````````````````````````` example
  5371. ****foo*
  5372. .
  5373. <p>***<em>foo</em></p>
  5374. ````````````````````````````````
  5375. ```````````````````````````````` example
  5376. **foo***
  5377. .
  5378. <p><strong>foo</strong>*</p>
  5379. ````````````````````````````````
  5380. ```````````````````````````````` example
  5381. *foo****
  5382. .
  5383. <p><em>foo</em>***</p>
  5384. ````````````````````````````````
  5385. Rule 12:
  5386. ```````````````````````````````` example
  5387. foo ___
  5388. .
  5389. <p>foo ___</p>
  5390. ````````````````````````````````
  5391. ```````````````````````````````` example
  5392. foo _\__
  5393. .
  5394. <p>foo <em>_</em></p>
  5395. ````````````````````````````````
  5396. ```````````````````````````````` example
  5397. foo _*_
  5398. .
  5399. <p>foo <em>*</em></p>
  5400. ````````````````````````````````
  5401. ```````````````````````````````` example
  5402. foo _____
  5403. .
  5404. <p>foo _____</p>
  5405. ````````````````````````````````
  5406. ```````````````````````````````` example
  5407. foo __\___
  5408. .
  5409. <p>foo <strong>_</strong></p>
  5410. ````````````````````````````````
  5411. ```````````````````````````````` example
  5412. foo __*__
  5413. .
  5414. <p>foo <strong>*</strong></p>
  5415. ````````````````````````````````
  5416. ```````````````````````````````` example
  5417. __foo_
  5418. .
  5419. <p>_<em>foo</em></p>
  5420. ````````````````````````````````
  5421. Note that when delimiters do not match evenly, Rule 12 determines
  5422. that the excess literal `_` characters will appear outside of the
  5423. emphasis, rather than inside it:
  5424. ```````````````````````````````` example
  5425. _foo__
  5426. .
  5427. <p><em>foo</em>_</p>
  5428. ````````````````````````````````
  5429. ```````````````````````````````` example
  5430. ___foo__
  5431. .
  5432. <p>_<strong>foo</strong></p>
  5433. ````````````````````````````````
  5434. ```````````````````````````````` example
  5435. ____foo_
  5436. .
  5437. <p>___<em>foo</em></p>
  5438. ````````````````````````````````
  5439. ```````````````````````````````` example
  5440. __foo___
  5441. .
  5442. <p><strong>foo</strong>_</p>
  5443. ````````````````````````````````
  5444. ```````````````````````````````` example
  5445. _foo____
  5446. .
  5447. <p><em>foo</em>___</p>
  5448. ````````````````````````````````
  5449. Rule 13 implies that if you want emphasis nested directly inside
  5450. emphasis, you must use different delimiters:
  5451. ```````````````````````````````` example
  5452. **foo**
  5453. .
  5454. <p><strong>foo</strong></p>
  5455. ````````````````````````````````
  5456. ```````````````````````````````` example
  5457. *_foo_*
  5458. .
  5459. <p><em><em>foo</em></em></p>
  5460. ````````````````````````````````
  5461. ```````````````````````````````` example
  5462. __foo__
  5463. .
  5464. <p><strong>foo</strong></p>
  5465. ````````````````````````````````
  5466. ```````````````````````````````` example
  5467. _*foo*_
  5468. .
  5469. <p><em><em>foo</em></em></p>
  5470. ````````````````````````````````
  5471. However, strong emphasis within strong emphasis is possible without
  5472. switching delimiters:
  5473. ```````````````````````````````` example
  5474. ****foo****
  5475. .
  5476. <p><strong><strong>foo</strong></strong></p>
  5477. ````````````````````````````````
  5478. ```````````````````````````````` example
  5479. ____foo____
  5480. .
  5481. <p><strong><strong>foo</strong></strong></p>
  5482. ````````````````````````````````
  5483. Rule 13 can be applied to arbitrarily long sequences of
  5484. delimiters:
  5485. ```````````````````````````````` example
  5486. ******foo******
  5487. .
  5488. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5489. ````````````````````````````````
  5490. Rule 14:
  5491. ```````````````````````````````` example
  5492. ***foo***
  5493. .
  5494. <p><em><strong>foo</strong></em></p>
  5495. ````````````````````````````````
  5496. ```````````````````````````````` example
  5497. _____foo_____
  5498. .
  5499. <p><em><strong><strong>foo</strong></strong></em></p>
  5500. ````````````````````````````````
  5501. Rule 15:
  5502. ```````````````````````````````` example
  5503. *foo _bar* baz_
  5504. .
  5505. <p><em>foo _bar</em> baz_</p>
  5506. ````````````````````````````````
  5507. ```````````````````````````````` example
  5508. *foo __bar *baz bim__ bam*
  5509. .
  5510. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5511. ````````````````````````````````
  5512. Rule 16:
  5513. ```````````````````````````````` example
  5514. **foo **bar baz**
  5515. .
  5516. <p>**foo <strong>bar baz</strong></p>
  5517. ````````````````````````````````
  5518. ```````````````````````````````` example
  5519. *foo *bar baz*
  5520. .
  5521. <p>*foo <em>bar baz</em></p>
  5522. ````````````````````````````````
  5523. Rule 17:
  5524. ```````````````````````````````` example
  5525. *[bar*](/url)
  5526. .
  5527. <p>*<a href="/url">bar*</a></p>
  5528. ````````````````````````````````
  5529. ```````````````````````````````` example
  5530. _foo [bar_](/url)
  5531. .
  5532. <p>_foo <a href="/url">bar_</a></p>
  5533. ````````````````````````````````
  5534. ```````````````````````````````` example
  5535. *<img src="foo" title="*"/>
  5536. .
  5537. <p>*<img src="foo" title="*"/></p>
  5538. ````````````````````````````````
  5539. ```````````````````````````````` example
  5540. **<a href="**">
  5541. .
  5542. <p>**<a href="**"></p>
  5543. ````````````````````````````````
  5544. ```````````````````````````````` example
  5545. __<a href="__">
  5546. .
  5547. <p>__<a href="__"></p>
  5548. ````````````````````````````````
  5549. ```````````````````````````````` example
  5550. *a `*`*
  5551. .
  5552. <p><em>a <code>*</code></em></p>
  5553. ````````````````````````````````
  5554. ```````````````````````````````` example
  5555. _a `_`_
  5556. .
  5557. <p><em>a <code>_</code></em></p>
  5558. ````````````````````````````````
  5559. ```````````````````````````````` example
  5560. **a<http://foo.bar/?q=**>
  5561. .
  5562. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5563. ````````````````````````````````
  5564. ```````````````````````````````` example
  5565. __a<http://foo.bar/?q=__>
  5566. .
  5567. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5568. ````````````````````````````````
  5569. ## Links
  5570. A link contains [link text] (the visible text), a [link destination]
  5571. (the URI that is the link destination), and optionally a [link title].
  5572. There are two basic kinds of links in Markdown. In [inline links] the
  5573. destination and title are given immediately after the link text. In
  5574. [reference links] the destination and title are defined elsewhere in
  5575. the document.
  5576. A [link text](@) consists of a sequence of zero or more
  5577. inline elements enclosed by square brackets (`[` and `]`). The
  5578. following rules apply:
  5579. - Links may not contain other links, at any level of nesting. If
  5580. multiple otherwise valid link definitions appear nested inside each
  5581. other, the inner-most definition is used.
  5582. - Brackets are allowed in the [link text] only if (a) they
  5583. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5584. with an open bracket `[`, a sequence of zero or more inlines, and
  5585. a close bracket `]`.
  5586. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5587. than the brackets in link text. Thus, for example,
  5588. `` [foo`]` `` could not be a link text, since the second `]`
  5589. is part of a code span.
  5590. - The brackets in link text bind more tightly than markers for
  5591. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5592. A [link destination](@) consists of either
  5593. - a sequence of zero or more characters between an opening `<` and a
  5594. closing `>` that contains no line breaks or unescaped
  5595. `<` or `>` characters, or
  5596. - a nonempty sequence of characters that does not start with
  5597. `<`, does not include ASCII space or control characters, and
  5598. includes parentheses only if (a) they are backslash-escaped or
  5599. (b) they are part of a balanced pair of unescaped parentheses.
  5600. (Implementations may impose limits on parentheses nesting to
  5601. avoid performance issues, but at least three levels of nesting
  5602. should be supported.)
  5603. A [link title](@) consists of either
  5604. - a sequence of zero or more characters between straight double-quote
  5605. characters (`"`), including a `"` character only if it is
  5606. backslash-escaped, or
  5607. - a sequence of zero or more characters between straight single-quote
  5608. characters (`'`), including a `'` character only if it is
  5609. backslash-escaped, or
  5610. - a sequence of zero or more characters between matching parentheses
  5611. (`(...)`), including a `(` or `)` character only if it is
  5612. backslash-escaped.
  5613. Although [link titles] may span multiple lines, they may not contain
  5614. a [blank line].
  5615. An [inline link](@) consists of a [link text] followed immediately
  5616. by a left parenthesis `(`, optional [whitespace], an optional
  5617. [link destination], an optional [link title] separated from the link
  5618. destination by [whitespace], optional [whitespace], and a right
  5619. parenthesis `)`. The link's text consists of the inlines contained
  5620. in the [link text] (excluding the enclosing square brackets).
  5621. The link's URI consists of the link destination, excluding enclosing
  5622. `<...>` if present, with backslash-escapes in effect as described
  5623. above. The link's title consists of the link title, excluding its
  5624. enclosing delimiters, with backslash-escapes in effect as described
  5625. above.
  5626. Here is a simple inline link:
  5627. ```````````````````````````````` example
  5628. [link](/uri "title")
  5629. .
  5630. <p><a href="/uri" title="title">link</a></p>
  5631. ````````````````````````````````
  5632. The title may be omitted:
  5633. ```````````````````````````````` example
  5634. [link](/uri)
  5635. .
  5636. <p><a href="/uri">link</a></p>
  5637. ````````````````````````````````
  5638. Both the title and the destination may be omitted:
  5639. ```````````````````````````````` example
  5640. [link]()
  5641. .
  5642. <p><a href="">link</a></p>
  5643. ````````````````````````````````
  5644. ```````````````````````````````` example
  5645. [link](<>)
  5646. .
  5647. <p><a href="">link</a></p>
  5648. ````````````````````````````````
  5649. The destination can only contain spaces if it is
  5650. enclosed in pointy brackets:
  5651. ```````````````````````````````` example
  5652. [link](/my uri)
  5653. .
  5654. <p>[link](/my uri)</p>
  5655. ````````````````````````````````
  5656. ```````````````````````````````` example
  5657. [link](</my uri>)
  5658. .
  5659. <p><a href="/my%20uri">link</a></p>
  5660. ````````````````````````````````
  5661. The destination cannot contain line breaks,
  5662. even if enclosed in pointy brackets:
  5663. ```````````````````````````````` example
  5664. [link](foo
  5665. bar)
  5666. .
  5667. <p>[link](foo
  5668. bar)</p>
  5669. ````````````````````````````````
  5670. ```````````````````````````````` example
  5671. [link](<foo
  5672. bar>)
  5673. .
  5674. <p>[link](<foo
  5675. bar>)</p>
  5676. ````````````````````````````````
  5677. Pointy brackets that enclose links must be unescaped:
  5678. ```````````````````````````````` example
  5679. [link](<foo\>)
  5680. .
  5681. <p>[link](&lt;foo&gt;)</p>
  5682. ````````````````````````````````
  5683. Parentheses inside the link destination may be escaped:
  5684. ```````````````````````````````` example
  5685. [link](\(foo\))
  5686. .
  5687. <p><a href="(foo)">link</a></p>
  5688. ````````````````````````````````
  5689. Any number of parentheses are allowed without escaping, as long as they are
  5690. balanced:
  5691. ```````````````````````````````` example
  5692. [link](foo(and(bar)))
  5693. .
  5694. <p><a href="foo(and(bar))">link</a></p>
  5695. ````````````````````````````````
  5696. However, if you have unbalanced parentheses, you need to escape or use the
  5697. `<...>` form:
  5698. ```````````````````````````````` example
  5699. [link](foo\(and\(bar\))
  5700. .
  5701. <p><a href="foo(and(bar)">link</a></p>
  5702. ````````````````````````````````
  5703. ```````````````````````````````` example
  5704. [link](<foo(and(bar)>)
  5705. .
  5706. <p><a href="foo(and(bar)">link</a></p>
  5707. ````````````````````````````````
  5708. Parentheses and other symbols can also be escaped, as usual
  5709. in Markdown:
  5710. ```````````````````````````````` example
  5711. [link](foo\)\:)
  5712. .
  5713. <p><a href="foo):">link</a></p>
  5714. ````````````````````````````````
  5715. A link can contain fragment identifiers and queries:
  5716. ```````````````````````````````` example
  5717. [link](#fragment)
  5718. [link](http://example.com#fragment)
  5719. [link](http://example.com?foo=3#frag)
  5720. .
  5721. <p><a href="#fragment">link</a></p>
  5722. <p><a href="http://example.com#fragment">link</a></p>
  5723. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5724. ````````````````````````````````
  5725. Note that a backslash before a non-escapable character is
  5726. just a backslash:
  5727. ```````````````````````````````` example
  5728. [link](foo\bar)
  5729. .
  5730. <p><a href="foo%5Cbar">link</a></p>
  5731. ````````````````````````````````
  5732. URL-escaping should be left alone inside the destination, as all
  5733. URL-escaped characters are also valid URL characters. Entity and
  5734. numerical character references in the destination will be parsed
  5735. into the corresponding Unicode code points, as usual. These may
  5736. be optionally URL-escaped when written as HTML, but this spec
  5737. does not enforce any particular policy for rendering URLs in
  5738. HTML or other formats. Renderers may make different decisions
  5739. about how to escape or normalize URLs in the output.
  5740. ```````````````````````````````` example
  5741. [link](foo%20b&auml;)
  5742. .
  5743. <p><a href="foo%20b%C3%A4">link</a></p>
  5744. ````````````````````````````````
  5745. Note that, because titles can often be parsed as destinations,
  5746. if you try to omit the destination and keep the title, you'll
  5747. get unexpected results:
  5748. ```````````````````````````````` example
  5749. [link]("title")
  5750. .
  5751. <p><a href="%22title%22">link</a></p>
  5752. ````````````````````````````````
  5753. Titles may be in single quotes, double quotes, or parentheses:
  5754. ```````````````````````````````` example
  5755. [link](/url "title")
  5756. [link](/url 'title')
  5757. [link](/url (title))
  5758. .
  5759. <p><a href="/url" title="title">link</a>
  5760. <a href="/url" title="title">link</a>
  5761. <a href="/url" title="title">link</a></p>
  5762. ````````````````````````````````
  5763. Backslash escapes and entity and numeric character references
  5764. may be used in titles:
  5765. ```````````````````````````````` example
  5766. [link](/url "title \"&quot;")
  5767. .
  5768. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5769. ````````````````````````````````
  5770. Titles must be separated from the link using a [whitespace].
  5771. Other [Unicode whitespace] like non-breaking space doesn't work.
  5772. ```````````````````````````````` example
  5773. [link](/url "title")
  5774. .
  5775. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5776. ````````````````````````````````
  5777. Nested balanced quotes are not allowed without escaping:
  5778. ```````````````````````````````` example
  5779. [link](/url "title "and" title")
  5780. .
  5781. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5782. ````````````````````````````````
  5783. But it is easy to work around this by using a different quote type:
  5784. ```````````````````````````````` example
  5785. [link](/url 'title "and" title')
  5786. .
  5787. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5788. ````````````````````````````````
  5789. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5790. title, and its test suite included a test demonstrating this.
  5791. But it is hard to see a good rationale for the extra complexity this
  5792. brings, since there are already many ways---backslash escaping,
  5793. entity and numeric character references, or using a different
  5794. quote type for the enclosing title---to write titles containing
  5795. double quotes. `Markdown.pl`'s handling of titles has a number
  5796. of other strange features. For example, it allows single-quoted
  5797. titles in inline links, but not reference links. And, in
  5798. reference links but not inline links, it allows a title to begin
  5799. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5800. titles with no closing quotation mark, though 1.0.2b8 does not.
  5801. It seems preferable to adopt a simple, rational rule that works
  5802. the same way in inline links and link reference definitions.)
  5803. [Whitespace] is allowed around the destination and title:
  5804. ```````````````````````````````` example
  5805. [link]( /uri
  5806. "title" )
  5807. .
  5808. <p><a href="/uri" title="title">link</a></p>
  5809. ````````````````````````````````
  5810. But it is not allowed between the link text and the
  5811. following parenthesis:
  5812. ```````````````````````````````` example
  5813. [link] (/uri)
  5814. .
  5815. <p>[link] (/uri)</p>
  5816. ````````````````````````````````
  5817. The link text may contain balanced brackets, but not unbalanced ones,
  5818. unless they are escaped:
  5819. ```````````````````````````````` example
  5820. [link [foo [bar]]](/uri)
  5821. .
  5822. <p><a href="/uri">link [foo [bar]]</a></p>
  5823. ````````````````````````````````
  5824. ```````````````````````````````` example
  5825. [link] bar](/uri)
  5826. .
  5827. <p>[link] bar](/uri)</p>
  5828. ````````````````````````````````
  5829. ```````````````````````````````` example
  5830. [link [bar](/uri)
  5831. .
  5832. <p>[link <a href="/uri">bar</a></p>
  5833. ````````````````````````````````
  5834. ```````````````````````````````` example
  5835. [link \[bar](/uri)
  5836. .
  5837. <p><a href="/uri">link [bar</a></p>
  5838. ````````````````````````````````
  5839. The link text may contain inline content:
  5840. ```````````````````````````````` example
  5841. [link *foo **bar** `#`*](/uri)
  5842. .
  5843. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5844. ````````````````````````````````
  5845. ```````````````````````````````` example
  5846. [![moon](moon.jpg)](/uri)
  5847. .
  5848. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5849. ````````````````````````````````
  5850. However, links may not contain other links, at any level of nesting.
  5851. ```````````````````````````````` example
  5852. [foo [bar](/uri)](/uri)
  5853. .
  5854. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5855. ````````````````````````````````
  5856. ```````````````````````````````` example
  5857. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5858. .
  5859. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5860. ````````````````````````````````
  5861. ```````````````````````````````` example
  5862. ![[[foo](uri1)](uri2)](uri3)
  5863. .
  5864. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5865. ````````````````````````````````
  5866. These cases illustrate the precedence of link text grouping over
  5867. emphasis grouping:
  5868. ```````````````````````````````` example
  5869. *[foo*](/uri)
  5870. .
  5871. <p>*<a href="/uri">foo*</a></p>
  5872. ````````````````````````````````
  5873. ```````````````````````````````` example
  5874. [foo *bar](baz*)
  5875. .
  5876. <p><a href="baz*">foo *bar</a></p>
  5877. ````````````````````````````````
  5878. Note that brackets that *aren't* part of links do not take
  5879. precedence:
  5880. ```````````````````````````````` example
  5881. *foo [bar* baz]
  5882. .
  5883. <p><em>foo [bar</em> baz]</p>
  5884. ````````````````````````````````
  5885. These cases illustrate the precedence of HTML tags, code spans,
  5886. and autolinks over link grouping:
  5887. ```````````````````````````````` example
  5888. [foo <bar attr="](baz)">
  5889. .
  5890. <p>[foo <bar attr="](baz)"></p>
  5891. ````````````````````````````````
  5892. ```````````````````````````````` example
  5893. [foo`](/uri)`
  5894. .
  5895. <p>[foo<code>](/uri)</code></p>
  5896. ````````````````````````````````
  5897. ```````````````````````````````` example
  5898. [foo<http://example.com/?search=](uri)>
  5899. .
  5900. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5901. ````````````````````````````````
  5902. There are three kinds of [reference link](@)s:
  5903. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5904. and [shortcut](#shortcut-reference-link).
  5905. A [full reference link](@)
  5906. consists of a [link text] immediately followed by a [link label]
  5907. that [matches] a [link reference definition] elsewhere in the document.
  5908. A [link label](@) begins with a left bracket (`[`) and ends
  5909. with the first right bracket (`]`) that is not backslash-escaped.
  5910. Between these brackets there must be at least one [non-whitespace character].
  5911. Unescaped square bracket characters are not allowed inside the
  5912. opening and closing square brackets of [link labels]. A link
  5913. label can have at most 999 characters inside the square
  5914. brackets.
  5915. One label [matches](@)
  5916. another just in case their normalized forms are equal. To normalize a
  5917. label, strip off the opening and closing brackets,
  5918. perform the *Unicode case fold*, strip leading and trailing
  5919. [whitespace] and collapse consecutive internal
  5920. [whitespace] to a single space. If there are multiple
  5921. matching reference link definitions, the one that comes first in the
  5922. document is used. (It is desirable in such cases to emit a warning.)
  5923. The contents of the first link label are parsed as inlines, which are
  5924. used as the link's text. The link's URI and title are provided by the
  5925. matching [link reference definition].
  5926. Here is a simple example:
  5927. ```````````````````````````````` example
  5928. [foo][bar]
  5929. [bar]: /url "title"
  5930. .
  5931. <p><a href="/url" title="title">foo</a></p>
  5932. ````````````````````````````````
  5933. The rules for the [link text] are the same as with
  5934. [inline links]. Thus:
  5935. The link text may contain balanced brackets, but not unbalanced ones,
  5936. unless they are escaped:
  5937. ```````````````````````````````` example
  5938. [link [foo [bar]]][ref]
  5939. [ref]: /uri
  5940. .
  5941. <p><a href="/uri">link [foo [bar]]</a></p>
  5942. ````````````````````````````````
  5943. ```````````````````````````````` example
  5944. [link \[bar][ref]
  5945. [ref]: /uri
  5946. .
  5947. <p><a href="/uri">link [bar</a></p>
  5948. ````````````````````````````````
  5949. The link text may contain inline content:
  5950. ```````````````````````````````` example
  5951. [link *foo **bar** `#`*][ref]
  5952. [ref]: /uri
  5953. .
  5954. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5955. ````````````````````````````````
  5956. ```````````````````````````````` example
  5957. [![moon](moon.jpg)][ref]
  5958. [ref]: /uri
  5959. .
  5960. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5961. ````````````````````````````````
  5962. However, links may not contain other links, at any level of nesting.
  5963. ```````````````````````````````` example
  5964. [foo [bar](/uri)][ref]
  5965. [ref]: /uri
  5966. .
  5967. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5968. ````````````````````````````````
  5969. ```````````````````````````````` example
  5970. [foo *bar [baz][ref]*][ref]
  5971. [ref]: /uri
  5972. .
  5973. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5974. ````````````````````````````````
  5975. (In the examples above, we have two [shortcut reference links]
  5976. instead of one [full reference link].)
  5977. The following cases illustrate the precedence of link text grouping over
  5978. emphasis grouping:
  5979. ```````````````````````````````` example
  5980. *[foo*][ref]
  5981. [ref]: /uri
  5982. .
  5983. <p>*<a href="/uri">foo*</a></p>
  5984. ````````````````````````````````
  5985. ```````````````````````````````` example
  5986. [foo *bar][ref]
  5987. [ref]: /uri
  5988. .
  5989. <p><a href="/uri">foo *bar</a></p>
  5990. ````````````````````````````````
  5991. These cases illustrate the precedence of HTML tags, code spans,
  5992. and autolinks over link grouping:
  5993. ```````````````````````````````` example
  5994. [foo <bar attr="][ref]">
  5995. [ref]: /uri
  5996. .
  5997. <p>[foo <bar attr="][ref]"></p>
  5998. ````````````````````````````````
  5999. ```````````````````````````````` example
  6000. [foo`][ref]`
  6001. [ref]: /uri
  6002. .
  6003. <p>[foo<code>][ref]</code></p>
  6004. ````````````````````````````````
  6005. ```````````````````````````````` example
  6006. [foo<http://example.com/?search=][ref]>
  6007. [ref]: /uri
  6008. .
  6009. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6010. ````````````````````````````````
  6011. Matching is case-insensitive:
  6012. ```````````````````````````````` example
  6013. [foo][BaR]
  6014. [bar]: /url "title"
  6015. .
  6016. <p><a href="/url" title="title">foo</a></p>
  6017. ````````````````````````````````
  6018. Unicode case fold is used:
  6019. ```````````````````````````````` example
  6020. [Толпой][Толпой] is a Russian word.
  6021. [ТОЛПОЙ]: /url
  6022. .
  6023. <p><a href="/url">Толпой</a> is a Russian word.</p>
  6024. ````````````````````````````````
  6025. Consecutive internal [whitespace] is treated as one space for
  6026. purposes of determining matching:
  6027. ```````````````````````````````` example
  6028. [Foo
  6029. bar]: /url
  6030. [Baz][Foo bar]
  6031. .
  6032. <p><a href="/url">Baz</a></p>
  6033. ````````````````````````````````
  6034. No [whitespace] is allowed between the [link text] and the
  6035. [link label]:
  6036. ```````````````````````````````` example
  6037. [foo] [bar]
  6038. [bar]: /url "title"
  6039. .
  6040. <p>[foo] <a href="/url" title="title">bar</a></p>
  6041. ````````````````````````````````
  6042. ```````````````````````````````` example
  6043. [foo]
  6044. [bar]
  6045. [bar]: /url "title"
  6046. .
  6047. <p>[foo]
  6048. <a href="/url" title="title">bar</a></p>
  6049. ````````````````````````````````
  6050. This is a departure from John Gruber's original Markdown syntax
  6051. description, which explicitly allows whitespace between the link
  6052. text and the link label. It brings reference links in line with
  6053. [inline links], which (according to both original Markdown and
  6054. this spec) cannot have whitespace after the link text. More
  6055. importantly, it prevents inadvertent capture of consecutive
  6056. [shortcut reference links]. If whitespace is allowed between the
  6057. link text and the link label, then in the following we will have
  6058. a single reference link, not two shortcut reference links, as
  6059. intended:
  6060. ``` markdown
  6061. [foo]
  6062. [bar]
  6063. [foo]: /url1
  6064. [bar]: /url2
  6065. ```
  6066. (Note that [shortcut reference links] were introduced by Gruber
  6067. himself in a beta version of `Markdown.pl`, but never included
  6068. in the official syntax description. Without shortcut reference
  6069. links, it is harmless to allow space between the link text and
  6070. link label; but once shortcut references are introduced, it is
  6071. too dangerous to allow this, as it frequently leads to
  6072. unintended results.)
  6073. When there are multiple matching [link reference definitions],
  6074. the first is used:
  6075. ```````````````````````````````` example
  6076. [foo]: /url1
  6077. [foo]: /url2
  6078. [bar][foo]
  6079. .
  6080. <p><a href="/url1">bar</a></p>
  6081. ````````````````````````````````
  6082. Note that matching is performed on normalized strings, not parsed
  6083. inline content. So the following does not match, even though the
  6084. labels define equivalent inline content:
  6085. ```````````````````````````````` example
  6086. [bar][foo\!]
  6087. [foo!]: /url
  6088. .
  6089. <p>[bar][foo!]</p>
  6090. ````````````````````````````````
  6091. [Link labels] cannot contain brackets, unless they are
  6092. backslash-escaped:
  6093. ```````````````````````````````` example
  6094. [foo][ref[]
  6095. [ref[]: /uri
  6096. .
  6097. <p>[foo][ref[]</p>
  6098. <p>[ref[]: /uri</p>
  6099. ````````````````````````````````
  6100. ```````````````````````````````` example
  6101. [foo][ref[bar]]
  6102. [ref[bar]]: /uri
  6103. .
  6104. <p>[foo][ref[bar]]</p>
  6105. <p>[ref[bar]]: /uri</p>
  6106. ````````````````````````````````
  6107. ```````````````````````````````` example
  6108. [[[foo]]]
  6109. [[[foo]]]: /url
  6110. .
  6111. <p>[[[foo]]]</p>
  6112. <p>[[[foo]]]: /url</p>
  6113. ````````````````````````````````
  6114. ```````````````````````````````` example
  6115. [foo][ref\[]
  6116. [ref\[]: /uri
  6117. .
  6118. <p><a href="/uri">foo</a></p>
  6119. ````````````````````````````````
  6120. Note that in this example `]` is not backslash-escaped:
  6121. ```````````````````````````````` example
  6122. [bar\\]: /uri
  6123. [bar\\]
  6124. .
  6125. <p><a href="/uri">bar\</a></p>
  6126. ````````````````````````````````
  6127. A [link label] must contain at least one [non-whitespace character]:
  6128. ```````````````````````````````` example
  6129. []
  6130. []: /uri
  6131. .
  6132. <p>[]</p>
  6133. <p>[]: /uri</p>
  6134. ````````````````````````````````
  6135. ```````````````````````````````` example
  6136. [
  6137. ]
  6138. [
  6139. ]: /uri
  6140. .
  6141. <p>[
  6142. ]</p>
  6143. <p>[
  6144. ]: /uri</p>
  6145. ````````````````````````````````
  6146. A [collapsed reference link](@)
  6147. consists of a [link label] that [matches] a
  6148. [link reference definition] elsewhere in the
  6149. document, followed by the string `[]`.
  6150. The contents of the first link label are parsed as inlines,
  6151. which are used as the link's text. The link's URI and title are
  6152. provided by the matching reference link definition. Thus,
  6153. `[foo][]` is equivalent to `[foo][foo]`.
  6154. ```````````````````````````````` example
  6155. [foo][]
  6156. [foo]: /url "title"
  6157. .
  6158. <p><a href="/url" title="title">foo</a></p>
  6159. ````````````````````````````````
  6160. ```````````````````````````````` example
  6161. [*foo* bar][]
  6162. [*foo* bar]: /url "title"
  6163. .
  6164. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6165. ````````````````````````````````
  6166. The link labels are case-insensitive:
  6167. ```````````````````````````````` example
  6168. [Foo][]
  6169. [foo]: /url "title"
  6170. .
  6171. <p><a href="/url" title="title">Foo</a></p>
  6172. ````````````````````````````````
  6173. As with full reference links, [whitespace] is not
  6174. allowed between the two sets of brackets:
  6175. ```````````````````````````````` example
  6176. [foo]
  6177. []
  6178. [foo]: /url "title"
  6179. .
  6180. <p><a href="/url" title="title">foo</a>
  6181. []</p>
  6182. ````````````````````````````````
  6183. A [shortcut reference link](@)
  6184. consists of a [link label] that [matches] a
  6185. [link reference definition] elsewhere in the
  6186. document and is not followed by `[]` or a link label.
  6187. The contents of the first link label are parsed as inlines,
  6188. which are used as the link's text. The link's URI and title
  6189. are provided by the matching link reference definition.
  6190. Thus, `[foo]` is equivalent to `[foo][]`.
  6191. ```````````````````````````````` example
  6192. [foo]
  6193. [foo]: /url "title"
  6194. .
  6195. <p><a href="/url" title="title">foo</a></p>
  6196. ````````````````````````````````
  6197. ```````````````````````````````` example
  6198. [*foo* bar]
  6199. [*foo* bar]: /url "title"
  6200. .
  6201. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6202. ````````````````````````````````
  6203. ```````````````````````````````` example
  6204. [[*foo* bar]]
  6205. [*foo* bar]: /url "title"
  6206. .
  6207. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6208. ````````````````````````````````
  6209. ```````````````````````````````` example
  6210. [[bar [foo]
  6211. [foo]: /url
  6212. .
  6213. <p>[[bar <a href="/url">foo</a></p>
  6214. ````````````````````````````````
  6215. The link labels are case-insensitive:
  6216. ```````````````````````````````` example
  6217. [Foo]
  6218. [foo]: /url "title"
  6219. .
  6220. <p><a href="/url" title="title">Foo</a></p>
  6221. ````````````````````````````````
  6222. A space after the link text should be preserved:
  6223. ```````````````````````````````` example
  6224. [foo] bar
  6225. [foo]: /url
  6226. .
  6227. <p><a href="/url">foo</a> bar</p>
  6228. ````````````````````````````````
  6229. If you just want bracketed text, you can backslash-escape the
  6230. opening bracket to avoid links:
  6231. ```````````````````````````````` example
  6232. \[foo]
  6233. [foo]: /url "title"
  6234. .
  6235. <p>[foo]</p>
  6236. ````````````````````````````````
  6237. Note that this is a link, because a link label ends with the first
  6238. following closing bracket:
  6239. ```````````````````````````````` example
  6240. [foo*]: /url
  6241. *[foo*]
  6242. .
  6243. <p>*<a href="/url">foo*</a></p>
  6244. ````````````````````````````````
  6245. Full and compact references take precedence over shortcut
  6246. references:
  6247. ```````````````````````````````` example
  6248. [foo][bar]
  6249. [foo]: /url1
  6250. [bar]: /url2
  6251. .
  6252. <p><a href="/url2">foo</a></p>
  6253. ````````````````````````````````
  6254. ```````````````````````````````` example
  6255. [foo][]
  6256. [foo]: /url1
  6257. .
  6258. <p><a href="/url1">foo</a></p>
  6259. ````````````````````````````````
  6260. Inline links also take precedence:
  6261. ```````````````````````````````` example
  6262. [foo]()
  6263. [foo]: /url1
  6264. .
  6265. <p><a href="">foo</a></p>
  6266. ````````````````````````````````
  6267. ```````````````````````````````` example
  6268. [foo](not a link)
  6269. [foo]: /url1
  6270. .
  6271. <p><a href="/url1">foo</a>(not a link)</p>
  6272. ````````````````````````````````
  6273. In the following case `[bar][baz]` is parsed as a reference,
  6274. `[foo]` as normal text:
  6275. ```````````````````````````````` example
  6276. [foo][bar][baz]
  6277. [baz]: /url
  6278. .
  6279. <p>[foo]<a href="/url">bar</a></p>
  6280. ````````````````````````````````
  6281. Here, though, `[foo][bar]` is parsed as a reference, since
  6282. `[bar]` is defined:
  6283. ```````````````````````````````` example
  6284. [foo][bar][baz]
  6285. [baz]: /url1
  6286. [bar]: /url2
  6287. .
  6288. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6289. ````````````````````````````````
  6290. Here `[foo]` is not parsed as a shortcut reference, because it
  6291. is followed by a link label (even though `[bar]` is not defined):
  6292. ```````````````````````````````` example
  6293. [foo][bar][baz]
  6294. [baz]: /url1
  6295. [foo]: /url2
  6296. .
  6297. <p>[foo]<a href="/url1">bar</a></p>
  6298. ````````````````````````````````
  6299. ## Images
  6300. Syntax for images is like the syntax for links, with one
  6301. difference. Instead of [link text], we have an
  6302. [image description](@). The rules for this are the
  6303. same as for [link text], except that (a) an
  6304. image description starts with `![` rather than `[`, and
  6305. (b) an image description may contain links.
  6306. An image description has inline elements
  6307. as its contents. When an image is rendered to HTML,
  6308. this is standardly used as the image's `alt` attribute.
  6309. ```````````````````````````````` example
  6310. ![foo](/url "title")
  6311. .
  6312. <p><img src="/url" alt="foo" title="title" /></p>
  6313. ````````````````````````````````
  6314. ```````````````````````````````` example
  6315. ![foo *bar*]
  6316. [foo *bar*]: train.jpg "train & tracks"
  6317. .
  6318. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6319. ````````````````````````````````
  6320. ```````````````````````````````` example
  6321. ![foo ![bar](/url)](/url2)
  6322. .
  6323. <p><img src="/url2" alt="foo bar" /></p>
  6324. ````````````````````````````````
  6325. ```````````````````````````````` example
  6326. ![foo [bar](/url)](/url2)
  6327. .
  6328. <p><img src="/url2" alt="foo bar" /></p>
  6329. ````````````````````````````````
  6330. Though this spec is concerned with parsing, not rendering, it is
  6331. recommended that in rendering to HTML, only the plain string content
  6332. of the [image description] be used. Note that in
  6333. the above example, the alt attribute's value is `foo bar`, not `foo
  6334. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6335. content is rendered, without formatting.
  6336. ```````````````````````````````` example
  6337. ![foo *bar*][]
  6338. [foo *bar*]: train.jpg "train & tracks"
  6339. .
  6340. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6341. ````````````````````````````````
  6342. ```````````````````````````````` example
  6343. ![foo *bar*][foobar]
  6344. [FOOBAR]: train.jpg "train & tracks"
  6345. .
  6346. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6347. ````````````````````````````````
  6348. ```````````````````````````````` example
  6349. ![foo](train.jpg)
  6350. .
  6351. <p><img src="train.jpg" alt="foo" /></p>
  6352. ````````````````````````````````
  6353. ```````````````````````````````` example
  6354. My ![foo bar](/path/to/train.jpg "title" )
  6355. .
  6356. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6357. ````````````````````````````````
  6358. ```````````````````````````````` example
  6359. ![foo](<url>)
  6360. .
  6361. <p><img src="url" alt="foo" /></p>
  6362. ````````````````````````````````
  6363. ```````````````````````````````` example
  6364. ![](/url)
  6365. .
  6366. <p><img src="/url" alt="" /></p>
  6367. ````````````````````````````````
  6368. Reference-style:
  6369. ```````````````````````````````` example
  6370. ![foo][bar]
  6371. [bar]: /url
  6372. .
  6373. <p><img src="/url" alt="foo" /></p>
  6374. ````````````````````````````````
  6375. ```````````````````````````````` example
  6376. ![foo][bar]
  6377. [BAR]: /url
  6378. .
  6379. <p><img src="/url" alt="foo" /></p>
  6380. ````````````````````````````````
  6381. Collapsed:
  6382. ```````````````````````````````` example
  6383. ![foo][]
  6384. [foo]: /url "title"
  6385. .
  6386. <p><img src="/url" alt="foo" title="title" /></p>
  6387. ````````````````````````````````
  6388. ```````````````````````````````` example
  6389. ![*foo* bar][]
  6390. [*foo* bar]: /url "title"
  6391. .
  6392. <p><img src="/url" alt="foo bar" title="title" /></p>
  6393. ````````````````````````````````
  6394. The labels are case-insensitive:
  6395. ```````````````````````````````` example
  6396. ![Foo][]
  6397. [foo]: /url "title"
  6398. .
  6399. <p><img src="/url" alt="Foo" title="title" /></p>
  6400. ````````````````````````````````
  6401. As with reference links, [whitespace] is not allowed
  6402. between the two sets of brackets:
  6403. ```````````````````````````````` example
  6404. ![foo]
  6405. []
  6406. [foo]: /url "title"
  6407. .
  6408. <p><img src="/url" alt="foo" title="title" />
  6409. []</p>
  6410. ````````````````````````````````
  6411. Shortcut:
  6412. ```````````````````````````````` example
  6413. ![foo]
  6414. [foo]: /url "title"
  6415. .
  6416. <p><img src="/url" alt="foo" title="title" /></p>
  6417. ````````````````````````````````
  6418. ```````````````````````````````` example
  6419. ![*foo* bar]
  6420. [*foo* bar]: /url "title"
  6421. .
  6422. <p><img src="/url" alt="foo bar" title="title" /></p>
  6423. ````````````````````````````````
  6424. Note that link labels cannot contain unescaped brackets:
  6425. ```````````````````````````````` example
  6426. ![[foo]]
  6427. [[foo]]: /url "title"
  6428. .
  6429. <p>![[foo]]</p>
  6430. <p>[[foo]]: /url &quot;title&quot;</p>
  6431. ````````````````````````````````
  6432. The link labels are case-insensitive:
  6433. ```````````````````````````````` example
  6434. ![Foo]
  6435. [foo]: /url "title"
  6436. .
  6437. <p><img src="/url" alt="Foo" title="title" /></p>
  6438. ````````````````````````````````
  6439. If you just want a literal `!` followed by bracketed text, you can
  6440. backslash-escape the opening `[`:
  6441. ```````````````````````````````` example
  6442. !\[foo]
  6443. [foo]: /url "title"
  6444. .
  6445. <p>![foo]</p>
  6446. ````````````````````````````````
  6447. If you want a link after a literal `!`, backslash-escape the
  6448. `!`:
  6449. ```````````````````````````````` example
  6450. \![foo]
  6451. [foo]: /url "title"
  6452. .
  6453. <p>!<a href="/url" title="title">foo</a></p>
  6454. ````````````````````````````````
  6455. ## Autolinks
  6456. [Autolink](@)s are absolute URIs and email addresses inside
  6457. `<` and `>`. They are parsed as links, with the URL or email address
  6458. as the link label.
  6459. A [URI autolink](@) consists of `<`, followed by an
  6460. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6461. a link to the URI, with the URI as the link's label.
  6462. An [absolute URI](@),
  6463. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6464. followed by zero or more characters other than ASCII
  6465. [whitespace] and control characters, `<`, and `>`. If
  6466. the URI includes these characters, they must be percent-encoded
  6467. (e.g. `%20` for a space).
  6468. For purposes of this spec, a [scheme](@) is any sequence
  6469. of 2--32 characters beginning with an ASCII letter and followed
  6470. by any combination of ASCII letters, digits, or the symbols plus
  6471. ("+"), period ("."), or hyphen ("-").
  6472. Here are some valid autolinks:
  6473. ```````````````````````````````` example
  6474. <http://foo.bar.baz>
  6475. .
  6476. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6477. ````````````````````````````````
  6478. ```````````````````````````````` example
  6479. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6480. .
  6481. <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>
  6482. ````````````````````````````````
  6483. ```````````````````````````````` example
  6484. <irc://foo.bar:2233/baz>
  6485. .
  6486. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6487. ````````````````````````````````
  6488. Uppercase is also fine:
  6489. ```````````````````````````````` example
  6490. <MAILTO:FOO@BAR.BAZ>
  6491. .
  6492. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6493. ````````````````````````````````
  6494. Note that many strings that count as [absolute URIs] for
  6495. purposes of this spec are not valid URIs, because their
  6496. schemes are not registered or because of other problems
  6497. with their syntax:
  6498. ```````````````````````````````` example
  6499. <a+b+c:d>
  6500. .
  6501. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6502. ````````````````````````````````
  6503. ```````````````````````````````` example
  6504. <made-up-scheme://foo,bar>
  6505. .
  6506. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6507. ````````````````````````````````
  6508. ```````````````````````````````` example
  6509. <http://../>
  6510. .
  6511. <p><a href="http://../">http://../</a></p>
  6512. ````````````````````````````````
  6513. ```````````````````````````````` example
  6514. <localhost:5001/foo>
  6515. .
  6516. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6517. ````````````````````````````````
  6518. Spaces are not allowed in autolinks:
  6519. ```````````````````````````````` example
  6520. <http://foo.bar/baz bim>
  6521. .
  6522. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6523. ````````````````````````````````
  6524. Backslash-escapes do not work inside autolinks:
  6525. ```````````````````````````````` example
  6526. <http://example.com/\[\>
  6527. .
  6528. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6529. ````````````````````````````````
  6530. An [email autolink](@)
  6531. consists of `<`, followed by an [email address],
  6532. followed by `>`. The link's label is the email address,
  6533. and the URL is `mailto:` followed by the email address.
  6534. An [email address](@),
  6535. for these purposes, is anything that matches
  6536. the [non-normative regex from the HTML5
  6537. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6538. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6539. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6540. Examples of email autolinks:
  6541. ```````````````````````````````` example
  6542. <foo@bar.example.com>
  6543. .
  6544. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6545. ````````````````````````````````
  6546. ```````````````````````````````` example
  6547. <foo+special@Bar.baz-bar0.com>
  6548. .
  6549. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6550. ````````````````````````````````
  6551. Backslash-escapes do not work inside email autolinks:
  6552. ```````````````````````````````` example
  6553. <foo\+@bar.example.com>
  6554. .
  6555. <p>&lt;foo+@bar.example.com&gt;</p>
  6556. ````````````````````````````````
  6557. These are not autolinks:
  6558. ```````````````````````````````` example
  6559. <>
  6560. .
  6561. <p>&lt;&gt;</p>
  6562. ````````````````````````````````
  6563. ```````````````````````````````` example
  6564. < http://foo.bar >
  6565. .
  6566. <p>&lt; http://foo.bar &gt;</p>
  6567. ````````````````````````````````
  6568. ```````````````````````````````` example
  6569. <m:abc>
  6570. .
  6571. <p>&lt;m:abc&gt;</p>
  6572. ````````````````````````````````
  6573. ```````````````````````````````` example
  6574. <foo.bar.baz>
  6575. .
  6576. <p>&lt;foo.bar.baz&gt;</p>
  6577. ````````````````````````````````
  6578. ```````````````````````````````` example
  6579. http://example.com
  6580. .
  6581. <p>http://example.com</p>
  6582. ````````````````````````````````
  6583. ```````````````````````````````` example
  6584. foo@bar.example.com
  6585. .
  6586. <p>foo@bar.example.com</p>
  6587. ````````````````````````````````
  6588. ## Raw HTML
  6589. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6590. raw HTML tag and will be rendered in HTML without escaping.
  6591. Tag and attribute names are not limited to current HTML tags,
  6592. so custom tags (and even, say, DocBook tags) may be used.
  6593. Here is the grammar for tags:
  6594. A [tag name](@) consists of an ASCII letter
  6595. followed by zero or more ASCII letters, digits, or
  6596. hyphens (`-`).
  6597. An [attribute](@) consists of [whitespace],
  6598. an [attribute name], and an optional
  6599. [attribute value specification].
  6600. An [attribute name](@)
  6601. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6602. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6603. specification restricted to ASCII. HTML5 is laxer.)
  6604. An [attribute value specification](@)
  6605. consists of optional [whitespace],
  6606. a `=` character, optional [whitespace], and an [attribute
  6607. value].
  6608. An [attribute value](@)
  6609. consists of an [unquoted attribute value],
  6610. a [single-quoted attribute value], or a [double-quoted attribute value].
  6611. An [unquoted attribute value](@)
  6612. is a nonempty string of characters not
  6613. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6614. A [single-quoted attribute value](@)
  6615. consists of `'`, zero or more
  6616. characters not including `'`, and a final `'`.
  6617. A [double-quoted attribute value](@)
  6618. consists of `"`, zero or more
  6619. characters not including `"`, and a final `"`.
  6620. An [open tag](@) consists of a `<` character, a [tag name],
  6621. zero or more [attributes], optional [whitespace], an optional `/`
  6622. character, and a `>` character.
  6623. A [closing tag](@) consists of the string `</`, a
  6624. [tag name], optional [whitespace], and the character `>`.
  6625. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6626. where *text* does not start with `>` or `->`, does not end with `-`,
  6627. and does not contain `--`. (See the
  6628. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6629. A [processing instruction](@)
  6630. consists of the string `<?`, a string
  6631. of characters not including the string `?>`, and the string
  6632. `?>`.
  6633. A [declaration](@) consists of the
  6634. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6635. [whitespace], a string of characters not including the
  6636. character `>`, and the character `>`.
  6637. A [CDATA section](@) consists of
  6638. the string `<![CDATA[`, a string of characters not including the string
  6639. `]]>`, and the string `]]>`.
  6640. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6641. an [HTML comment], a [processing instruction], a [declaration],
  6642. or a [CDATA section].
  6643. Here are some simple open tags:
  6644. ```````````````````````````````` example
  6645. <a><bab><c2c>
  6646. .
  6647. <p><a><bab><c2c></p>
  6648. ````````````````````````````````
  6649. Empty elements:
  6650. ```````````````````````````````` example
  6651. <a/><b2/>
  6652. .
  6653. <p><a/><b2/></p>
  6654. ````````````````````````````````
  6655. [Whitespace] is allowed:
  6656. ```````````````````````````````` example
  6657. <a /><b2
  6658. data="foo" >
  6659. .
  6660. <p><a /><b2
  6661. data="foo" ></p>
  6662. ````````````````````````````````
  6663. With attributes:
  6664. ```````````````````````````````` example
  6665. <a foo="bar" bam = 'baz <em>"</em>'
  6666. _boolean zoop:33=zoop:33 />
  6667. .
  6668. <p><a foo="bar" bam = 'baz <em>"</em>'
  6669. _boolean zoop:33=zoop:33 /></p>
  6670. ````````````````````````````````
  6671. Custom tag names can be used:
  6672. ```````````````````````````````` example
  6673. Foo <responsive-image src="foo.jpg" />
  6674. .
  6675. <p>Foo <responsive-image src="foo.jpg" /></p>
  6676. ````````````````````````````````
  6677. Illegal tag names, not parsed as HTML:
  6678. ```````````````````````````````` example
  6679. <33> <__>
  6680. .
  6681. <p>&lt;33&gt; &lt;__&gt;</p>
  6682. ````````````````````````````````
  6683. Illegal attribute names:
  6684. ```````````````````````````````` example
  6685. <a h*#ref="hi">
  6686. .
  6687. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6688. ````````````````````````````````
  6689. Illegal attribute values:
  6690. ```````````````````````````````` example
  6691. <a href="hi'> <a href=hi'>
  6692. .
  6693. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6694. ````````````````````````````````
  6695. Illegal [whitespace]:
  6696. ```````````````````````````````` example
  6697. < a><
  6698. foo><bar/ >
  6699. <foo bar=baz
  6700. bim!bop />
  6701. .
  6702. <p>&lt; a&gt;&lt;
  6703. foo&gt;&lt;bar/ &gt;
  6704. &lt;foo bar=baz
  6705. bim!bop /&gt;</p>
  6706. ````````````````````````````````
  6707. Missing [whitespace]:
  6708. ```````````````````````````````` example
  6709. <a href='bar'title=title>
  6710. .
  6711. <p>&lt;a href='bar'title=title&gt;</p>
  6712. ````````````````````````````````
  6713. Closing tags:
  6714. ```````````````````````````````` example
  6715. </a></foo >
  6716. .
  6717. <p></a></foo ></p>
  6718. ````````````````````````````````
  6719. Illegal attributes in closing tag:
  6720. ```````````````````````````````` example
  6721. </a href="foo">
  6722. .
  6723. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6724. ````````````````````````````````
  6725. Comments:
  6726. ```````````````````````````````` example
  6727. foo <!-- this is a
  6728. comment - with hyphen -->
  6729. .
  6730. <p>foo <!-- this is a
  6731. comment - with hyphen --></p>
  6732. ````````````````````````````````
  6733. ```````````````````````````````` example
  6734. foo <!-- not a comment -- two hyphens -->
  6735. .
  6736. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6737. ````````````````````````````````
  6738. Not comments:
  6739. ```````````````````````````````` example
  6740. foo <!--> foo -->
  6741. foo <!-- foo--->
  6742. .
  6743. <p>foo &lt;!--&gt; foo --&gt;</p>
  6744. <p>foo &lt;!-- foo---&gt;</p>
  6745. ````````````````````````````````
  6746. Processing instructions:
  6747. ```````````````````````````````` example
  6748. foo <?php echo $a; ?>
  6749. .
  6750. <p>foo <?php echo $a; ?></p>
  6751. ````````````````````````````````
  6752. Declarations:
  6753. ```````````````````````````````` example
  6754. foo <!ELEMENT br EMPTY>
  6755. .
  6756. <p>foo <!ELEMENT br EMPTY></p>
  6757. ````````````````````````````````
  6758. CDATA sections:
  6759. ```````````````````````````````` example
  6760. foo <![CDATA[>&<]]>
  6761. .
  6762. <p>foo <![CDATA[>&<]]></p>
  6763. ````````````````````````````````
  6764. Entity and numeric character references are preserved in HTML
  6765. attributes:
  6766. ```````````````````````````````` example
  6767. foo <a href="&ouml;">
  6768. .
  6769. <p>foo <a href="&ouml;"></p>
  6770. ````````````````````````````````
  6771. Backslash escapes do not work in HTML attributes:
  6772. ```````````````````````````````` example
  6773. foo <a href="\*">
  6774. .
  6775. <p>foo <a href="\*"></p>
  6776. ````````````````````````````````
  6777. ```````````````````````````````` example
  6778. <a href="\"">
  6779. .
  6780. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6781. ````````````````````````````````
  6782. ## Hard line breaks
  6783. A line break (not in a code span or HTML tag) that is preceded
  6784. by two or more spaces and does not occur at the end of a block
  6785. is parsed as a [hard line break](@) (rendered
  6786. in HTML as a `<br />` tag):
  6787. ```````````````````````````````` example
  6788. foo
  6789. baz
  6790. .
  6791. <p>foo<br />
  6792. baz</p>
  6793. ````````````````````````````````
  6794. For a more visible alternative, a backslash before the
  6795. [line ending] may be used instead of two spaces:
  6796. ```````````````````````````````` example
  6797. foo\
  6798. baz
  6799. .
  6800. <p>foo<br />
  6801. baz</p>
  6802. ````````````````````````````````
  6803. More than two spaces can be used:
  6804. ```````````````````````````````` example
  6805. foo
  6806. baz
  6807. .
  6808. <p>foo<br />
  6809. baz</p>
  6810. ````````````````````````````````
  6811. Leading spaces at the beginning of the next line are ignored:
  6812. ```````````````````````````````` example
  6813. foo
  6814. bar
  6815. .
  6816. <p>foo<br />
  6817. bar</p>
  6818. ````````````````````````````````
  6819. ```````````````````````````````` example
  6820. foo\
  6821. bar
  6822. .
  6823. <p>foo<br />
  6824. bar</p>
  6825. ````````````````````````````````
  6826. Line breaks can occur inside emphasis, links, and other constructs
  6827. that allow inline content:
  6828. ```````````````````````````````` example
  6829. *foo
  6830. bar*
  6831. .
  6832. <p><em>foo<br />
  6833. bar</em></p>
  6834. ````````````````````````````````
  6835. ```````````````````````````````` example
  6836. *foo\
  6837. bar*
  6838. .
  6839. <p><em>foo<br />
  6840. bar</em></p>
  6841. ````````````````````````````````
  6842. Line breaks do not occur inside code spans
  6843. ```````````````````````````````` example
  6844. `code
  6845. span`
  6846. .
  6847. <p><code>code span</code></p>
  6848. ````````````````````````````````
  6849. ```````````````````````````````` example
  6850. `code\
  6851. span`
  6852. .
  6853. <p><code>code\ span</code></p>
  6854. ````````````````````````````````
  6855. or HTML tags:
  6856. ```````````````````````````````` example
  6857. <a href="foo
  6858. bar">
  6859. .
  6860. <p><a href="foo
  6861. bar"></p>
  6862. ````````````````````````````````
  6863. ```````````````````````````````` example
  6864. <a href="foo\
  6865. bar">
  6866. .
  6867. <p><a href="foo\
  6868. bar"></p>
  6869. ````````````````````````````````
  6870. Hard line breaks are for separating inline content within a block.
  6871. Neither syntax for hard line breaks works at the end of a paragraph or
  6872. other block element:
  6873. ```````````````````````````````` example
  6874. foo\
  6875. .
  6876. <p>foo\</p>
  6877. ````````````````````````````````
  6878. ```````````````````````````````` example
  6879. foo
  6880. .
  6881. <p>foo</p>
  6882. ````````````````````````````````
  6883. ```````````````````````````````` example
  6884. ### foo\
  6885. .
  6886. <h3>foo\</h3>
  6887. ````````````````````````````````
  6888. ```````````````````````````````` example
  6889. ### foo
  6890. .
  6891. <h3>foo</h3>
  6892. ````````````````````````````````
  6893. ## Soft line breaks
  6894. A regular line break (not in a code span or HTML tag) that is not
  6895. preceded by two or more spaces or a backslash is parsed as a
  6896. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6897. [line ending] or as a space. The result will be the same in
  6898. browsers. In the examples here, a [line ending] will be used.)
  6899. ```````````````````````````````` example
  6900. foo
  6901. baz
  6902. .
  6903. <p>foo
  6904. baz</p>
  6905. ````````````````````````````````
  6906. Spaces at the end of the line and beginning of the next line are
  6907. removed:
  6908. ```````````````````````````````` example
  6909. foo
  6910. baz
  6911. .
  6912. <p>foo
  6913. baz</p>
  6914. ````````````````````````````````
  6915. A conforming parser may render a soft line break in HTML either as a
  6916. line break or as a space.
  6917. A renderer may also provide an option to render soft line breaks
  6918. as hard line breaks.
  6919. ## Textual content
  6920. Any characters not given an interpretation by the above rules will
  6921. be parsed as plain textual content.
  6922. ```````````````````````````````` example
  6923. hello $.;'there
  6924. .
  6925. <p>hello $.;'there</p>
  6926. ````````````````````````````````
  6927. ```````````````````````````````` example
  6928. Foo χρῆν
  6929. .
  6930. <p>Foo χρῆν</p>
  6931. ````````````````````````````````
  6932. Internal spaces are preserved verbatim:
  6933. ```````````````````````````````` example
  6934. Multiple spaces
  6935. .
  6936. <p>Multiple spaces</p>
  6937. ````````````````````````````````
  6938. <!-- END TESTS -->
  6939. # Appendix: A parsing strategy
  6940. In this appendix we describe some features of the parsing strategy
  6941. used in the CommonMark reference implementations.
  6942. ## Overview
  6943. Parsing has two phases:
  6944. 1. In the first phase, lines of input are consumed and the block
  6945. structure of the document---its division into paragraphs, block quotes,
  6946. list items, and so on---is constructed. Text is assigned to these
  6947. blocks but not parsed. Link reference definitions are parsed and a
  6948. map of links is constructed.
  6949. 2. In the second phase, the raw text contents of paragraphs and headings
  6950. are parsed into sequences of Markdown inline elements (strings,
  6951. code spans, links, emphasis, and so on), using the map of link
  6952. references constructed in phase 1.
  6953. At each point in processing, the document is represented as a tree of
  6954. **blocks**. The root of the tree is a `document` block. The `document`
  6955. may have any number of other blocks as **children**. These children
  6956. may, in turn, have other blocks as children. The last child of a block
  6957. is normally considered **open**, meaning that subsequent lines of input
  6958. can alter its contents. (Blocks that are not open are **closed**.)
  6959. Here, for example, is a possible document tree, with the open blocks
  6960. marked by arrows:
  6961. ``` tree
  6962. -> document
  6963. -> block_quote
  6964. paragraph
  6965. "Lorem ipsum dolor\nsit amet."
  6966. -> list (type=bullet tight=true bullet_char=-)
  6967. list_item
  6968. paragraph
  6969. "Qui *quodsi iracundia*"
  6970. -> list_item
  6971. -> paragraph
  6972. "aliquando id"
  6973. ```
  6974. ## Phase 1: block structure
  6975. Each line that is processed has an effect on this tree. The line is
  6976. analyzed and, depending on its contents, the document may be altered
  6977. in one or more of the following ways:
  6978. 1. One or more open blocks may be closed.
  6979. 2. One or more new blocks may be created as children of the
  6980. last open block.
  6981. 3. Text may be added to the last (deepest) open block remaining
  6982. on the tree.
  6983. Once a line has been incorporated into the tree in this way,
  6984. it can be discarded, so input can be read in a stream.
  6985. For each line, we follow this procedure:
  6986. 1. First we iterate through the open blocks, starting with the
  6987. root document, and descending through last children down to the last
  6988. open block. Each block imposes a condition that the line must satisfy
  6989. if the block is to remain open. For example, a block quote requires a
  6990. `>` character. A paragraph requires a non-blank line.
  6991. In this phase we may match all or just some of the open
  6992. blocks. But we cannot close unmatched blocks yet, because we may have a
  6993. [lazy continuation line].
  6994. 2. Next, after consuming the continuation markers for existing
  6995. blocks, we look for new block starts (e.g. `>` for a block quote).
  6996. If we encounter a new block start, we close any blocks unmatched
  6997. in step 1 before creating the new block as a child of the last
  6998. matched block.
  6999. 3. Finally, we look at the remainder of the line (after block
  7000. markers like `>`, list markers, and indentation have been consumed).
  7001. This is text that can be incorporated into the last open
  7002. block (a paragraph, code block, heading, or raw HTML).
  7003. Setext headings are formed when we see a line of a paragraph
  7004. that is a [setext heading underline].
  7005. Reference link definitions are detected when a paragraph is closed;
  7006. the accumulated text lines are parsed to see if they begin with
  7007. one or more reference link definitions. Any remainder becomes a
  7008. normal paragraph.
  7009. We can see how this works by considering how the tree above is
  7010. generated by four lines of Markdown:
  7011. ``` markdown
  7012. > Lorem ipsum dolor
  7013. sit amet.
  7014. > - Qui *quodsi iracundia*
  7015. > - aliquando id
  7016. ```
  7017. At the outset, our document model is just
  7018. ``` tree
  7019. -> document
  7020. ```
  7021. The first line of our text,
  7022. ``` markdown
  7023. > Lorem ipsum dolor
  7024. ```
  7025. causes a `block_quote` block to be created as a child of our
  7026. open `document` block, and a `paragraph` block as a child of
  7027. the `block_quote`. Then the text is added to the last open
  7028. block, the `paragraph`:
  7029. ``` tree
  7030. -> document
  7031. -> block_quote
  7032. -> paragraph
  7033. "Lorem ipsum dolor"
  7034. ```
  7035. The next line,
  7036. ``` markdown
  7037. sit amet.
  7038. ```
  7039. is a "lazy continuation" of the open `paragraph`, so it gets added
  7040. to the paragraph's text:
  7041. ``` tree
  7042. -> document
  7043. -> block_quote
  7044. -> paragraph
  7045. "Lorem ipsum dolor\nsit amet."
  7046. ```
  7047. The third line,
  7048. ``` markdown
  7049. > - Qui *quodsi iracundia*
  7050. ```
  7051. causes the `paragraph` block to be closed, and a new `list` block
  7052. opened as a child of the `block_quote`. A `list_item` is also
  7053. added as a child of the `list`, and a `paragraph` as a child of
  7054. the `list_item`. The text is then added to the new `paragraph`:
  7055. ``` tree
  7056. -> document
  7057. -> block_quote
  7058. paragraph
  7059. "Lorem ipsum dolor\nsit amet."
  7060. -> list (type=bullet tight=true bullet_char=-)
  7061. -> list_item
  7062. -> paragraph
  7063. "Qui *quodsi iracundia*"
  7064. ```
  7065. The fourth line,
  7066. ``` markdown
  7067. > - aliquando id
  7068. ```
  7069. causes the `list_item` (and its child the `paragraph`) to be closed,
  7070. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7071. is added as a child of the new `list_item`, to contain the text.
  7072. We thus obtain the final tree:
  7073. ``` tree
  7074. -> document
  7075. -> block_quote
  7076. paragraph
  7077. "Lorem ipsum dolor\nsit amet."
  7078. -> list (type=bullet tight=true bullet_char=-)
  7079. list_item
  7080. paragraph
  7081. "Qui *quodsi iracundia*"
  7082. -> list_item
  7083. -> paragraph
  7084. "aliquando id"
  7085. ```
  7086. ## Phase 2: inline structure
  7087. Once all of the input has been parsed, all open blocks are closed.
  7088. We then "walk the tree," visiting every node, and parse raw
  7089. string contents of paragraphs and headings as inlines. At this
  7090. point we have seen all the link reference definitions, so we can
  7091. resolve reference links as we go.
  7092. ``` tree
  7093. document
  7094. block_quote
  7095. paragraph
  7096. str "Lorem ipsum dolor"
  7097. softbreak
  7098. str "sit amet."
  7099. list (type=bullet tight=true bullet_char=-)
  7100. list_item
  7101. paragraph
  7102. str "Qui "
  7103. emph
  7104. str "quodsi iracundia"
  7105. list_item
  7106. paragraph
  7107. str "aliquando id"
  7108. ```
  7109. Notice how the [line ending] in the first paragraph has
  7110. been parsed as a `softbreak`, and the asterisks in the first list item
  7111. have become an `emph`.
  7112. ### An algorithm for parsing nested emphasis and links
  7113. By far the trickiest part of inline parsing is handling emphasis,
  7114. strong emphasis, links, and images. This is done using the following
  7115. algorithm.
  7116. When we're parsing inlines and we hit either
  7117. - a run of `*` or `_` characters, or
  7118. - a `[` or `![`
  7119. we insert a text node with these symbols as its literal content, and we
  7120. add a pointer to this text node to the [delimiter stack](@).
  7121. The [delimiter stack] is a doubly linked list. Each
  7122. element contains a pointer to a text node, plus information about
  7123. - the type of delimiter (`[`, `![`, `*`, `_`)
  7124. - the number of delimiters,
  7125. - whether the delimiter is "active" (all are active to start), and
  7126. - whether the delimiter is a potential opener, a potential closer,
  7127. or both (which depends on what sort of characters precede
  7128. and follow the delimiters).
  7129. When we hit a `]` character, we call the *look for link or image*
  7130. procedure (see below).
  7131. When we hit the end of the input, we call the *process emphasis*
  7132. procedure (see below), with `stack_bottom` = NULL.
  7133. #### *look for link or image*
  7134. Starting at the top of the delimiter stack, we look backwards
  7135. through the stack for an opening `[` or `![` delimiter.
  7136. - If we don't find one, we return a literal text node `]`.
  7137. - If we do find one, but it's not *active*, we remove the inactive
  7138. delimiter from the stack, and return a literal text node `]`.
  7139. - If we find one and it's active, then we parse ahead to see if
  7140. we have an inline link/image, reference link/image, compact reference
  7141. link/image, or shortcut reference link/image.
  7142. + If we don't, then we remove the opening delimiter from the
  7143. delimiter stack and return a literal text node `]`.
  7144. + If we do, then
  7145. * We return a link or image node whose children are the inlines
  7146. after the text node pointed to by the opening delimiter.
  7147. * We run *process emphasis* on these inlines, with the `[` opener
  7148. as `stack_bottom`.
  7149. * We remove the opening delimiter.
  7150. * If we have a link (and not an image), we also set all
  7151. `[` delimiters before the opening delimiter to *inactive*. (This
  7152. will prevent us from getting links within links.)
  7153. #### *process emphasis*
  7154. Parameter `stack_bottom` sets a lower bound to how far we
  7155. descend in the [delimiter stack]. If it is NULL, we can
  7156. go all the way to the bottom. Otherwise, we stop before
  7157. visiting `stack_bottom`.
  7158. Let `current_position` point to the element on the [delimiter stack]
  7159. just above `stack_bottom` (or the first element if `stack_bottom`
  7160. is NULL).
  7161. We keep track of the `openers_bottom` for each delimiter
  7162. type (`*`, `_`) and each length of the closing delimiter run
  7163. (modulo 3). Initialize this to `stack_bottom`.
  7164. Then we repeat the following until we run out of potential
  7165. closers:
  7166. - Move `current_position` forward in the delimiter stack (if needed)
  7167. until we find the first potential closer with delimiter `*` or `_`.
  7168. (This will be the potential closer closest
  7169. to the beginning of the input -- the first one in parse order.)
  7170. - Now, look back in the stack (staying above `stack_bottom` and
  7171. the `openers_bottom` for this delimiter type) for the
  7172. first matching potential opener ("matching" means same delimiter).
  7173. - If one is found:
  7174. + Figure out whether we have emphasis or strong emphasis:
  7175. if both closer and opener spans have length >= 2, we have
  7176. strong, otherwise regular.
  7177. + Insert an emph or strong emph node accordingly, after
  7178. the text node corresponding to the opener.
  7179. + Remove any delimiters between the opener and closer from
  7180. the delimiter stack.
  7181. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7182. from the opening and closing text nodes. If they become empty
  7183. as a result, remove them and remove the corresponding element
  7184. of the delimiter stack. If the closing node is removed, reset
  7185. `current_position` to the next element in the stack.
  7186. - If none is found:
  7187. + Set `openers_bottom` to the element before `current_position`.
  7188. (We know that there are no openers for this kind of closer up to and
  7189. including this point, so this puts a lower bound on future searches.)
  7190. + If the closer at `current_position` is not a potential opener,
  7191. remove it from the delimiter stack (since we know it can't
  7192. be a closer either).
  7193. + Advance `current_position` to the next element in the stack.
  7194. After we're done, we remove all delimiters above `stack_bottom` from the
  7195. delimiter stack.