aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: b3f849344f0e6ede36c349700f0f947d8f026dd9 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a github wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  252. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  253. A [punctuation character](@) is an [ASCII
  254. punctuation character] or anything in
  255. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  256. ## Tabs
  257. Tabs in lines are not expanded to [spaces]. However,
  258. in contexts where whitespace helps to define block structure,
  259. tabs behave as if they were replaced by spaces with a tab stop
  260. of 4 characters.
  261. Thus, for example, a tab can be used instead of four spaces
  262. in an indented code block. (Note, however, that internal
  263. tabs are passed through as literal tabs, not expanded to
  264. spaces.)
  265. ```````````````````````````````` example
  266. →foo→baz→→bim
  267. .
  268. <pre><code>foo→baz→→bim
  269. </code></pre>
  270. ````````````````````````````````
  271. ```````````````````````````````` example
  272. →foo→baz→→bim
  273. .
  274. <pre><code>foo→baz→→bim
  275. </code></pre>
  276. ````````````````````````````````
  277. ```````````````````````````````` example
  278. a→a
  279. ὐ→a
  280. .
  281. <pre><code>a→a
  282. ὐ→a
  283. </code></pre>
  284. ````````````````````````````````
  285. In the following example, a continuation paragraph of a list
  286. item is indented with a tab; this has exactly the same effect
  287. as indentation with four spaces would:
  288. ```````````````````````````````` example
  289. - foo
  290. →bar
  291. .
  292. <ul>
  293. <li>
  294. <p>foo</p>
  295. <p>bar</p>
  296. </li>
  297. </ul>
  298. ````````````````````````````````
  299. ```````````````````````````````` example
  300. - foo
  301. →→bar
  302. .
  303. <ul>
  304. <li>
  305. <p>foo</p>
  306. <pre><code> bar
  307. </code></pre>
  308. </li>
  309. </ul>
  310. ````````````````````````````````
  311. Normally the `>` that begins a block quote may be followed
  312. optionally by a space, which is not considered part of the
  313. content. In the following case `>` is followed by a tab,
  314. which is treated as if it were expanded into three spaces.
  315. Since one of these spaces is considered part of the
  316. delimiter, `foo` is considered to be indented six spaces
  317. inside the block quote context, so we get an indented
  318. code block starting with two spaces.
  319. ```````````````````````````````` example
  320. >→→foo
  321. .
  322. <blockquote>
  323. <pre><code> foo
  324. </code></pre>
  325. </blockquote>
  326. ````````````````````````````````
  327. ```````````````````````````````` example
  328. -→→foo
  329. .
  330. <ul>
  331. <li>
  332. <pre><code> foo
  333. </code></pre>
  334. </li>
  335. </ul>
  336. ````````````````````````````````
  337. ```````````````````````````````` example
  338. foo
  339. →bar
  340. .
  341. <pre><code>foo
  342. bar
  343. </code></pre>
  344. ````````````````````````````````
  345. ```````````````````````````````` example
  346. - foo
  347. - bar
  348. → - baz
  349. .
  350. <ul>
  351. <li>foo
  352. <ul>
  353. <li>bar
  354. <ul>
  355. <li>baz</li>
  356. </ul>
  357. </li>
  358. </ul>
  359. </li>
  360. </ul>
  361. ````````````````````````````````
  362. ```````````````````````````````` example
  363. #→Foo
  364. .
  365. <h1>Foo</h1>
  366. ````````````````````````````````
  367. ```````````````````````````````` example
  368. *→*→*→
  369. .
  370. <hr />
  371. ````````````````````````````````
  372. ## Insecure characters
  373. For security reasons, the Unicode character `U+0000` must be replaced
  374. with the REPLACEMENT CHARACTER (`U+FFFD`).
  375. # Blocks and inlines
  376. We can think of a document as a sequence of
  377. [blocks](@)---structural elements like paragraphs, block
  378. quotations, lists, headings, rules, and code blocks. Some blocks (like
  379. block quotes and list items) contain other blocks; others (like
  380. headings and paragraphs) contain [inline](@) content---text,
  381. links, emphasized text, images, code spans, and so on.
  382. ## Precedence
  383. Indicators of block structure always take precedence over indicators
  384. of inline structure. So, for example, the following is a list with
  385. two items, not a list with one item containing a code span:
  386. ```````````````````````````````` example
  387. - `one
  388. - two`
  389. .
  390. <ul>
  391. <li>`one</li>
  392. <li>two`</li>
  393. </ul>
  394. ````````````````````````````````
  395. This means that parsing can proceed in two steps: first, the block
  396. structure of the document can be discerned; second, text lines inside
  397. paragraphs, headings, and other block constructs can be parsed for inline
  398. structure. The second step requires information about link reference
  399. definitions that will be available only at the end of the first
  400. step. Note that the first step requires processing lines in sequence,
  401. but the second can be parallelized, since the inline parsing of
  402. one block element does not affect the inline parsing of any other.
  403. ## Container blocks and leaf blocks
  404. We can divide blocks into two types:
  405. [container block](@)s,
  406. which can contain other blocks, and [leaf block](@)s,
  407. which cannot.
  408. # Leaf blocks
  409. This section describes the different kinds of leaf block that make up a
  410. Markdown document.
  411. ## Thematic breaks
  412. A line consisting of 0-3 spaces of indentation, followed by a sequence
  413. of three or more matching `-`, `_`, or `*` characters, each followed
  414. optionally by any number of spaces, forms a
  415. [thematic break](@).
  416. ```````````````````````````````` example
  417. ***
  418. ---
  419. ___
  420. .
  421. <hr />
  422. <hr />
  423. <hr />
  424. ````````````````````````````````
  425. Wrong characters:
  426. ```````````````````````````````` example
  427. +++
  428. .
  429. <p>+++</p>
  430. ````````````````````````````````
  431. ```````````````````````````````` example
  432. ===
  433. .
  434. <p>===</p>
  435. ````````````````````````````````
  436. Not enough characters:
  437. ```````````````````````````````` example
  438. --
  439. **
  440. __
  441. .
  442. <p>--
  443. **
  444. __</p>
  445. ````````````````````````````````
  446. One to three spaces indent are allowed:
  447. ```````````````````````````````` example
  448. ***
  449. ***
  450. ***
  451. .
  452. <hr />
  453. <hr />
  454. <hr />
  455. ````````````````````````````````
  456. Four spaces is too many:
  457. ```````````````````````````````` example
  458. ***
  459. .
  460. <pre><code>***
  461. </code></pre>
  462. ````````````````````````````````
  463. ```````````````````````````````` example
  464. Foo
  465. ***
  466. .
  467. <p>Foo
  468. ***</p>
  469. ````````````````````````````````
  470. More than three characters may be used:
  471. ```````````````````````````````` example
  472. _____________________________________
  473. .
  474. <hr />
  475. ````````````````````````````````
  476. Spaces are allowed between the characters:
  477. ```````````````````````````````` example
  478. - - -
  479. .
  480. <hr />
  481. ````````````````````````````````
  482. ```````````````````````````````` example
  483. ** * ** * ** * **
  484. .
  485. <hr />
  486. ````````````````````````````````
  487. ```````````````````````````````` example
  488. - - - -
  489. .
  490. <hr />
  491. ````````````````````````````````
  492. Spaces are allowed at the end:
  493. ```````````````````````````````` example
  494. - - - -
  495. .
  496. <hr />
  497. ````````````````````````````````
  498. However, no other characters may occur in the line:
  499. ```````````````````````````````` example
  500. _ _ _ _ a
  501. a------
  502. ---a---
  503. .
  504. <p>_ _ _ _ a</p>
  505. <p>a------</p>
  506. <p>---a---</p>
  507. ````````````````````````````````
  508. It is required that all of the [non-whitespace characters] be the same.
  509. So, this is not a thematic break:
  510. ```````````````````````````````` example
  511. *-*
  512. .
  513. <p><em>-</em></p>
  514. ````````````````````````````````
  515. Thematic breaks do not need blank lines before or after:
  516. ```````````````````````````````` example
  517. - foo
  518. ***
  519. - bar
  520. .
  521. <ul>
  522. <li>foo</li>
  523. </ul>
  524. <hr />
  525. <ul>
  526. <li>bar</li>
  527. </ul>
  528. ````````````````````````````````
  529. Thematic breaks can interrupt a paragraph:
  530. ```````````````````````````````` example
  531. Foo
  532. ***
  533. bar
  534. .
  535. <p>Foo</p>
  536. <hr />
  537. <p>bar</p>
  538. ````````````````````````````````
  539. If a line of dashes that meets the above conditions for being a
  540. thematic break could also be interpreted as the underline of a [setext
  541. heading], the interpretation as a
  542. [setext heading] takes precedence. Thus, for example,
  543. this is a setext heading, not a paragraph followed by a thematic break:
  544. ```````````````````````````````` example
  545. Foo
  546. ---
  547. bar
  548. .
  549. <h2>Foo</h2>
  550. <p>bar</p>
  551. ````````````````````````````````
  552. When both a thematic break and a list item are possible
  553. interpretations of a line, the thematic break takes precedence:
  554. ```````````````````````````````` example
  555. * Foo
  556. * * *
  557. * Bar
  558. .
  559. <ul>
  560. <li>Foo</li>
  561. </ul>
  562. <hr />
  563. <ul>
  564. <li>Bar</li>
  565. </ul>
  566. ````````````````````````````````
  567. If you want a thematic break in a list item, use a different bullet:
  568. ```````````````````````````````` example
  569. - Foo
  570. - * * *
  571. .
  572. <ul>
  573. <li>Foo</li>
  574. <li>
  575. <hr />
  576. </li>
  577. </ul>
  578. ````````````````````````````````
  579. ## ATX headings
  580. An [ATX heading](@)
  581. consists of a string of characters, parsed as inline content, between an
  582. opening sequence of 1--6 unescaped `#` characters and an optional
  583. closing sequence of any number of unescaped `#` characters.
  584. The opening sequence of `#` characters must be followed by a
  585. [space] or by the end of line. The optional closing sequence of `#`s must be
  586. preceded by a [space] and may be followed by spaces only. The opening
  587. `#` character may be indented 0-3 spaces. The raw contents of the
  588. heading are stripped of leading and trailing spaces before being parsed
  589. as inline content. The heading level is equal to the number of `#`
  590. characters in the opening sequence.
  591. Simple headings:
  592. ```````````````````````````````` example
  593. # foo
  594. ## foo
  595. ### foo
  596. #### foo
  597. ##### foo
  598. ###### foo
  599. .
  600. <h1>foo</h1>
  601. <h2>foo</h2>
  602. <h3>foo</h3>
  603. <h4>foo</h4>
  604. <h5>foo</h5>
  605. <h6>foo</h6>
  606. ````````````````````````````````
  607. More than six `#` characters is not a heading:
  608. ```````````````````````````````` example
  609. ####### foo
  610. .
  611. <p>####### foo</p>
  612. ````````````````````````````````
  613. At least one space is required between the `#` characters and the
  614. heading's contents, unless the heading is empty. Note that many
  615. implementations currently do not require the space. However, the
  616. space was required by the
  617. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  618. and it helps prevent things like the following from being parsed as
  619. headings:
  620. ```````````````````````````````` example
  621. #5 bolt
  622. #hashtag
  623. .
  624. <p>#5 bolt</p>
  625. <p>#hashtag</p>
  626. ````````````````````````````````
  627. This is not a heading, because the first `#` is escaped:
  628. ```````````````````````````````` example
  629. \## foo
  630. .
  631. <p>## foo</p>
  632. ````````````````````````````````
  633. Contents are parsed as inlines:
  634. ```````````````````````````````` example
  635. # foo *bar* \*baz\*
  636. .
  637. <h1>foo <em>bar</em> *baz*</h1>
  638. ````````````````````````````````
  639. Leading and trailing blanks are ignored in parsing inline content:
  640. ```````````````````````````````` example
  641. # foo
  642. .
  643. <h1>foo</h1>
  644. ````````````````````````````````
  645. One to three spaces indentation are allowed:
  646. ```````````````````````````````` example
  647. ### foo
  648. ## foo
  649. # foo
  650. .
  651. <h3>foo</h3>
  652. <h2>foo</h2>
  653. <h1>foo</h1>
  654. ````````````````````````````````
  655. Four spaces are too much:
  656. ```````````````````````````````` example
  657. # foo
  658. .
  659. <pre><code># foo
  660. </code></pre>
  661. ````````````````````````````````
  662. ```````````````````````````````` example
  663. foo
  664. # bar
  665. .
  666. <p>foo
  667. # bar</p>
  668. ````````````````````````````````
  669. A closing sequence of `#` characters is optional:
  670. ```````````````````````````````` example
  671. ## foo ##
  672. ### bar ###
  673. .
  674. <h2>foo</h2>
  675. <h3>bar</h3>
  676. ````````````````````````````````
  677. It need not be the same length as the opening sequence:
  678. ```````````````````````````````` example
  679. # foo ##################################
  680. ##### foo ##
  681. .
  682. <h1>foo</h1>
  683. <h5>foo</h5>
  684. ````````````````````````````````
  685. Spaces are allowed after the closing sequence:
  686. ```````````````````````````````` example
  687. ### foo ###
  688. .
  689. <h3>foo</h3>
  690. ````````````````````````````````
  691. A sequence of `#` characters with anything but [spaces] following it
  692. is not a closing sequence, but counts as part of the contents of the
  693. heading:
  694. ```````````````````````````````` example
  695. ### foo ### b
  696. .
  697. <h3>foo ### b</h3>
  698. ````````````````````````````````
  699. The closing sequence must be preceded by a space:
  700. ```````````````````````````````` example
  701. # foo#
  702. .
  703. <h1>foo#</h1>
  704. ````````````````````````````````
  705. Backslash-escaped `#` characters do not count as part
  706. of the closing sequence:
  707. ```````````````````````````````` example
  708. ### foo \###
  709. ## foo #\##
  710. # foo \#
  711. .
  712. <h3>foo ###</h3>
  713. <h2>foo ###</h2>
  714. <h1>foo #</h1>
  715. ````````````````````````````````
  716. ATX headings need not be separated from surrounding content by blank
  717. lines, and they can interrupt paragraphs:
  718. ```````````````````````````````` example
  719. ****
  720. ## foo
  721. ****
  722. .
  723. <hr />
  724. <h2>foo</h2>
  725. <hr />
  726. ````````````````````````````````
  727. ```````````````````````````````` example
  728. Foo bar
  729. # baz
  730. Bar foo
  731. .
  732. <p>Foo bar</p>
  733. <h1>baz</h1>
  734. <p>Bar foo</p>
  735. ````````````````````````````````
  736. ATX headings can be empty:
  737. ```````````````````````````````` example
  738. ##
  739. #
  740. ### ###
  741. .
  742. <h2></h2>
  743. <h1></h1>
  744. <h3></h3>
  745. ````````````````````````````````
  746. ## Setext headings
  747. A [setext heading](@) consists of one or more
  748. lines of text, each containing at least one [non-whitespace
  749. character], with no more than 3 spaces indentation, followed by
  750. a [setext heading underline]. The lines of text must be such
  751. that, were they not followed by the setext heading underline,
  752. they would be interpreted as a paragraph: they cannot be
  753. interpretable as a [code fence], [ATX heading][ATX headings],
  754. [block quote][block quotes], [thematic break][thematic breaks],
  755. [list item][list items], or [HTML block][HTML blocks].
  756. A [setext heading underline](@) is a sequence of
  757. `=` characters or a sequence of `-` characters, with no more than 3
  758. spaces indentation and any number of trailing spaces. If a line
  759. containing a single `-` can be interpreted as an
  760. empty [list items], it should be interpreted this way
  761. and not as a [setext heading underline].
  762. The heading is a level 1 heading if `=` characters are used in
  763. the [setext heading underline], and a level 2 heading if `-`
  764. characters are used. The contents of the heading are the result
  765. of parsing the preceding lines of text as CommonMark inline
  766. content.
  767. In general, a setext heading need not be preceded or followed by a
  768. blank line. However, it cannot interrupt a paragraph, so when a
  769. setext heading comes after a paragraph, a blank line is needed between
  770. them.
  771. Simple examples:
  772. ```````````````````````````````` example
  773. Foo *bar*
  774. =========
  775. Foo *bar*
  776. ---------
  777. .
  778. <h1>Foo <em>bar</em></h1>
  779. <h2>Foo <em>bar</em></h2>
  780. ````````````````````````````````
  781. The content of the header may span more than one line:
  782. ```````````````````````````````` example
  783. Foo *bar
  784. baz*
  785. ====
  786. .
  787. <h1>Foo <em>bar
  788. baz</em></h1>
  789. ````````````````````````````````
  790. The underlining can be any length:
  791. ```````````````````````````````` example
  792. Foo
  793. -------------------------
  794. Foo
  795. =
  796. .
  797. <h2>Foo</h2>
  798. <h1>Foo</h1>
  799. ````````````````````````````````
  800. The heading content can be indented up to three spaces, and need
  801. not line up with the underlining:
  802. ```````````````````````````````` example
  803. Foo
  804. ---
  805. Foo
  806. -----
  807. Foo
  808. ===
  809. .
  810. <h2>Foo</h2>
  811. <h2>Foo</h2>
  812. <h1>Foo</h1>
  813. ````````````````````````````````
  814. Four spaces indent is too much:
  815. ```````````````````````````````` example
  816. Foo
  817. ---
  818. Foo
  819. ---
  820. .
  821. <pre><code>Foo
  822. ---
  823. Foo
  824. </code></pre>
  825. <hr />
  826. ````````````````````````````````
  827. The setext heading underline can be indented up to three spaces, and
  828. may have trailing spaces:
  829. ```````````````````````````````` example
  830. Foo
  831. ----
  832. .
  833. <h2>Foo</h2>
  834. ````````````````````````````````
  835. Four spaces is too much:
  836. ```````````````````````````````` example
  837. Foo
  838. ---
  839. .
  840. <p>Foo
  841. ---</p>
  842. ````````````````````````````````
  843. The setext heading underline cannot contain internal spaces:
  844. ```````````````````````````````` example
  845. Foo
  846. = =
  847. Foo
  848. --- -
  849. .
  850. <p>Foo
  851. = =</p>
  852. <p>Foo</p>
  853. <hr />
  854. ````````````````````````````````
  855. Trailing spaces in the content line do not cause a line break:
  856. ```````````````````````````````` example
  857. Foo
  858. -----
  859. .
  860. <h2>Foo</h2>
  861. ````````````````````````````````
  862. Nor does a backslash at the end:
  863. ```````````````````````````````` example
  864. Foo\
  865. ----
  866. .
  867. <h2>Foo\</h2>
  868. ````````````````````````````````
  869. Since indicators of block structure take precedence over
  870. indicators of inline structure, the following are setext headings:
  871. ```````````````````````````````` example
  872. `Foo
  873. ----
  874. `
  875. <a title="a lot
  876. ---
  877. of dashes"/>
  878. .
  879. <h2>`Foo</h2>
  880. <p>`</p>
  881. <h2>&lt;a title=&quot;a lot</h2>
  882. <p>of dashes&quot;/&gt;</p>
  883. ````````````````````````````````
  884. The setext heading underline cannot be a [lazy continuation
  885. line] in a list item or block quote:
  886. ```````````````````````````````` example
  887. > Foo
  888. ---
  889. .
  890. <blockquote>
  891. <p>Foo</p>
  892. </blockquote>
  893. <hr />
  894. ````````````````````````````````
  895. ```````````````````````````````` example
  896. > foo
  897. bar
  898. ===
  899. .
  900. <blockquote>
  901. <p>foo
  902. bar
  903. ===</p>
  904. </blockquote>
  905. ````````````````````````````````
  906. ```````````````````````````````` example
  907. - Foo
  908. ---
  909. .
  910. <ul>
  911. <li>Foo</li>
  912. </ul>
  913. <hr />
  914. ````````````````````````````````
  915. A blank line is needed between a paragraph and a following
  916. setext heading, since otherwise the paragraph becomes part
  917. of the heading's content:
  918. ```````````````````````````````` example
  919. Foo
  920. Bar
  921. ---
  922. .
  923. <h2>Foo
  924. Bar</h2>
  925. ````````````````````````````````
  926. But in general a blank line is not required before or after
  927. setext headings:
  928. ```````````````````````````````` example
  929. ---
  930. Foo
  931. ---
  932. Bar
  933. ---
  934. Baz
  935. .
  936. <hr />
  937. <h2>Foo</h2>
  938. <h2>Bar</h2>
  939. <p>Baz</p>
  940. ````````````````````````````````
  941. Setext headings cannot be empty:
  942. ```````````````````````````````` example
  943. ====
  944. .
  945. <p>====</p>
  946. ````````````````````````````````
  947. Setext heading text lines must not be interpretable as block
  948. constructs other than paragraphs. So, the line of dashes
  949. in these examples gets interpreted as a thematic break:
  950. ```````````````````````````````` example
  951. ---
  952. ---
  953. .
  954. <hr />
  955. <hr />
  956. ````````````````````````````````
  957. ```````````````````````````````` example
  958. - foo
  959. -----
  960. .
  961. <ul>
  962. <li>foo</li>
  963. </ul>
  964. <hr />
  965. ````````````````````````````````
  966. ```````````````````````````````` example
  967. foo
  968. ---
  969. .
  970. <pre><code>foo
  971. </code></pre>
  972. <hr />
  973. ````````````````````````````````
  974. ```````````````````````````````` example
  975. > foo
  976. -----
  977. .
  978. <blockquote>
  979. <p>foo</p>
  980. </blockquote>
  981. <hr />
  982. ````````````````````````````````
  983. If you want a heading with `> foo` as its literal text, you can
  984. use backslash escapes:
  985. ```````````````````````````````` example
  986. \> foo
  987. ------
  988. .
  989. <h2>&gt; foo</h2>
  990. ````````````````````````````````
  991. **Compatibility note:** Most existing Markdown implementations
  992. do not allow the text of setext headings to span multiple lines.
  993. But there is no consensus about how to interpret
  994. ``` markdown
  995. Foo
  996. bar
  997. ---
  998. baz
  999. ```
  1000. One can find four different interpretations:
  1001. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1002. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1003. 3. paragraph "Foo bar --- baz"
  1004. 4. heading "Foo bar", paragraph "baz"
  1005. We find interpretation 4 most natural, and interpretation 4
  1006. increases the expressive power of CommonMark, by allowing
  1007. multiline headings. Authors who want interpretation 1 can
  1008. put a blank line after the first paragraph:
  1009. ```````````````````````````````` example
  1010. Foo
  1011. bar
  1012. ---
  1013. baz
  1014. .
  1015. <p>Foo</p>
  1016. <h2>bar</h2>
  1017. <p>baz</p>
  1018. ````````````````````````````````
  1019. Authors who want interpretation 2 can put blank lines around
  1020. the thematic break,
  1021. ```````````````````````````````` example
  1022. Foo
  1023. bar
  1024. ---
  1025. baz
  1026. .
  1027. <p>Foo
  1028. bar</p>
  1029. <hr />
  1030. <p>baz</p>
  1031. ````````````````````````````````
  1032. or use a thematic break that cannot count as a [setext heading
  1033. underline], such as
  1034. ```````````````````````````````` example
  1035. Foo
  1036. bar
  1037. * * *
  1038. baz
  1039. .
  1040. <p>Foo
  1041. bar</p>
  1042. <hr />
  1043. <p>baz</p>
  1044. ````````````````````````````````
  1045. Authors who want interpretation 3 can use backslash escapes:
  1046. ```````````````````````````````` example
  1047. Foo
  1048. bar
  1049. \---
  1050. baz
  1051. .
  1052. <p>Foo
  1053. bar
  1054. ---
  1055. baz</p>
  1056. ````````````````````````````````
  1057. ## Indented code blocks
  1058. An [indented code block](@) is composed of one or more
  1059. [indented chunks] separated by blank lines.
  1060. An [indented chunk](@) is a sequence of non-blank lines,
  1061. each indented four or more spaces. The contents of the code block are
  1062. the literal contents of the lines, including trailing
  1063. [line endings], minus four spaces of indentation.
  1064. An indented code block has no [info string].
  1065. An indented code block cannot interrupt a paragraph, so there must be
  1066. a blank line between a paragraph and a following indented code block.
  1067. (A blank line is not needed, however, between a code block and a following
  1068. paragraph.)
  1069. ```````````````````````````````` example
  1070. a simple
  1071. indented code block
  1072. .
  1073. <pre><code>a simple
  1074. indented code block
  1075. </code></pre>
  1076. ````````````````````````````````
  1077. If there is any ambiguity between an interpretation of indentation
  1078. as a code block and as indicating that material belongs to a [list
  1079. item][list items], the list item interpretation takes precedence:
  1080. ```````````````````````````````` example
  1081. - foo
  1082. bar
  1083. .
  1084. <ul>
  1085. <li>
  1086. <p>foo</p>
  1087. <p>bar</p>
  1088. </li>
  1089. </ul>
  1090. ````````````````````````````````
  1091. ```````````````````````````````` example
  1092. 1. foo
  1093. - bar
  1094. .
  1095. <ol>
  1096. <li>
  1097. <p>foo</p>
  1098. <ul>
  1099. <li>bar</li>
  1100. </ul>
  1101. </li>
  1102. </ol>
  1103. ````````````````````````````````
  1104. The contents of a code block are literal text, and do not get parsed
  1105. as Markdown:
  1106. ```````````````````````````````` example
  1107. <a/>
  1108. *hi*
  1109. - one
  1110. .
  1111. <pre><code>&lt;a/&gt;
  1112. *hi*
  1113. - one
  1114. </code></pre>
  1115. ````````````````````````````````
  1116. Here we have three chunks separated by blank lines:
  1117. ```````````````````````````````` example
  1118. chunk1
  1119. chunk2
  1120. chunk3
  1121. .
  1122. <pre><code>chunk1
  1123. chunk2
  1124. chunk3
  1125. </code></pre>
  1126. ````````````````````````````````
  1127. Any initial spaces beyond four will be included in the content, even
  1128. in interior blank lines:
  1129. ```````````````````````````````` example
  1130. chunk1
  1131. chunk2
  1132. .
  1133. <pre><code>chunk1
  1134. chunk2
  1135. </code></pre>
  1136. ````````````````````````````````
  1137. An indented code block cannot interrupt a paragraph. (This
  1138. allows hanging indents and the like.)
  1139. ```````````````````````````````` example
  1140. Foo
  1141. bar
  1142. .
  1143. <p>Foo
  1144. bar</p>
  1145. ````````````````````````````````
  1146. However, any non-blank line with fewer than four leading spaces ends
  1147. the code block immediately. So a paragraph may occur immediately
  1148. after indented code:
  1149. ```````````````````````````````` example
  1150. foo
  1151. bar
  1152. .
  1153. <pre><code>foo
  1154. </code></pre>
  1155. <p>bar</p>
  1156. ````````````````````````````````
  1157. And indented code can occur immediately before and after other kinds of
  1158. blocks:
  1159. ```````````````````````````````` example
  1160. # Heading
  1161. foo
  1162. Heading
  1163. ------
  1164. foo
  1165. ----
  1166. .
  1167. <h1>Heading</h1>
  1168. <pre><code>foo
  1169. </code></pre>
  1170. <h2>Heading</h2>
  1171. <pre><code>foo
  1172. </code></pre>
  1173. <hr />
  1174. ````````````````````````````````
  1175. The first line can be indented more than four spaces:
  1176. ```````````````````````````````` example
  1177. foo
  1178. bar
  1179. .
  1180. <pre><code> foo
  1181. bar
  1182. </code></pre>
  1183. ````````````````````````````````
  1184. Blank lines preceding or following an indented code block
  1185. are not included in it:
  1186. ```````````````````````````````` example
  1187. foo
  1188. .
  1189. <pre><code>foo
  1190. </code></pre>
  1191. ````````````````````````````````
  1192. Trailing spaces are included in the code block's content:
  1193. ```````````````````````````````` example
  1194. foo
  1195. .
  1196. <pre><code>foo
  1197. </code></pre>
  1198. ````````````````````````````````
  1199. ## Fenced code blocks
  1200. A [code fence](@) is a sequence
  1201. of at least three consecutive backtick characters (`` ` ``) or
  1202. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1203. A [fenced code block](@)
  1204. begins with a code fence, indented no more than three spaces.
  1205. The line with the opening code fence may optionally contain some text
  1206. following the code fence; this is trimmed of leading and trailing
  1207. spaces and called the [info string](@).
  1208. The [info string] may not contain any backtick
  1209. characters. (The reason for this restriction is that otherwise
  1210. some inline code would be incorrectly interpreted as the
  1211. beginning of a fenced code block.)
  1212. The content of the code block consists of all subsequent lines, until
  1213. a closing [code fence] of the same type as the code block
  1214. began with (backticks or tildes), and with at least as many backticks
  1215. or tildes as the opening code fence. If the leading code fence is
  1216. indented N spaces, then up to N spaces of indentation are removed from
  1217. each line of the content (if present). (If a content line is not
  1218. indented, it is preserved unchanged. If it is indented less than N
  1219. spaces, all of the indentation is removed.)
  1220. The closing code fence may be indented up to three spaces, and may be
  1221. followed only by spaces, which are ignored. If the end of the
  1222. containing block (or document) is reached and no closing code fence
  1223. has been found, the code block contains all of the lines after the
  1224. opening code fence until the end of the containing block (or
  1225. document). (An alternative spec would require backtracking in the
  1226. event that a closing code fence is not found. But this makes parsing
  1227. much less efficient, and there seems to be no real down side to the
  1228. behavior described here.)
  1229. A fenced code block may interrupt a paragraph, and does not require
  1230. a blank line either before or after.
  1231. The content of a code fence is treated as literal text, not parsed
  1232. as inlines. The first word of the [info string] is typically used to
  1233. specify the language of the code sample, and rendered in the `class`
  1234. attribute of the `code` tag. However, this spec does not mandate any
  1235. particular treatment of the [info string].
  1236. Here is a simple example with backticks:
  1237. ```````````````````````````````` example
  1238. ```
  1239. <
  1240. >
  1241. ```
  1242. .
  1243. <pre><code>&lt;
  1244. &gt;
  1245. </code></pre>
  1246. ````````````````````````````````
  1247. With tildes:
  1248. ```````````````````````````````` example
  1249. ~~~
  1250. <
  1251. >
  1252. ~~~
  1253. .
  1254. <pre><code>&lt;
  1255. &gt;
  1256. </code></pre>
  1257. ````````````````````````````````
  1258. Fewer than three backticks is not enough:
  1259. ```````````````````````````````` example
  1260. ``
  1261. foo
  1262. ``
  1263. .
  1264. <p><code>foo</code></p>
  1265. ````````````````````````````````
  1266. The closing code fence must use the same character as the opening
  1267. fence:
  1268. ```````````````````````````````` example
  1269. ```
  1270. aaa
  1271. ~~~
  1272. ```
  1273. .
  1274. <pre><code>aaa
  1275. ~~~
  1276. </code></pre>
  1277. ````````````````````````````````
  1278. ```````````````````````````````` example
  1279. ~~~
  1280. aaa
  1281. ```
  1282. ~~~
  1283. .
  1284. <pre><code>aaa
  1285. ```
  1286. </code></pre>
  1287. ````````````````````````````````
  1288. The closing code fence must be at least as long as the opening fence:
  1289. ```````````````````````````````` example
  1290. ````
  1291. aaa
  1292. ```
  1293. ``````
  1294. .
  1295. <pre><code>aaa
  1296. ```
  1297. </code></pre>
  1298. ````````````````````````````````
  1299. ```````````````````````````````` example
  1300. ~~~~
  1301. aaa
  1302. ~~~
  1303. ~~~~
  1304. .
  1305. <pre><code>aaa
  1306. ~~~
  1307. </code></pre>
  1308. ````````````````````````````````
  1309. Unclosed code blocks are closed by the end of the document
  1310. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1311. ```````````````````````````````` example
  1312. ```
  1313. .
  1314. <pre><code></code></pre>
  1315. ````````````````````````````````
  1316. ```````````````````````````````` example
  1317. `````
  1318. ```
  1319. aaa
  1320. .
  1321. <pre><code>
  1322. ```
  1323. aaa
  1324. </code></pre>
  1325. ````````````````````````````````
  1326. ```````````````````````````````` example
  1327. > ```
  1328. > aaa
  1329. bbb
  1330. .
  1331. <blockquote>
  1332. <pre><code>aaa
  1333. </code></pre>
  1334. </blockquote>
  1335. <p>bbb</p>
  1336. ````````````````````````````````
  1337. A code block can have all empty lines as its content:
  1338. ```````````````````````````````` example
  1339. ```
  1340. ```
  1341. .
  1342. <pre><code>
  1343. </code></pre>
  1344. ````````````````````````````````
  1345. A code block can be empty:
  1346. ```````````````````````````````` example
  1347. ```
  1348. ```
  1349. .
  1350. <pre><code></code></pre>
  1351. ````````````````````````````````
  1352. Fences can be indented. If the opening fence is indented,
  1353. content lines will have equivalent opening indentation removed,
  1354. if present:
  1355. ```````````````````````````````` example
  1356. ```
  1357. aaa
  1358. aaa
  1359. ```
  1360. .
  1361. <pre><code>aaa
  1362. aaa
  1363. </code></pre>
  1364. ````````````````````````````````
  1365. ```````````````````````````````` example
  1366. ```
  1367. aaa
  1368. aaa
  1369. aaa
  1370. ```
  1371. .
  1372. <pre><code>aaa
  1373. aaa
  1374. aaa
  1375. </code></pre>
  1376. ````````````````````````````````
  1377. ```````````````````````````````` example
  1378. ```
  1379. aaa
  1380. aaa
  1381. aaa
  1382. ```
  1383. .
  1384. <pre><code>aaa
  1385. aaa
  1386. aaa
  1387. </code></pre>
  1388. ````````````````````````````````
  1389. Four spaces indentation produces an indented code block:
  1390. ```````````````````````````````` example
  1391. ```
  1392. aaa
  1393. ```
  1394. .
  1395. <pre><code>```
  1396. aaa
  1397. ```
  1398. </code></pre>
  1399. ````````````````````````````````
  1400. Closing fences may be indented by 0-3 spaces, and their indentation
  1401. need not match that of the opening fence:
  1402. ```````````````````````````````` example
  1403. ```
  1404. aaa
  1405. ```
  1406. .
  1407. <pre><code>aaa
  1408. </code></pre>
  1409. ````````````````````````````````
  1410. ```````````````````````````````` example
  1411. ```
  1412. aaa
  1413. ```
  1414. .
  1415. <pre><code>aaa
  1416. </code></pre>
  1417. ````````````````````````````````
  1418. This is not a closing fence, because it is indented 4 spaces:
  1419. ```````````````````````````````` example
  1420. ```
  1421. aaa
  1422. ```
  1423. .
  1424. <pre><code>aaa
  1425. ```
  1426. </code></pre>
  1427. ````````````````````````````````
  1428. Code fences (opening and closing) cannot contain internal spaces:
  1429. ```````````````````````````````` example
  1430. ``` ```
  1431. aaa
  1432. .
  1433. <p><code></code>
  1434. aaa</p>
  1435. ````````````````````````````````
  1436. ```````````````````````````````` example
  1437. ~~~~~~
  1438. aaa
  1439. ~~~ ~~
  1440. .
  1441. <pre><code>aaa
  1442. ~~~ ~~
  1443. </code></pre>
  1444. ````````````````````````````````
  1445. Fenced code blocks can interrupt paragraphs, and can be followed
  1446. directly by paragraphs, without a blank line between:
  1447. ```````````````````````````````` example
  1448. foo
  1449. ```
  1450. bar
  1451. ```
  1452. baz
  1453. .
  1454. <p>foo</p>
  1455. <pre><code>bar
  1456. </code></pre>
  1457. <p>baz</p>
  1458. ````````````````````````````````
  1459. Other blocks can also occur before and after fenced code blocks
  1460. without an intervening blank line:
  1461. ```````````````````````````````` example
  1462. foo
  1463. ---
  1464. ~~~
  1465. bar
  1466. ~~~
  1467. # baz
  1468. .
  1469. <h2>foo</h2>
  1470. <pre><code>bar
  1471. </code></pre>
  1472. <h1>baz</h1>
  1473. ````````````````````````````````
  1474. An [info string] can be provided after the opening code fence.
  1475. Opening and closing spaces will be stripped, and the first word, prefixed
  1476. with `language-`, is used as the value for the `class` attribute of the
  1477. `code` element within the enclosing `pre` element.
  1478. ```````````````````````````````` example
  1479. ```ruby
  1480. def foo(x)
  1481. return 3
  1482. end
  1483. ```
  1484. .
  1485. <pre><code class="language-ruby">def foo(x)
  1486. return 3
  1487. end
  1488. </code></pre>
  1489. ````````````````````````````````
  1490. ```````````````````````````````` example
  1491. ~~~~ ruby startline=3 $%@#$
  1492. def foo(x)
  1493. return 3
  1494. end
  1495. ~~~~~~~
  1496. .
  1497. <pre><code class="language-ruby">def foo(x)
  1498. return 3
  1499. end
  1500. </code></pre>
  1501. ````````````````````````````````
  1502. ```````````````````````````````` example
  1503. ````;
  1504. ````
  1505. .
  1506. <pre><code class="language-;"></code></pre>
  1507. ````````````````````````````````
  1508. [Info strings] for backtick code blocks cannot contain backticks:
  1509. ```````````````````````````````` example
  1510. ``` aa ```
  1511. foo
  1512. .
  1513. <p><code>aa</code>
  1514. foo</p>
  1515. ````````````````````````````````
  1516. Closing code fences cannot have [info strings]:
  1517. ```````````````````````````````` example
  1518. ```
  1519. ``` aaa
  1520. ```
  1521. .
  1522. <pre><code>``` aaa
  1523. </code></pre>
  1524. ````````````````````````````````
  1525. ## HTML blocks
  1526. An [HTML block](@) is a group of lines that is treated
  1527. as raw HTML (and will not be escaped in HTML output).
  1528. There are seven kinds of [HTML block], which can be defined
  1529. by their start and end conditions. The block begins with a line that
  1530. meets a [start condition](@) (after up to three spaces
  1531. optional indentation). It ends with the first subsequent line that
  1532. meets a matching [end condition](@), or the last line of
  1533. the document or other [container block]), if no line is encountered that meets the
  1534. [end condition]. If the first line meets both the [start condition]
  1535. and the [end condition], the block will contain just that line.
  1536. 1. **Start condition:** line begins with the string `<script`,
  1537. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1538. the string `>`, or the end of the line.\
  1539. **End condition:** line contains an end tag
  1540. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1541. need not match the start tag).
  1542. 2. **Start condition:** line begins with the string `<!--`.\
  1543. **End condition:** line contains the string `-->`.
  1544. 3. **Start condition:** line begins with the string `<?`.\
  1545. **End condition:** line contains the string `?>`.
  1546. 4. **Start condition:** line begins with the string `<!`
  1547. followed by an uppercase ASCII letter.\
  1548. **End condition:** line contains the character `>`.
  1549. 5. **Start condition:** line begins with the string
  1550. `<![CDATA[`.\
  1551. **End condition:** line contains the string `]]>`.
  1552. 6. **Start condition:** line begins the string `<` or `</`
  1553. followed by one of the strings (case-insensitive) `address`,
  1554. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1555. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1556. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1557. `footer`, `form`, `frame`, `frameset`,
  1558. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1559. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1560. `meta`, `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1561. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1562. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1563. by [whitespace], the end of the line, the string `>`, or
  1564. the string `/>`.\
  1565. **End condition:** line is followed by a [blank line].
  1566. 7. **Start condition:** line begins with a complete [open tag]
  1567. or [closing tag] (with any [tag name] other than `script`,
  1568. `style`, or `pre`) followed only by [whitespace]
  1569. or the end of the line.\
  1570. **End condition:** line is followed by a [blank line].
  1571. All types of [HTML blocks] except type 7 may interrupt
  1572. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1573. (This restriction is intended to prevent unwanted interpretation
  1574. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1575. Some simple examples follow. Here are some basic HTML blocks
  1576. of type 6:
  1577. ```````````````````````````````` example
  1578. <table>
  1579. <tr>
  1580. <td>
  1581. hi
  1582. </td>
  1583. </tr>
  1584. </table>
  1585. okay.
  1586. .
  1587. <table>
  1588. <tr>
  1589. <td>
  1590. hi
  1591. </td>
  1592. </tr>
  1593. </table>
  1594. <p>okay.</p>
  1595. ````````````````````````````````
  1596. ```````````````````````````````` example
  1597. <div>
  1598. *hello*
  1599. <foo><a>
  1600. .
  1601. <div>
  1602. *hello*
  1603. <foo><a>
  1604. ````````````````````````````````
  1605. A block can also start with a closing tag:
  1606. ```````````````````````````````` example
  1607. </div>
  1608. *foo*
  1609. .
  1610. </div>
  1611. *foo*
  1612. ````````````````````````````````
  1613. Here we have two HTML blocks with a Markdown paragraph between them:
  1614. ```````````````````````````````` example
  1615. <DIV CLASS="foo">
  1616. *Markdown*
  1617. </DIV>
  1618. .
  1619. <DIV CLASS="foo">
  1620. <p><em>Markdown</em></p>
  1621. </DIV>
  1622. ````````````````````````````````
  1623. The tag on the first line can be partial, as long
  1624. as it is split where there would be whitespace:
  1625. ```````````````````````````````` example
  1626. <div id="foo"
  1627. class="bar">
  1628. </div>
  1629. .
  1630. <div id="foo"
  1631. class="bar">
  1632. </div>
  1633. ````````````````````````````````
  1634. ```````````````````````````````` example
  1635. <div id="foo" class="bar
  1636. baz">
  1637. </div>
  1638. .
  1639. <div id="foo" class="bar
  1640. baz">
  1641. </div>
  1642. ````````````````````````````````
  1643. An open tag need not be closed:
  1644. ```````````````````````````````` example
  1645. <div>
  1646. *foo*
  1647. *bar*
  1648. .
  1649. <div>
  1650. *foo*
  1651. <p><em>bar</em></p>
  1652. ````````````````````````````````
  1653. A partial tag need not even be completed (garbage
  1654. in, garbage out):
  1655. ```````````````````````````````` example
  1656. <div id="foo"
  1657. *hi*
  1658. .
  1659. <div id="foo"
  1660. *hi*
  1661. ````````````````````````````````
  1662. ```````````````````````````````` example
  1663. <div class
  1664. foo
  1665. .
  1666. <div class
  1667. foo
  1668. ````````````````````````````````
  1669. The initial tag doesn't even need to be a valid
  1670. tag, as long as it starts like one:
  1671. ```````````````````````````````` example
  1672. <div *???-&&&-<---
  1673. *foo*
  1674. .
  1675. <div *???-&&&-<---
  1676. *foo*
  1677. ````````````````````````````````
  1678. In type 6 blocks, the initial tag need not be on a line by
  1679. itself:
  1680. ```````````````````````````````` example
  1681. <div><a href="bar">*foo*</a></div>
  1682. .
  1683. <div><a href="bar">*foo*</a></div>
  1684. ````````````````````````````````
  1685. ```````````````````````````````` example
  1686. <table><tr><td>
  1687. foo
  1688. </td></tr></table>
  1689. .
  1690. <table><tr><td>
  1691. foo
  1692. </td></tr></table>
  1693. ````````````````````````````````
  1694. Everything until the next blank line or end of document
  1695. gets included in the HTML block. So, in the following
  1696. example, what looks like a Markdown code block
  1697. is actually part of the HTML block, which continues until a blank
  1698. line or the end of the document is reached:
  1699. ```````````````````````````````` example
  1700. <div></div>
  1701. ``` c
  1702. int x = 33;
  1703. ```
  1704. .
  1705. <div></div>
  1706. ``` c
  1707. int x = 33;
  1708. ```
  1709. ````````````````````````````````
  1710. To start an [HTML block] with a tag that is *not* in the
  1711. list of block-level tags in (6), you must put the tag by
  1712. itself on the first line (and it must be complete):
  1713. ```````````````````````````````` example
  1714. <a href="foo">
  1715. *bar*
  1716. </a>
  1717. .
  1718. <a href="foo">
  1719. *bar*
  1720. </a>
  1721. ````````````````````````````````
  1722. In type 7 blocks, the [tag name] can be anything:
  1723. ```````````````````````````````` example
  1724. <Warning>
  1725. *bar*
  1726. </Warning>
  1727. .
  1728. <Warning>
  1729. *bar*
  1730. </Warning>
  1731. ````````````````````````````````
  1732. ```````````````````````````````` example
  1733. <i class="foo">
  1734. *bar*
  1735. </i>
  1736. .
  1737. <i class="foo">
  1738. *bar*
  1739. </i>
  1740. ````````````````````````````````
  1741. ```````````````````````````````` example
  1742. </ins>
  1743. *bar*
  1744. .
  1745. </ins>
  1746. *bar*
  1747. ````````````````````````````````
  1748. These rules are designed to allow us to work with tags that
  1749. can function as either block-level or inline-level tags.
  1750. The `<del>` tag is a nice example. We can surround content with
  1751. `<del>` tags in three different ways. In this case, we get a raw
  1752. HTML block, because the `<del>` tag is on a line by itself:
  1753. ```````````````````````````````` example
  1754. <del>
  1755. *foo*
  1756. </del>
  1757. .
  1758. <del>
  1759. *foo*
  1760. </del>
  1761. ````````````````````````````````
  1762. In this case, we get a raw HTML block that just includes
  1763. the `<del>` tag (because it ends with the following blank
  1764. line). So the contents get interpreted as CommonMark:
  1765. ```````````````````````````````` example
  1766. <del>
  1767. *foo*
  1768. </del>
  1769. .
  1770. <del>
  1771. <p><em>foo</em></p>
  1772. </del>
  1773. ````````````````````````````````
  1774. Finally, in this case, the `<del>` tags are interpreted
  1775. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1776. the tag is not on a line by itself, we get inline HTML
  1777. rather than an [HTML block].)
  1778. ```````````````````````````````` example
  1779. <del>*foo*</del>
  1780. .
  1781. <p><del><em>foo</em></del></p>
  1782. ````````````````````````````````
  1783. HTML tags designed to contain literal content
  1784. (`script`, `style`, `pre`), comments, processing instructions,
  1785. and declarations are treated somewhat differently.
  1786. Instead of ending at the first blank line, these blocks
  1787. end at the first line containing a corresponding end tag.
  1788. As a result, these blocks can contain blank lines:
  1789. A pre tag (type 1):
  1790. ```````````````````````````````` example
  1791. <pre language="haskell"><code>
  1792. import Text.HTML.TagSoup
  1793. main :: IO ()
  1794. main = print $ parseTags tags
  1795. </code></pre>
  1796. okay
  1797. .
  1798. <pre language="haskell"><code>
  1799. import Text.HTML.TagSoup
  1800. main :: IO ()
  1801. main = print $ parseTags tags
  1802. </code></pre>
  1803. <p>okay</p>
  1804. ````````````````````````````````
  1805. A script tag (type 1):
  1806. ```````````````````````````````` example
  1807. <script type="text/javascript">
  1808. // JavaScript example
  1809. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1810. </script>
  1811. okay
  1812. .
  1813. <script type="text/javascript">
  1814. // JavaScript example
  1815. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1816. </script>
  1817. <p>okay</p>
  1818. ````````````````````````````````
  1819. A style tag (type 1):
  1820. ```````````````````````````````` example
  1821. <style
  1822. type="text/css">
  1823. h1 {color:red;}
  1824. p {color:blue;}
  1825. </style>
  1826. okay
  1827. .
  1828. <style
  1829. type="text/css">
  1830. h1 {color:red;}
  1831. p {color:blue;}
  1832. </style>
  1833. <p>okay</p>
  1834. ````````````````````````````````
  1835. If there is no matching end tag, the block will end at the
  1836. end of the document (or the enclosing [block quote][block quotes]
  1837. or [list item][list items]):
  1838. ```````````````````````````````` example
  1839. <style
  1840. type="text/css">
  1841. foo
  1842. .
  1843. <style
  1844. type="text/css">
  1845. foo
  1846. ````````````````````````````````
  1847. ```````````````````````````````` example
  1848. > <div>
  1849. > foo
  1850. bar
  1851. .
  1852. <blockquote>
  1853. <div>
  1854. foo
  1855. </blockquote>
  1856. <p>bar</p>
  1857. ````````````````````````````````
  1858. ```````````````````````````````` example
  1859. - <div>
  1860. - foo
  1861. .
  1862. <ul>
  1863. <li>
  1864. <div>
  1865. </li>
  1866. <li>foo</li>
  1867. </ul>
  1868. ````````````````````````````````
  1869. The end tag can occur on the same line as the start tag:
  1870. ```````````````````````````````` example
  1871. <style>p{color:red;}</style>
  1872. *foo*
  1873. .
  1874. <style>p{color:red;}</style>
  1875. <p><em>foo</em></p>
  1876. ````````````````````````````````
  1877. ```````````````````````````````` example
  1878. <!-- foo -->*bar*
  1879. *baz*
  1880. .
  1881. <!-- foo -->*bar*
  1882. <p><em>baz</em></p>
  1883. ````````````````````````````````
  1884. Note that anything on the last line after the
  1885. end tag will be included in the [HTML block]:
  1886. ```````````````````````````````` example
  1887. <script>
  1888. foo
  1889. </script>1. *bar*
  1890. .
  1891. <script>
  1892. foo
  1893. </script>1. *bar*
  1894. ````````````````````````````````
  1895. A comment (type 2):
  1896. ```````````````````````````````` example
  1897. <!-- Foo
  1898. bar
  1899. baz -->
  1900. okay
  1901. .
  1902. <!-- Foo
  1903. bar
  1904. baz -->
  1905. <p>okay</p>
  1906. ````````````````````````````````
  1907. A processing instruction (type 3):
  1908. ```````````````````````````````` example
  1909. <?php
  1910. echo '>';
  1911. ?>
  1912. okay
  1913. .
  1914. <?php
  1915. echo '>';
  1916. ?>
  1917. <p>okay</p>
  1918. ````````````````````````````````
  1919. A declaration (type 4):
  1920. ```````````````````````````````` example
  1921. <!DOCTYPE html>
  1922. .
  1923. <!DOCTYPE html>
  1924. ````````````````````````````````
  1925. CDATA (type 5):
  1926. ```````````````````````````````` example
  1927. <![CDATA[
  1928. function matchwo(a,b)
  1929. {
  1930. if (a < b && a < 0) then {
  1931. return 1;
  1932. } else {
  1933. return 0;
  1934. }
  1935. }
  1936. ]]>
  1937. okay
  1938. .
  1939. <![CDATA[
  1940. function matchwo(a,b)
  1941. {
  1942. if (a < b && a < 0) then {
  1943. return 1;
  1944. } else {
  1945. return 0;
  1946. }
  1947. }
  1948. ]]>
  1949. <p>okay</p>
  1950. ````````````````````````````````
  1951. The opening tag can be indented 1-3 spaces, but not 4:
  1952. ```````````````````````````````` example
  1953. <!-- foo -->
  1954. <!-- foo -->
  1955. .
  1956. <!-- foo -->
  1957. <pre><code>&lt;!-- foo --&gt;
  1958. </code></pre>
  1959. ````````````````````````````````
  1960. ```````````````````````````````` example
  1961. <div>
  1962. <div>
  1963. .
  1964. <div>
  1965. <pre><code>&lt;div&gt;
  1966. </code></pre>
  1967. ````````````````````````````````
  1968. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1969. preceded by a blank line.
  1970. ```````````````````````````````` example
  1971. Foo
  1972. <div>
  1973. bar
  1974. </div>
  1975. .
  1976. <p>Foo</p>
  1977. <div>
  1978. bar
  1979. </div>
  1980. ````````````````````````````````
  1981. However, a following blank line is needed, except at the end of
  1982. a document, and except for blocks of types 1--5, above:
  1983. ```````````````````````````````` example
  1984. <div>
  1985. bar
  1986. </div>
  1987. *foo*
  1988. .
  1989. <div>
  1990. bar
  1991. </div>
  1992. *foo*
  1993. ````````````````````````````````
  1994. HTML blocks of type 7 cannot interrupt a paragraph:
  1995. ```````````````````````````````` example
  1996. Foo
  1997. <a href="bar">
  1998. baz
  1999. .
  2000. <p>Foo
  2001. <a href="bar">
  2002. baz</p>
  2003. ````````````````````````````````
  2004. This rule differs from John Gruber's original Markdown syntax
  2005. specification, which says:
  2006. > The only restrictions are that block-level HTML elements —
  2007. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2008. > surrounding content by blank lines, and the start and end tags of the
  2009. > block should not be indented with tabs or spaces.
  2010. In some ways Gruber's rule is more restrictive than the one given
  2011. here:
  2012. - It requires that an HTML block be preceded by a blank line.
  2013. - It does not allow the start tag to be indented.
  2014. - It requires a matching end tag, which it also does not allow to
  2015. be indented.
  2016. Most Markdown implementations (including some of Gruber's own) do not
  2017. respect all of these restrictions.
  2018. There is one respect, however, in which Gruber's rule is more liberal
  2019. than the one given here, since it allows blank lines to occur inside
  2020. an HTML block. There are two reasons for disallowing them here.
  2021. First, it removes the need to parse balanced tags, which is
  2022. expensive and can require backtracking from the end of the document
  2023. if no matching end tag is found. Second, it provides a very simple
  2024. and flexible way of including Markdown content inside HTML tags:
  2025. simply separate the Markdown from the HTML using blank lines:
  2026. Compare:
  2027. ```````````````````````````````` example
  2028. <div>
  2029. *Emphasized* text.
  2030. </div>
  2031. .
  2032. <div>
  2033. <p><em>Emphasized</em> text.</p>
  2034. </div>
  2035. ````````````````````````````````
  2036. ```````````````````````````````` example
  2037. <div>
  2038. *Emphasized* text.
  2039. </div>
  2040. .
  2041. <div>
  2042. *Emphasized* text.
  2043. </div>
  2044. ````````````````````````````````
  2045. Some Markdown implementations have adopted a convention of
  2046. interpreting content inside tags as text if the open tag has
  2047. the attribute `markdown=1`. The rule given above seems a simpler and
  2048. more elegant way of achieving the same expressive power, which is also
  2049. much simpler to parse.
  2050. The main potential drawback is that one can no longer paste HTML
  2051. blocks into Markdown documents with 100% reliability. However,
  2052. *in most cases* this will work fine, because the blank lines in
  2053. HTML are usually followed by HTML block tags. For example:
  2054. ```````````````````````````````` example
  2055. <table>
  2056. <tr>
  2057. <td>
  2058. Hi
  2059. </td>
  2060. </tr>
  2061. </table>
  2062. .
  2063. <table>
  2064. <tr>
  2065. <td>
  2066. Hi
  2067. </td>
  2068. </tr>
  2069. </table>
  2070. ````````````````````````````````
  2071. There are problems, however, if the inner tags are indented
  2072. *and* separated by spaces, as then they will be interpreted as
  2073. an indented code block:
  2074. ```````````````````````````````` example
  2075. <table>
  2076. <tr>
  2077. <td>
  2078. Hi
  2079. </td>
  2080. </tr>
  2081. </table>
  2082. .
  2083. <table>
  2084. <tr>
  2085. <pre><code>&lt;td&gt;
  2086. Hi
  2087. &lt;/td&gt;
  2088. </code></pre>
  2089. </tr>
  2090. </table>
  2091. ````````````````````````````````
  2092. Fortunately, blank lines are usually not necessary and can be
  2093. deleted. The exception is inside `<pre>` tags, but as described
  2094. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2095. lines.
  2096. ## Link reference definitions
  2097. A [link reference definition](@)
  2098. consists of a [link label], indented up to three spaces, followed
  2099. by a colon (`:`), optional [whitespace] (including up to one
  2100. [line ending]), a [link destination],
  2101. optional [whitespace] (including up to one
  2102. [line ending]), and an optional [link
  2103. title], which if it is present must be separated
  2104. from the [link destination] by [whitespace].
  2105. No further [non-whitespace characters] may occur on the line.
  2106. A [link reference definition]
  2107. does not correspond to a structural element of a document. Instead, it
  2108. defines a label which can be used in [reference links]
  2109. and reference-style [images] elsewhere in the document. [Link
  2110. reference definitions] can come either before or after the links that use
  2111. them.
  2112. ```````````````````````````````` example
  2113. [foo]: /url "title"
  2114. [foo]
  2115. .
  2116. <p><a href="/url" title="title">foo</a></p>
  2117. ````````````````````````````````
  2118. ```````````````````````````````` example
  2119. [foo]:
  2120. /url
  2121. 'the title'
  2122. [foo]
  2123. .
  2124. <p><a href="/url" title="the title">foo</a></p>
  2125. ````````````````````````````````
  2126. ```````````````````````````````` example
  2127. [Foo*bar\]]:my_(url) 'title (with parens)'
  2128. [Foo*bar\]]
  2129. .
  2130. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2131. ````````````````````````````````
  2132. ```````````````````````````````` example
  2133. [Foo bar]:
  2134. <my%20url>
  2135. 'title'
  2136. [Foo bar]
  2137. .
  2138. <p><a href="my%20url" title="title">Foo bar</a></p>
  2139. ````````````````````````````````
  2140. The title may extend over multiple lines:
  2141. ```````````````````````````````` example
  2142. [foo]: /url '
  2143. title
  2144. line1
  2145. line2
  2146. '
  2147. [foo]
  2148. .
  2149. <p><a href="/url" title="
  2150. title
  2151. line1
  2152. line2
  2153. ">foo</a></p>
  2154. ````````````````````````````````
  2155. However, it may not contain a [blank line]:
  2156. ```````````````````````````````` example
  2157. [foo]: /url 'title
  2158. with blank line'
  2159. [foo]
  2160. .
  2161. <p>[foo]: /url 'title</p>
  2162. <p>with blank line'</p>
  2163. <p>[foo]</p>
  2164. ````````````````````````````````
  2165. The title may be omitted:
  2166. ```````````````````````````````` example
  2167. [foo]:
  2168. /url
  2169. [foo]
  2170. .
  2171. <p><a href="/url">foo</a></p>
  2172. ````````````````````````````````
  2173. The link destination may not be omitted:
  2174. ```````````````````````````````` example
  2175. [foo]:
  2176. [foo]
  2177. .
  2178. <p>[foo]:</p>
  2179. <p>[foo]</p>
  2180. ````````````````````````````````
  2181. Both title and destination can contain backslash escapes
  2182. and literal backslashes:
  2183. ```````````````````````````````` example
  2184. [foo]: /url\bar\*baz "foo\"bar\baz"
  2185. [foo]
  2186. .
  2187. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2188. ````````````````````````````````
  2189. A link can come before its corresponding definition:
  2190. ```````````````````````````````` example
  2191. [foo]
  2192. [foo]: url
  2193. .
  2194. <p><a href="url">foo</a></p>
  2195. ````````````````````````````````
  2196. If there are several matching definitions, the first one takes
  2197. precedence:
  2198. ```````````````````````````````` example
  2199. [foo]
  2200. [foo]: first
  2201. [foo]: second
  2202. .
  2203. <p><a href="first">foo</a></p>
  2204. ````````````````````````````````
  2205. As noted in the section on [Links], matching of labels is
  2206. case-insensitive (see [matches]).
  2207. ```````````````````````````````` example
  2208. [FOO]: /url
  2209. [Foo]
  2210. .
  2211. <p><a href="/url">Foo</a></p>
  2212. ````````````````````````````````
  2213. ```````````````````````````````` example
  2214. [ΑΓΩ]: /φου
  2215. [αγω]
  2216. .
  2217. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2218. ````````````````````````````````
  2219. Here is a link reference definition with no corresponding link.
  2220. It contributes nothing to the document.
  2221. ```````````````````````````````` example
  2222. [foo]: /url
  2223. .
  2224. ````````````````````````````````
  2225. Here is another one:
  2226. ```````````````````````````````` example
  2227. [
  2228. foo
  2229. ]: /url
  2230. bar
  2231. .
  2232. <p>bar</p>
  2233. ````````````````````````````````
  2234. This is not a link reference definition, because there are
  2235. [non-whitespace characters] after the title:
  2236. ```````````````````````````````` example
  2237. [foo]: /url "title" ok
  2238. .
  2239. <p>[foo]: /url &quot;title&quot; ok</p>
  2240. ````````````````````````````````
  2241. This is a link reference definition, but it has no title:
  2242. ```````````````````````````````` example
  2243. [foo]: /url
  2244. "title" ok
  2245. .
  2246. <p>&quot;title&quot; ok</p>
  2247. ````````````````````````````````
  2248. This is not a link reference definition, because it is indented
  2249. four spaces:
  2250. ```````````````````````````````` example
  2251. [foo]: /url "title"
  2252. [foo]
  2253. .
  2254. <pre><code>[foo]: /url &quot;title&quot;
  2255. </code></pre>
  2256. <p>[foo]</p>
  2257. ````````````````````````````````
  2258. This is not a link reference definition, because it occurs inside
  2259. a code block:
  2260. ```````````````````````````````` example
  2261. ```
  2262. [foo]: /url
  2263. ```
  2264. [foo]
  2265. .
  2266. <pre><code>[foo]: /url
  2267. </code></pre>
  2268. <p>[foo]</p>
  2269. ````````````````````````````````
  2270. A [link reference definition] cannot interrupt a paragraph.
  2271. ```````````````````````````````` example
  2272. Foo
  2273. [bar]: /baz
  2274. [bar]
  2275. .
  2276. <p>Foo
  2277. [bar]: /baz</p>
  2278. <p>[bar]</p>
  2279. ````````````````````````````````
  2280. However, it can directly follow other block elements, such as headings
  2281. and thematic breaks, and it need not be followed by a blank line.
  2282. ```````````````````````````````` example
  2283. # [Foo]
  2284. [foo]: /url
  2285. > bar
  2286. .
  2287. <h1><a href="/url">Foo</a></h1>
  2288. <blockquote>
  2289. <p>bar</p>
  2290. </blockquote>
  2291. ````````````````````````````````
  2292. Several [link reference definitions]
  2293. can occur one after another, without intervening blank lines.
  2294. ```````````````````````````````` example
  2295. [foo]: /foo-url "foo"
  2296. [bar]: /bar-url
  2297. "bar"
  2298. [baz]: /baz-url
  2299. [foo],
  2300. [bar],
  2301. [baz]
  2302. .
  2303. <p><a href="/foo-url" title="foo">foo</a>,
  2304. <a href="/bar-url" title="bar">bar</a>,
  2305. <a href="/baz-url">baz</a></p>
  2306. ````````````````````````````````
  2307. [Link reference definitions] can occur
  2308. inside block containers, like lists and block quotations. They
  2309. affect the entire document, not just the container in which they
  2310. are defined:
  2311. ```````````````````````````````` example
  2312. [foo]
  2313. > [foo]: /url
  2314. .
  2315. <p><a href="/url">foo</a></p>
  2316. <blockquote>
  2317. </blockquote>
  2318. ````````````````````````````````
  2319. ## Paragraphs
  2320. A sequence of non-blank lines that cannot be interpreted as other
  2321. kinds of blocks forms a [paragraph](@).
  2322. The contents of the paragraph are the result of parsing the
  2323. paragraph's raw content as inlines. The paragraph's raw content
  2324. is formed by concatenating the lines and removing initial and final
  2325. [whitespace].
  2326. A simple example with two paragraphs:
  2327. ```````````````````````````````` example
  2328. aaa
  2329. bbb
  2330. .
  2331. <p>aaa</p>
  2332. <p>bbb</p>
  2333. ````````````````````````````````
  2334. Paragraphs can contain multiple lines, but no blank lines:
  2335. ```````````````````````````````` example
  2336. aaa
  2337. bbb
  2338. ccc
  2339. ddd
  2340. .
  2341. <p>aaa
  2342. bbb</p>
  2343. <p>ccc
  2344. ddd</p>
  2345. ````````````````````````````````
  2346. Multiple blank lines between paragraph have no effect:
  2347. ```````````````````````````````` example
  2348. aaa
  2349. bbb
  2350. .
  2351. <p>aaa</p>
  2352. <p>bbb</p>
  2353. ````````````````````````````````
  2354. Leading spaces are skipped:
  2355. ```````````````````````````````` example
  2356. aaa
  2357. bbb
  2358. .
  2359. <p>aaa
  2360. bbb</p>
  2361. ````````````````````````````````
  2362. Lines after the first may be indented any amount, since indented
  2363. code blocks cannot interrupt paragraphs.
  2364. ```````````````````````````````` example
  2365. aaa
  2366. bbb
  2367. ccc
  2368. .
  2369. <p>aaa
  2370. bbb
  2371. ccc</p>
  2372. ````````````````````````````````
  2373. However, the first line may be indented at most three spaces,
  2374. or an indented code block will be triggered:
  2375. ```````````````````````````````` example
  2376. aaa
  2377. bbb
  2378. .
  2379. <p>aaa
  2380. bbb</p>
  2381. ````````````````````````````````
  2382. ```````````````````````````````` example
  2383. aaa
  2384. bbb
  2385. .
  2386. <pre><code>aaa
  2387. </code></pre>
  2388. <p>bbb</p>
  2389. ````````````````````````````````
  2390. Final spaces are stripped before inline parsing, so a paragraph
  2391. that ends with two or more spaces will not end with a [hard line
  2392. break]:
  2393. ```````````````````````````````` example
  2394. aaa
  2395. bbb
  2396. .
  2397. <p>aaa<br />
  2398. bbb</p>
  2399. ````````````````````````````````
  2400. ## Blank lines
  2401. [Blank lines] between block-level elements are ignored,
  2402. except for the role they play in determining whether a [list]
  2403. is [tight] or [loose].
  2404. Blank lines at the beginning and end of the document are also ignored.
  2405. ```````````````````````````````` example
  2406. aaa
  2407. # aaa
  2408. .
  2409. <p>aaa</p>
  2410. <h1>aaa</h1>
  2411. ````````````````````````````````
  2412. # Container blocks
  2413. A [container block] is a block that has other
  2414. blocks as its contents. There are two basic kinds of container blocks:
  2415. [block quotes] and [list items].
  2416. [Lists] are meta-containers for [list items].
  2417. We define the syntax for container blocks recursively. The general
  2418. form of the definition is:
  2419. > If X is a sequence of blocks, then the result of
  2420. > transforming X in such-and-such a way is a container of type Y
  2421. > with these blocks as its content.
  2422. So, we explain what counts as a block quote or list item by explaining
  2423. how these can be *generated* from their contents. This should suffice
  2424. to define the syntax, although it does not give a recipe for *parsing*
  2425. these constructions. (A recipe is provided below in the section entitled
  2426. [A parsing strategy](#appendix-a-parsing-strategy).)
  2427. ## Block quotes
  2428. A [block quote marker](@)
  2429. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2430. with a following space, or (b) a single character `>` not followed by a space.
  2431. The following rules define [block quotes]:
  2432. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2433. of blocks *Bs*, then the result of prepending a [block quote
  2434. marker] to the beginning of each line in *Ls*
  2435. is a [block quote](#block-quotes) containing *Bs*.
  2436. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2437. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2438. the initial [block quote marker] from one or
  2439. more lines in which the next [non-whitespace character] after the [block
  2440. quote marker] is [paragraph continuation
  2441. text] is a block quote with *Bs* as its content.
  2442. [Paragraph continuation text](@) is text
  2443. that will be parsed as part of the content of a paragraph, but does
  2444. not occur at the beginning of the paragraph.
  2445. 3. **Consecutiveness.** A document cannot contain two [block
  2446. quotes] in a row unless there is a [blank line] between them.
  2447. Nothing else counts as a [block quote](#block-quotes).
  2448. Here is a simple example:
  2449. ```````````````````````````````` example
  2450. > # Foo
  2451. > bar
  2452. > baz
  2453. .
  2454. <blockquote>
  2455. <h1>Foo</h1>
  2456. <p>bar
  2457. baz</p>
  2458. </blockquote>
  2459. ````````````````````````````````
  2460. The spaces after the `>` characters can be omitted:
  2461. ```````````````````````````````` example
  2462. ># Foo
  2463. >bar
  2464. > baz
  2465. .
  2466. <blockquote>
  2467. <h1>Foo</h1>
  2468. <p>bar
  2469. baz</p>
  2470. </blockquote>
  2471. ````````````````````````````````
  2472. The `>` characters can be indented 1-3 spaces:
  2473. ```````````````````````````````` example
  2474. > # Foo
  2475. > bar
  2476. > baz
  2477. .
  2478. <blockquote>
  2479. <h1>Foo</h1>
  2480. <p>bar
  2481. baz</p>
  2482. </blockquote>
  2483. ````````````````````````````````
  2484. Four spaces gives us a code block:
  2485. ```````````````````````````````` example
  2486. > # Foo
  2487. > bar
  2488. > baz
  2489. .
  2490. <pre><code>&gt; # Foo
  2491. &gt; bar
  2492. &gt; baz
  2493. </code></pre>
  2494. ````````````````````````````````
  2495. The Laziness clause allows us to omit the `>` before
  2496. [paragraph continuation text]:
  2497. ```````````````````````````````` example
  2498. > # Foo
  2499. > bar
  2500. baz
  2501. .
  2502. <blockquote>
  2503. <h1>Foo</h1>
  2504. <p>bar
  2505. baz</p>
  2506. </blockquote>
  2507. ````````````````````````````````
  2508. A block quote can contain some lazy and some non-lazy
  2509. continuation lines:
  2510. ```````````````````````````````` example
  2511. > bar
  2512. baz
  2513. > foo
  2514. .
  2515. <blockquote>
  2516. <p>bar
  2517. baz
  2518. foo</p>
  2519. </blockquote>
  2520. ````````````````````````````````
  2521. Laziness only applies to lines that would have been continuations of
  2522. paragraphs had they been prepended with [block quote markers].
  2523. For example, the `> ` cannot be omitted in the second line of
  2524. ``` markdown
  2525. > foo
  2526. > ---
  2527. ```
  2528. without changing the meaning:
  2529. ```````````````````````````````` example
  2530. > foo
  2531. ---
  2532. .
  2533. <blockquote>
  2534. <p>foo</p>
  2535. </blockquote>
  2536. <hr />
  2537. ````````````````````````````````
  2538. Similarly, if we omit the `> ` in the second line of
  2539. ``` markdown
  2540. > - foo
  2541. > - bar
  2542. ```
  2543. then the block quote ends after the first line:
  2544. ```````````````````````````````` example
  2545. > - foo
  2546. - bar
  2547. .
  2548. <blockquote>
  2549. <ul>
  2550. <li>foo</li>
  2551. </ul>
  2552. </blockquote>
  2553. <ul>
  2554. <li>bar</li>
  2555. </ul>
  2556. ````````````````````````````````
  2557. For the same reason, we can't omit the `> ` in front of
  2558. subsequent lines of an indented or fenced code block:
  2559. ```````````````````````````````` example
  2560. > foo
  2561. bar
  2562. .
  2563. <blockquote>
  2564. <pre><code>foo
  2565. </code></pre>
  2566. </blockquote>
  2567. <pre><code>bar
  2568. </code></pre>
  2569. ````````````````````````````````
  2570. ```````````````````````````````` example
  2571. > ```
  2572. foo
  2573. ```
  2574. .
  2575. <blockquote>
  2576. <pre><code></code></pre>
  2577. </blockquote>
  2578. <p>foo</p>
  2579. <pre><code></code></pre>
  2580. ````````````````````````````````
  2581. Note that in the following case, we have a [lazy
  2582. continuation line]:
  2583. ```````````````````````````````` example
  2584. > foo
  2585. - bar
  2586. .
  2587. <blockquote>
  2588. <p>foo
  2589. - bar</p>
  2590. </blockquote>
  2591. ````````````````````````````````
  2592. To see why, note that in
  2593. ```markdown
  2594. > foo
  2595. > - bar
  2596. ```
  2597. the `- bar` is indented too far to start a list, and can't
  2598. be an indented code block because indented code blocks cannot
  2599. interrupt paragraphs, so it is [paragraph continuation text].
  2600. A block quote can be empty:
  2601. ```````````````````````````````` example
  2602. >
  2603. .
  2604. <blockquote>
  2605. </blockquote>
  2606. ````````````````````````````````
  2607. ```````````````````````````````` example
  2608. >
  2609. >
  2610. >
  2611. .
  2612. <blockquote>
  2613. </blockquote>
  2614. ````````````````````````````````
  2615. A block quote can have initial or final blank lines:
  2616. ```````````````````````````````` example
  2617. >
  2618. > foo
  2619. >
  2620. .
  2621. <blockquote>
  2622. <p>foo</p>
  2623. </blockquote>
  2624. ````````````````````````````````
  2625. A blank line always separates block quotes:
  2626. ```````````````````````````````` example
  2627. > foo
  2628. > bar
  2629. .
  2630. <blockquote>
  2631. <p>foo</p>
  2632. </blockquote>
  2633. <blockquote>
  2634. <p>bar</p>
  2635. </blockquote>
  2636. ````````````````````````````````
  2637. (Most current Markdown implementations, including John Gruber's
  2638. original `Markdown.pl`, will parse this example as a single block quote
  2639. with two paragraphs. But it seems better to allow the author to decide
  2640. whether two block quotes or one are wanted.)
  2641. Consecutiveness means that if we put these block quotes together,
  2642. we get a single block quote:
  2643. ```````````````````````````````` example
  2644. > foo
  2645. > bar
  2646. .
  2647. <blockquote>
  2648. <p>foo
  2649. bar</p>
  2650. </blockquote>
  2651. ````````````````````````````````
  2652. To get a block quote with two paragraphs, use:
  2653. ```````````````````````````````` example
  2654. > foo
  2655. >
  2656. > bar
  2657. .
  2658. <blockquote>
  2659. <p>foo</p>
  2660. <p>bar</p>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. Block quotes can interrupt paragraphs:
  2664. ```````````````````````````````` example
  2665. foo
  2666. > bar
  2667. .
  2668. <p>foo</p>
  2669. <blockquote>
  2670. <p>bar</p>
  2671. </blockquote>
  2672. ````````````````````````````````
  2673. In general, blank lines are not needed before or after block
  2674. quotes:
  2675. ```````````````````````````````` example
  2676. > aaa
  2677. ***
  2678. > bbb
  2679. .
  2680. <blockquote>
  2681. <p>aaa</p>
  2682. </blockquote>
  2683. <hr />
  2684. <blockquote>
  2685. <p>bbb</p>
  2686. </blockquote>
  2687. ````````````````````````````````
  2688. However, because of laziness, a blank line is needed between
  2689. a block quote and a following paragraph:
  2690. ```````````````````````````````` example
  2691. > bar
  2692. baz
  2693. .
  2694. <blockquote>
  2695. <p>bar
  2696. baz</p>
  2697. </blockquote>
  2698. ````````````````````````````````
  2699. ```````````````````````````````` example
  2700. > bar
  2701. baz
  2702. .
  2703. <blockquote>
  2704. <p>bar</p>
  2705. </blockquote>
  2706. <p>baz</p>
  2707. ````````````````````````````````
  2708. ```````````````````````````````` example
  2709. > bar
  2710. >
  2711. baz
  2712. .
  2713. <blockquote>
  2714. <p>bar</p>
  2715. </blockquote>
  2716. <p>baz</p>
  2717. ````````````````````````````````
  2718. It is a consequence of the Laziness rule that any number
  2719. of initial `>`s may be omitted on a continuation line of a
  2720. nested block quote:
  2721. ```````````````````````````````` example
  2722. > > > foo
  2723. bar
  2724. .
  2725. <blockquote>
  2726. <blockquote>
  2727. <blockquote>
  2728. <p>foo
  2729. bar</p>
  2730. </blockquote>
  2731. </blockquote>
  2732. </blockquote>
  2733. ````````````````````````````````
  2734. ```````````````````````````````` example
  2735. >>> foo
  2736. > bar
  2737. >>baz
  2738. .
  2739. <blockquote>
  2740. <blockquote>
  2741. <blockquote>
  2742. <p>foo
  2743. bar
  2744. baz</p>
  2745. </blockquote>
  2746. </blockquote>
  2747. </blockquote>
  2748. ````````````````````````````````
  2749. When including an indented code block in a block quote,
  2750. remember that the [block quote marker] includes
  2751. both the `>` and a following space. So *five spaces* are needed after
  2752. the `>`:
  2753. ```````````````````````````````` example
  2754. > code
  2755. > not code
  2756. .
  2757. <blockquote>
  2758. <pre><code>code
  2759. </code></pre>
  2760. </blockquote>
  2761. <blockquote>
  2762. <p>not code</p>
  2763. </blockquote>
  2764. ````````````````````````````````
  2765. ## List items
  2766. A [list marker](@) is a
  2767. [bullet list marker] or an [ordered list marker].
  2768. A [bullet list marker](@)
  2769. is a `-`, `+`, or `*` character.
  2770. An [ordered list marker](@)
  2771. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2772. `.` character or a `)` character. (The reason for the length
  2773. limit is that with 10 digits we start seeing integer overflows
  2774. in some browsers.)
  2775. The following rules define [list items]:
  2776. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2777. blocks *Bs* starting with a [non-whitespace character] and not separated
  2778. from each other by more than one blank line, and *M* is a list
  2779. marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2780. of prepending *M* and the following spaces to the first line of
  2781. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2782. list item with *Bs* as its contents. The type of the list item
  2783. (bullet or ordered) is determined by the type of its list marker.
  2784. If the list item is ordered, then it is also assigned a start
  2785. number, based on the ordered list marker.
  2786. Exceptions: When the first list item in a [list] interrupts
  2787. a paragraph---that is, when it starts on a line that would
  2788. otherwise count as [paragraph continuation text]---then (a)
  2789. the lines *Ls* must not begin with a blank line, and (b) if
  2790. the list item is ordered, the start number must be 1.
  2791. For example, let *Ls* be the lines
  2792. ```````````````````````````````` example
  2793. A paragraph
  2794. with two lines.
  2795. indented code
  2796. > A block quote.
  2797. .
  2798. <p>A paragraph
  2799. with two lines.</p>
  2800. <pre><code>indented code
  2801. </code></pre>
  2802. <blockquote>
  2803. <p>A block quote.</p>
  2804. </blockquote>
  2805. ````````````````````````````````
  2806. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2807. that the following is an ordered list item with start number 1,
  2808. and the same contents as *Ls*:
  2809. ```````````````````````````````` example
  2810. 1. A paragraph
  2811. with two lines.
  2812. indented code
  2813. > A block quote.
  2814. .
  2815. <ol>
  2816. <li>
  2817. <p>A paragraph
  2818. with two lines.</p>
  2819. <pre><code>indented code
  2820. </code></pre>
  2821. <blockquote>
  2822. <p>A block quote.</p>
  2823. </blockquote>
  2824. </li>
  2825. </ol>
  2826. ````````````````````````````````
  2827. The most important thing to notice is that the position of
  2828. the text after the list marker determines how much indentation
  2829. is needed in subsequent blocks in the list item. If the list
  2830. marker takes up two spaces, and there are three spaces between
  2831. the list marker and the next [non-whitespace character], then blocks
  2832. must be indented five spaces in order to fall under the list
  2833. item.
  2834. Here are some examples showing how far content must be indented to be
  2835. put under the list item:
  2836. ```````````````````````````````` example
  2837. - one
  2838. two
  2839. .
  2840. <ul>
  2841. <li>one</li>
  2842. </ul>
  2843. <p>two</p>
  2844. ````````````````````````````````
  2845. ```````````````````````````````` example
  2846. - one
  2847. two
  2848. .
  2849. <ul>
  2850. <li>
  2851. <p>one</p>
  2852. <p>two</p>
  2853. </li>
  2854. </ul>
  2855. ````````````````````````````````
  2856. ```````````````````````````````` example
  2857. - one
  2858. two
  2859. .
  2860. <ul>
  2861. <li>one</li>
  2862. </ul>
  2863. <pre><code> two
  2864. </code></pre>
  2865. ````````````````````````````````
  2866. ```````````````````````````````` example
  2867. - one
  2868. two
  2869. .
  2870. <ul>
  2871. <li>
  2872. <p>one</p>
  2873. <p>two</p>
  2874. </li>
  2875. </ul>
  2876. ````````````````````````````````
  2877. It is tempting to think of this in terms of columns: the continuation
  2878. blocks must be indented at least to the column of the first
  2879. [non-whitespace character] after the list marker. However, that is not quite right.
  2880. The spaces after the list marker determine how much relative indentation
  2881. is needed. Which column this indentation reaches will depend on
  2882. how the list item is embedded in other constructions, as shown by
  2883. this example:
  2884. ```````````````````````````````` example
  2885. > > 1. one
  2886. >>
  2887. >> two
  2888. .
  2889. <blockquote>
  2890. <blockquote>
  2891. <ol>
  2892. <li>
  2893. <p>one</p>
  2894. <p>two</p>
  2895. </li>
  2896. </ol>
  2897. </blockquote>
  2898. </blockquote>
  2899. ````````````````````````````````
  2900. Here `two` occurs in the same column as the list marker `1.`,
  2901. but is actually contained in the list item, because there is
  2902. sufficient indentation after the last containing blockquote marker.
  2903. The converse is also possible. In the following example, the word `two`
  2904. occurs far to the right of the initial text of the list item, `one`, but
  2905. it is not considered part of the list item, because it is not indented
  2906. far enough past the blockquote marker:
  2907. ```````````````````````````````` example
  2908. >>- one
  2909. >>
  2910. > > two
  2911. .
  2912. <blockquote>
  2913. <blockquote>
  2914. <ul>
  2915. <li>one</li>
  2916. </ul>
  2917. <p>two</p>
  2918. </blockquote>
  2919. </blockquote>
  2920. ````````````````````````````````
  2921. Note that at least one space is needed between the list marker and
  2922. any following content, so these are not list items:
  2923. ```````````````````````````````` example
  2924. -one
  2925. 2.two
  2926. .
  2927. <p>-one</p>
  2928. <p>2.two</p>
  2929. ````````````````````````````````
  2930. A list item may contain blocks that are separated by more than
  2931. one blank line.
  2932. ```````````````````````````````` example
  2933. - foo
  2934. bar
  2935. .
  2936. <ul>
  2937. <li>
  2938. <p>foo</p>
  2939. <p>bar</p>
  2940. </li>
  2941. </ul>
  2942. ````````````````````````````````
  2943. A list item may contain any kind of block:
  2944. ```````````````````````````````` example
  2945. 1. foo
  2946. ```
  2947. bar
  2948. ```
  2949. baz
  2950. > bam
  2951. .
  2952. <ol>
  2953. <li>
  2954. <p>foo</p>
  2955. <pre><code>bar
  2956. </code></pre>
  2957. <p>baz</p>
  2958. <blockquote>
  2959. <p>bam</p>
  2960. </blockquote>
  2961. </li>
  2962. </ol>
  2963. ````````````````````````````````
  2964. A list item that contains an indented code block will preserve
  2965. empty lines within the code block verbatim.
  2966. ```````````````````````````````` example
  2967. - Foo
  2968. bar
  2969. baz
  2970. .
  2971. <ul>
  2972. <li>
  2973. <p>Foo</p>
  2974. <pre><code>bar
  2975. baz
  2976. </code></pre>
  2977. </li>
  2978. </ul>
  2979. ````````````````````````````````
  2980. Note that ordered list start numbers must be nine digits or less:
  2981. ```````````````````````````````` example
  2982. 123456789. ok
  2983. .
  2984. <ol start="123456789">
  2985. <li>ok</li>
  2986. </ol>
  2987. ````````````````````````````````
  2988. ```````````````````````````````` example
  2989. 1234567890. not ok
  2990. .
  2991. <p>1234567890. not ok</p>
  2992. ````````````````````````````````
  2993. A start number may begin with 0s:
  2994. ```````````````````````````````` example
  2995. 0. ok
  2996. .
  2997. <ol start="0">
  2998. <li>ok</li>
  2999. </ol>
  3000. ````````````````````````````````
  3001. ```````````````````````````````` example
  3002. 003. ok
  3003. .
  3004. <ol start="3">
  3005. <li>ok</li>
  3006. </ol>
  3007. ````````````````````````````````
  3008. A start number may not be negative:
  3009. ```````````````````````````````` example
  3010. -1. not ok
  3011. .
  3012. <p>-1. not ok</p>
  3013. ````````````````````````````````
  3014. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3015. constitute a sequence of blocks *Bs* starting with an indented code
  3016. block and not separated from each other by more than one blank line,
  3017. and *M* is a list marker of width *W* followed by
  3018. one space, then the result of prepending *M* and the following
  3019. space to the first line of *Ls*, and indenting subsequent lines of
  3020. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3021. If a line is empty, then it need not be indented. The type of the
  3022. list item (bullet or ordered) is determined by the type of its list
  3023. marker. If the list item is ordered, then it is also assigned a
  3024. start number, based on the ordered list marker.
  3025. An indented code block will have to be indented four spaces beyond
  3026. the edge of the region where text will be included in the list item.
  3027. In the following case that is 6 spaces:
  3028. ```````````````````````````````` example
  3029. - foo
  3030. bar
  3031. .
  3032. <ul>
  3033. <li>
  3034. <p>foo</p>
  3035. <pre><code>bar
  3036. </code></pre>
  3037. </li>
  3038. </ul>
  3039. ````````````````````````````````
  3040. And in this case it is 11 spaces:
  3041. ```````````````````````````````` example
  3042. 10. foo
  3043. bar
  3044. .
  3045. <ol start="10">
  3046. <li>
  3047. <p>foo</p>
  3048. <pre><code>bar
  3049. </code></pre>
  3050. </li>
  3051. </ol>
  3052. ````````````````````````````````
  3053. If the *first* block in the list item is an indented code block,
  3054. then by rule #2, the contents must be indented *one* space after the
  3055. list marker:
  3056. ```````````````````````````````` example
  3057. indented code
  3058. paragraph
  3059. more code
  3060. .
  3061. <pre><code>indented code
  3062. </code></pre>
  3063. <p>paragraph</p>
  3064. <pre><code>more code
  3065. </code></pre>
  3066. ````````````````````````````````
  3067. ```````````````````````````````` example
  3068. 1. indented code
  3069. paragraph
  3070. more code
  3071. .
  3072. <ol>
  3073. <li>
  3074. <pre><code>indented code
  3075. </code></pre>
  3076. <p>paragraph</p>
  3077. <pre><code>more code
  3078. </code></pre>
  3079. </li>
  3080. </ol>
  3081. ````````````````````````````````
  3082. Note that an additional space indent is interpreted as space
  3083. inside the code block:
  3084. ```````````````````````````````` example
  3085. 1. indented code
  3086. paragraph
  3087. more code
  3088. .
  3089. <ol>
  3090. <li>
  3091. <pre><code> indented code
  3092. </code></pre>
  3093. <p>paragraph</p>
  3094. <pre><code>more code
  3095. </code></pre>
  3096. </li>
  3097. </ol>
  3098. ````````````````````````````````
  3099. Note that rules #1 and #2 only apply to two cases: (a) cases
  3100. in which the lines to be included in a list item begin with a
  3101. [non-whitespace character], and (b) cases in which
  3102. they begin with an indented code
  3103. block. In a case like the following, where the first block begins with
  3104. a three-space indent, the rules do not allow us to form a list item by
  3105. indenting the whole thing and prepending a list marker:
  3106. ```````````````````````````````` example
  3107. foo
  3108. bar
  3109. .
  3110. <p>foo</p>
  3111. <p>bar</p>
  3112. ````````````````````````````````
  3113. ```````````````````````````````` example
  3114. - foo
  3115. bar
  3116. .
  3117. <ul>
  3118. <li>foo</li>
  3119. </ul>
  3120. <p>bar</p>
  3121. ````````````````````````````````
  3122. This is not a significant restriction, because when a block begins
  3123. with 1-3 spaces indent, the indentation can always be removed without
  3124. a change in interpretation, allowing rule #1 to be applied. So, in
  3125. the above case:
  3126. ```````````````````````````````` example
  3127. - foo
  3128. bar
  3129. .
  3130. <ul>
  3131. <li>
  3132. <p>foo</p>
  3133. <p>bar</p>
  3134. </li>
  3135. </ul>
  3136. ````````````````````````````````
  3137. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3138. starting with a single [blank line] constitute a (possibly empty)
  3139. sequence of blocks *Bs*, not separated from each other by more than
  3140. one blank line, and *M* is a list marker of width *W*,
  3141. then the result of prepending *M* to the first line of *Ls*, and
  3142. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3143. item with *Bs* as its contents.
  3144. If a line is empty, then it need not be indented. The type of the
  3145. list item (bullet or ordered) is determined by the type of its list
  3146. marker. If the list item is ordered, then it is also assigned a
  3147. start number, based on the ordered list marker.
  3148. Here are some list items that start with a blank line but are not empty:
  3149. ```````````````````````````````` example
  3150. -
  3151. foo
  3152. -
  3153. ```
  3154. bar
  3155. ```
  3156. -
  3157. baz
  3158. .
  3159. <ul>
  3160. <li>foo</li>
  3161. <li>
  3162. <pre><code>bar
  3163. </code></pre>
  3164. </li>
  3165. <li>
  3166. <pre><code>baz
  3167. </code></pre>
  3168. </li>
  3169. </ul>
  3170. ````````````````````````````````
  3171. When the list item starts with a blank line, the number of spaces
  3172. following the list marker doesn't change the required indentation:
  3173. ```````````````````````````````` example
  3174. -
  3175. foo
  3176. .
  3177. <ul>
  3178. <li>foo</li>
  3179. </ul>
  3180. ````````````````````````````````
  3181. A list item can begin with at most one blank line.
  3182. In the following example, `foo` is not part of the list
  3183. item:
  3184. ```````````````````````````````` example
  3185. -
  3186. foo
  3187. .
  3188. <ul>
  3189. <li></li>
  3190. </ul>
  3191. <p>foo</p>
  3192. ````````````````````````````````
  3193. Here is an empty bullet list item:
  3194. ```````````````````````````````` example
  3195. - foo
  3196. -
  3197. - bar
  3198. .
  3199. <ul>
  3200. <li>foo</li>
  3201. <li></li>
  3202. <li>bar</li>
  3203. </ul>
  3204. ````````````````````````````````
  3205. It does not matter whether there are spaces following the [list marker]:
  3206. ```````````````````````````````` example
  3207. - foo
  3208. -
  3209. - bar
  3210. .
  3211. <ul>
  3212. <li>foo</li>
  3213. <li></li>
  3214. <li>bar</li>
  3215. </ul>
  3216. ````````````````````````````````
  3217. Here is an empty ordered list item:
  3218. ```````````````````````````````` example
  3219. 1. foo
  3220. 2.
  3221. 3. bar
  3222. .
  3223. <ol>
  3224. <li>foo</li>
  3225. <li></li>
  3226. <li>bar</li>
  3227. </ol>
  3228. ````````````````````````````````
  3229. A list may start or end with an empty list item:
  3230. ```````````````````````````````` example
  3231. *
  3232. .
  3233. <ul>
  3234. <li></li>
  3235. </ul>
  3236. ````````````````````````````````
  3237. However, an empty list item cannot interrupt a paragraph:
  3238. ```````````````````````````````` example
  3239. foo
  3240. *
  3241. foo
  3242. 1.
  3243. .
  3244. <p>foo
  3245. *</p>
  3246. <p>foo
  3247. 1.</p>
  3248. ````````````````````````````````
  3249. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3250. according to rule #1, #2, or #3, then the result of indenting each line
  3251. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3252. list item with the same contents and attributes. If a line is
  3253. empty, then it need not be indented.
  3254. Indented one space:
  3255. ```````````````````````````````` example
  3256. 1. A paragraph
  3257. with two lines.
  3258. indented code
  3259. > A block quote.
  3260. .
  3261. <ol>
  3262. <li>
  3263. <p>A paragraph
  3264. with two lines.</p>
  3265. <pre><code>indented code
  3266. </code></pre>
  3267. <blockquote>
  3268. <p>A block quote.</p>
  3269. </blockquote>
  3270. </li>
  3271. </ol>
  3272. ````````````````````````````````
  3273. Indented two spaces:
  3274. ```````````````````````````````` example
  3275. 1. A paragraph
  3276. with two lines.
  3277. indented code
  3278. > A block quote.
  3279. .
  3280. <ol>
  3281. <li>
  3282. <p>A paragraph
  3283. with two lines.</p>
  3284. <pre><code>indented code
  3285. </code></pre>
  3286. <blockquote>
  3287. <p>A block quote.</p>
  3288. </blockquote>
  3289. </li>
  3290. </ol>
  3291. ````````````````````````````````
  3292. Indented three spaces:
  3293. ```````````````````````````````` example
  3294. 1. A paragraph
  3295. with two lines.
  3296. indented code
  3297. > A block quote.
  3298. .
  3299. <ol>
  3300. <li>
  3301. <p>A paragraph
  3302. with two lines.</p>
  3303. <pre><code>indented code
  3304. </code></pre>
  3305. <blockquote>
  3306. <p>A block quote.</p>
  3307. </blockquote>
  3308. </li>
  3309. </ol>
  3310. ````````````````````````````````
  3311. Four spaces indent gives a code block:
  3312. ```````````````````````````````` example
  3313. 1. A paragraph
  3314. with two lines.
  3315. indented code
  3316. > A block quote.
  3317. .
  3318. <pre><code>1. A paragraph
  3319. with two lines.
  3320. indented code
  3321. &gt; A block quote.
  3322. </code></pre>
  3323. ````````````````````````````````
  3324. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3325. item](#list-items) with contents *Bs*, then the result of deleting
  3326. some or all of the indentation from one or more lines in which the
  3327. next [non-whitespace character] after the indentation is
  3328. [paragraph continuation text] is a
  3329. list item with the same contents and attributes. The unindented
  3330. lines are called
  3331. [lazy continuation line](@)s.
  3332. Here is an example with [lazy continuation lines]:
  3333. ```````````````````````````````` example
  3334. 1. A paragraph
  3335. with two lines.
  3336. indented code
  3337. > A block quote.
  3338. .
  3339. <ol>
  3340. <li>
  3341. <p>A paragraph
  3342. with two lines.</p>
  3343. <pre><code>indented code
  3344. </code></pre>
  3345. <blockquote>
  3346. <p>A block quote.</p>
  3347. </blockquote>
  3348. </li>
  3349. </ol>
  3350. ````````````````````````````````
  3351. Indentation can be partially deleted:
  3352. ```````````````````````````````` example
  3353. 1. A paragraph
  3354. with two lines.
  3355. .
  3356. <ol>
  3357. <li>A paragraph
  3358. with two lines.</li>
  3359. </ol>
  3360. ````````````````````````````````
  3361. These examples show how laziness can work in nested structures:
  3362. ```````````````````````````````` example
  3363. > 1. > Blockquote
  3364. continued here.
  3365. .
  3366. <blockquote>
  3367. <ol>
  3368. <li>
  3369. <blockquote>
  3370. <p>Blockquote
  3371. continued here.</p>
  3372. </blockquote>
  3373. </li>
  3374. </ol>
  3375. </blockquote>
  3376. ````````````````````````````````
  3377. ```````````````````````````````` example
  3378. > 1. > Blockquote
  3379. > continued here.
  3380. .
  3381. <blockquote>
  3382. <ol>
  3383. <li>
  3384. <blockquote>
  3385. <p>Blockquote
  3386. continued here.</p>
  3387. </blockquote>
  3388. </li>
  3389. </ol>
  3390. </blockquote>
  3391. ````````````````````````````````
  3392. 6. **That's all.** Nothing that is not counted as a list item by rules
  3393. #1--5 counts as a [list item](#list-items).
  3394. The rules for sublists follow from the general rules above. A sublist
  3395. must be indented the same number of spaces a paragraph would need to be
  3396. in order to be included in the list item.
  3397. So, in this case we need two spaces indent:
  3398. ```````````````````````````````` example
  3399. - foo
  3400. - bar
  3401. - baz
  3402. - boo
  3403. .
  3404. <ul>
  3405. <li>foo
  3406. <ul>
  3407. <li>bar
  3408. <ul>
  3409. <li>baz
  3410. <ul>
  3411. <li>boo</li>
  3412. </ul>
  3413. </li>
  3414. </ul>
  3415. </li>
  3416. </ul>
  3417. </li>
  3418. </ul>
  3419. ````````````````````````````````
  3420. One is not enough:
  3421. ```````````````````````````````` example
  3422. - foo
  3423. - bar
  3424. - baz
  3425. - boo
  3426. .
  3427. <ul>
  3428. <li>foo</li>
  3429. <li>bar</li>
  3430. <li>baz</li>
  3431. <li>boo</li>
  3432. </ul>
  3433. ````````````````````````````````
  3434. Here we need four, because the list marker is wider:
  3435. ```````````````````````````````` example
  3436. 10) foo
  3437. - bar
  3438. .
  3439. <ol start="10">
  3440. <li>foo
  3441. <ul>
  3442. <li>bar</li>
  3443. </ul>
  3444. </li>
  3445. </ol>
  3446. ````````````````````````````````
  3447. Three is not enough:
  3448. ```````````````````````````````` example
  3449. 10) foo
  3450. - bar
  3451. .
  3452. <ol start="10">
  3453. <li>foo</li>
  3454. </ol>
  3455. <ul>
  3456. <li>bar</li>
  3457. </ul>
  3458. ````````````````````````````````
  3459. A list may be the first block in a list item:
  3460. ```````````````````````````````` example
  3461. - - foo
  3462. .
  3463. <ul>
  3464. <li>
  3465. <ul>
  3466. <li>foo</li>
  3467. </ul>
  3468. </li>
  3469. </ul>
  3470. ````````````````````````````````
  3471. ```````````````````````````````` example
  3472. 1. - 2. foo
  3473. .
  3474. <ol>
  3475. <li>
  3476. <ul>
  3477. <li>
  3478. <ol start="2">
  3479. <li>foo</li>
  3480. </ol>
  3481. </li>
  3482. </ul>
  3483. </li>
  3484. </ol>
  3485. ````````````````````````````````
  3486. A list item can contain a heading:
  3487. ```````````````````````````````` example
  3488. - # Foo
  3489. - Bar
  3490. ---
  3491. baz
  3492. .
  3493. <ul>
  3494. <li>
  3495. <h1>Foo</h1>
  3496. </li>
  3497. <li>
  3498. <h2>Bar</h2>
  3499. baz</li>
  3500. </ul>
  3501. ````````````````````````````````
  3502. ### Motivation
  3503. John Gruber's Markdown spec says the following about list items:
  3504. 1. "List markers typically start at the left margin, but may be indented
  3505. by up to three spaces. List markers must be followed by one or more
  3506. spaces or a tab."
  3507. 2. "To make lists look nice, you can wrap items with hanging indents....
  3508. But if you don't want to, you don't have to."
  3509. 3. "List items may consist of multiple paragraphs. Each subsequent
  3510. paragraph in a list item must be indented by either 4 spaces or one
  3511. tab."
  3512. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3513. but here again, Markdown will allow you to be lazy."
  3514. 5. "To put a blockquote within a list item, the blockquote's `>`
  3515. delimiters need to be indented."
  3516. 6. "To put a code block within a list item, the code block needs to be
  3517. indented twice — 8 spaces or two tabs."
  3518. These rules specify that a paragraph under a list item must be indented
  3519. four spaces (presumably, from the left margin, rather than the start of
  3520. the list marker, but this is not said), and that code under a list item
  3521. must be indented eight spaces instead of the usual four. They also say
  3522. that a block quote must be indented, but not by how much; however, the
  3523. example given has four spaces indentation. Although nothing is said
  3524. about other kinds of block-level content, it is certainly reasonable to
  3525. infer that *all* block elements under a list item, including other
  3526. lists, must be indented four spaces. This principle has been called the
  3527. *four-space rule*.
  3528. The four-space rule is clear and principled, and if the reference
  3529. implementation `Markdown.pl` had followed it, it probably would have
  3530. become the standard. However, `Markdown.pl` allowed paragraphs and
  3531. sublists to start with only two spaces indentation, at least on the
  3532. outer level. Worse, its behavior was inconsistent: a sublist of an
  3533. outer-level list needed two spaces indentation, but a sublist of this
  3534. sublist needed three spaces. It is not surprising, then, that different
  3535. implementations of Markdown have developed very different rules for
  3536. determining what comes under a list item. (Pandoc and python-Markdown,
  3537. for example, stuck with Gruber's syntax description and the four-space
  3538. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3539. followed `Markdown.pl`'s behavior more closely.)
  3540. Unfortunately, given the divergences between implementations, there
  3541. is no way to give a spec for list items that will be guaranteed not
  3542. to break any existing documents. However, the spec given here should
  3543. correctly handle lists formatted with either the four-space rule or
  3544. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3545. in a way that is natural for a human to read.
  3546. The strategy here is to let the width and indentation of the list marker
  3547. determine the indentation necessary for blocks to fall under the list
  3548. item, rather than having a fixed and arbitrary number. The writer can
  3549. think of the body of the list item as a unit which gets indented to the
  3550. right enough to fit the list marker (and any indentation on the list
  3551. marker). (The laziness rule, #5, then allows continuation lines to be
  3552. unindented if needed.)
  3553. This rule is superior, we claim, to any rule requiring a fixed level of
  3554. indentation from the margin. The four-space rule is clear but
  3555. unnatural. It is quite unintuitive that
  3556. ``` markdown
  3557. - foo
  3558. bar
  3559. - baz
  3560. ```
  3561. should be parsed as two lists with an intervening paragraph,
  3562. ``` html
  3563. <ul>
  3564. <li>foo</li>
  3565. </ul>
  3566. <p>bar</p>
  3567. <ul>
  3568. <li>baz</li>
  3569. </ul>
  3570. ```
  3571. as the four-space rule demands, rather than a single list,
  3572. ``` html
  3573. <ul>
  3574. <li>
  3575. <p>foo</p>
  3576. <p>bar</p>
  3577. <ul>
  3578. <li>baz</li>
  3579. </ul>
  3580. </li>
  3581. </ul>
  3582. ```
  3583. The choice of four spaces is arbitrary. It can be learned, but it is
  3584. not likely to be guessed, and it trips up beginners regularly.
  3585. Would it help to adopt a two-space rule? The problem is that such
  3586. a rule, together with the rule allowing 1--3 spaces indentation of the
  3587. initial list marker, allows text that is indented *less than* the
  3588. original list marker to be included in the list item. For example,
  3589. `Markdown.pl` parses
  3590. ``` markdown
  3591. - one
  3592. two
  3593. ```
  3594. as a single list item, with `two` a continuation paragraph:
  3595. ``` html
  3596. <ul>
  3597. <li>
  3598. <p>one</p>
  3599. <p>two</p>
  3600. </li>
  3601. </ul>
  3602. ```
  3603. and similarly
  3604. ``` markdown
  3605. > - one
  3606. >
  3607. > two
  3608. ```
  3609. as
  3610. ``` html
  3611. <blockquote>
  3612. <ul>
  3613. <li>
  3614. <p>one</p>
  3615. <p>two</p>
  3616. </li>
  3617. </ul>
  3618. </blockquote>
  3619. ```
  3620. This is extremely unintuitive.
  3621. Rather than requiring a fixed indent from the margin, we could require
  3622. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3623. may itself be indented). This proposal would remove the last anomaly
  3624. discussed. Unlike the spec presented above, it would count the following
  3625. as a list item with a subparagraph, even though the paragraph `bar`
  3626. is not indented as far as the first paragraph `foo`:
  3627. ``` markdown
  3628. 10. foo
  3629. bar
  3630. ```
  3631. Arguably this text does read like a list item with `bar` as a subparagraph,
  3632. which may count in favor of the proposal. However, on this proposal indented
  3633. code would have to be indented six spaces after the list marker. And this
  3634. would break a lot of existing Markdown, which has the pattern:
  3635. ``` markdown
  3636. 1. foo
  3637. indented code
  3638. ```
  3639. where the code is indented eight spaces. The spec above, by contrast, will
  3640. parse this text as expected, since the code block's indentation is measured
  3641. from the beginning of `foo`.
  3642. The one case that needs special treatment is a list item that *starts*
  3643. with indented code. How much indentation is required in that case, since
  3644. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3645. that in such cases, we require one space indentation from the list marker
  3646. (and then the normal four spaces for the indented code). This will match the
  3647. four-space rule in cases where the list marker plus its initial indentation
  3648. takes four spaces (a common case), but diverge in other cases.
  3649. ## Lists
  3650. A [list](@) is a sequence of one or more
  3651. list items [of the same type]. The list items
  3652. may be separated by any number of blank lines.
  3653. Two list items are [of the same type](@)
  3654. if they begin with a [list marker] of the same type.
  3655. Two list markers are of the
  3656. same type if (a) they are bullet list markers using the same character
  3657. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3658. delimiter (either `.` or `)`).
  3659. A list is an [ordered list](@)
  3660. if its constituent list items begin with
  3661. [ordered list markers], and a
  3662. [bullet list](@) if its constituent list
  3663. items begin with [bullet list markers].
  3664. The [start number](@)
  3665. of an [ordered list] is determined by the list number of
  3666. its initial list item. The numbers of subsequent list items are
  3667. disregarded.
  3668. A list is [loose](@) if any of its constituent
  3669. list items are separated by blank lines, or if any of its constituent
  3670. list items directly contain two block-level elements with a blank line
  3671. between them. Otherwise a list is [tight](@).
  3672. (The difference in HTML output is that paragraphs in a loose list are
  3673. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3674. Changing the bullet or ordered list delimiter starts a new list:
  3675. ```````````````````````````````` example
  3676. - foo
  3677. - bar
  3678. + baz
  3679. .
  3680. <ul>
  3681. <li>foo</li>
  3682. <li>bar</li>
  3683. </ul>
  3684. <ul>
  3685. <li>baz</li>
  3686. </ul>
  3687. ````````````````````````````````
  3688. ```````````````````````````````` example
  3689. 1. foo
  3690. 2. bar
  3691. 3) baz
  3692. .
  3693. <ol>
  3694. <li>foo</li>
  3695. <li>bar</li>
  3696. </ol>
  3697. <ol start="3">
  3698. <li>baz</li>
  3699. </ol>
  3700. ````````````````````````````````
  3701. In CommonMark, a list can interrupt a paragraph. That is,
  3702. no blank line is needed to separate a paragraph from a following
  3703. list:
  3704. ```````````````````````````````` example
  3705. Foo
  3706. - bar
  3707. - baz
  3708. .
  3709. <p>Foo</p>
  3710. <ul>
  3711. <li>bar</li>
  3712. <li>baz</li>
  3713. </ul>
  3714. ````````````````````````````````
  3715. `Markdown.pl` does not allow this, through fear of triggering a list
  3716. via a numeral in a hard-wrapped line:
  3717. ``` markdown
  3718. The number of windows in my house is
  3719. 14. The number of doors is 6.
  3720. ```
  3721. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3722. interrupt a paragraph, even though the same considerations might
  3723. apply.
  3724. In CommonMark, we do allow lists to interrupt paragraphs, for
  3725. two reasons. First, it is natural and not uncommon for people
  3726. to start lists without blank lines:
  3727. ``` markdown
  3728. I need to buy
  3729. - new shoes
  3730. - a coat
  3731. - a plane ticket
  3732. ```
  3733. Second, we are attracted to a
  3734. > [principle of uniformity](@):
  3735. > if a chunk of text has a certain
  3736. > meaning, it will continue to have the same meaning when put into a
  3737. > container block (such as a list item or blockquote).
  3738. (Indeed, the spec for [list items] and [block quotes] presupposes
  3739. this principle.) This principle implies that if
  3740. ``` markdown
  3741. * I need to buy
  3742. - new shoes
  3743. - a coat
  3744. - a plane ticket
  3745. ```
  3746. is a list item containing a paragraph followed by a nested sublist,
  3747. as all Markdown implementations agree it is (though the paragraph
  3748. may be rendered without `<p>` tags, since the list is "tight"),
  3749. then
  3750. ``` markdown
  3751. I need to buy
  3752. - new shoes
  3753. - a coat
  3754. - a plane ticket
  3755. ```
  3756. by itself should be a paragraph followed by a nested sublist.
  3757. Since it is well established Markdown practice to allow lists to
  3758. interrupt paragraphs inside list items, the [principle of
  3759. uniformity] requires us to allow this outside list items as
  3760. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3761. takes a different approach, requiring blank lines before lists
  3762. even inside other list items.)
  3763. In order to solve of unwanted lists in paragraphs with
  3764. hard-wrapped numerals, we allow only lists starting with `1` to
  3765. interrupt paragraphs. Thus,
  3766. ```````````````````````````````` example
  3767. The number of windows in my house is
  3768. 14. The number of doors is 6.
  3769. .
  3770. <p>The number of windows in my house is
  3771. 14. The number of doors is 6.</p>
  3772. ````````````````````````````````
  3773. We may still get an unintended result in cases like
  3774. ```````````````````````````````` example
  3775. The number of windows in my house is
  3776. 1. The number of doors is 6.
  3777. .
  3778. <p>The number of windows in my house is</p>
  3779. <ol>
  3780. <li>The number of doors is 6.</li>
  3781. </ol>
  3782. ````````````````````````````````
  3783. but this rule should prevent most spurious list captures.
  3784. There can be any number of blank lines between items:
  3785. ```````````````````````````````` example
  3786. - foo
  3787. - bar
  3788. - baz
  3789. .
  3790. <ul>
  3791. <li>
  3792. <p>foo</p>
  3793. </li>
  3794. <li>
  3795. <p>bar</p>
  3796. </li>
  3797. <li>
  3798. <p>baz</p>
  3799. </li>
  3800. </ul>
  3801. ````````````````````````````````
  3802. ```````````````````````````````` example
  3803. - foo
  3804. - bar
  3805. - baz
  3806. bim
  3807. .
  3808. <ul>
  3809. <li>foo
  3810. <ul>
  3811. <li>bar
  3812. <ul>
  3813. <li>
  3814. <p>baz</p>
  3815. <p>bim</p>
  3816. </li>
  3817. </ul>
  3818. </li>
  3819. </ul>
  3820. </li>
  3821. </ul>
  3822. ````````````````````````````````
  3823. To separate consecutive lists of the same type, or to separate a
  3824. list from an indented code block that would otherwise be parsed
  3825. as a subparagraph of the final list item, you can insert a blank HTML
  3826. comment:
  3827. ```````````````````````````````` example
  3828. - foo
  3829. - bar
  3830. <!-- -->
  3831. - baz
  3832. - bim
  3833. .
  3834. <ul>
  3835. <li>foo</li>
  3836. <li>bar</li>
  3837. </ul>
  3838. <!-- -->
  3839. <ul>
  3840. <li>baz</li>
  3841. <li>bim</li>
  3842. </ul>
  3843. ````````````````````````````````
  3844. ```````````````````````````````` example
  3845. - foo
  3846. notcode
  3847. - foo
  3848. <!-- -->
  3849. code
  3850. .
  3851. <ul>
  3852. <li>
  3853. <p>foo</p>
  3854. <p>notcode</p>
  3855. </li>
  3856. <li>
  3857. <p>foo</p>
  3858. </li>
  3859. </ul>
  3860. <!-- -->
  3861. <pre><code>code
  3862. </code></pre>
  3863. ````````````````````````````````
  3864. List items need not be indented to the same level. The following
  3865. list items will be treated as items at the same list level,
  3866. since none is indented enough to belong to the previous list
  3867. item:
  3868. ```````````````````````````````` example
  3869. - a
  3870. - b
  3871. - c
  3872. - d
  3873. - e
  3874. - f
  3875. - g
  3876. - h
  3877. - i
  3878. .
  3879. <ul>
  3880. <li>a</li>
  3881. <li>b</li>
  3882. <li>c</li>
  3883. <li>d</li>
  3884. <li>e</li>
  3885. <li>f</li>
  3886. <li>g</li>
  3887. <li>h</li>
  3888. <li>i</li>
  3889. </ul>
  3890. ````````````````````````````````
  3891. ```````````````````````````````` example
  3892. 1. a
  3893. 2. b
  3894. 3. c
  3895. .
  3896. <ol>
  3897. <li>
  3898. <p>a</p>
  3899. </li>
  3900. <li>
  3901. <p>b</p>
  3902. </li>
  3903. <li>
  3904. <p>c</p>
  3905. </li>
  3906. </ol>
  3907. ````````````````````````````````
  3908. This is a loose list, because there is a blank line between
  3909. two of the list items:
  3910. ```````````````````````````````` example
  3911. - a
  3912. - b
  3913. - c
  3914. .
  3915. <ul>
  3916. <li>
  3917. <p>a</p>
  3918. </li>
  3919. <li>
  3920. <p>b</p>
  3921. </li>
  3922. <li>
  3923. <p>c</p>
  3924. </li>
  3925. </ul>
  3926. ````````````````````````````````
  3927. So is this, with a empty second item:
  3928. ```````````````````````````````` example
  3929. * a
  3930. *
  3931. * c
  3932. .
  3933. <ul>
  3934. <li>
  3935. <p>a</p>
  3936. </li>
  3937. <li></li>
  3938. <li>
  3939. <p>c</p>
  3940. </li>
  3941. </ul>
  3942. ````````````````````````````````
  3943. These are loose lists, even though there is no space between the items,
  3944. because one of the items directly contains two block-level elements
  3945. with a blank line between them:
  3946. ```````````````````````````````` example
  3947. - a
  3948. - b
  3949. c
  3950. - d
  3951. .
  3952. <ul>
  3953. <li>
  3954. <p>a</p>
  3955. </li>
  3956. <li>
  3957. <p>b</p>
  3958. <p>c</p>
  3959. </li>
  3960. <li>
  3961. <p>d</p>
  3962. </li>
  3963. </ul>
  3964. ````````````````````````````````
  3965. ```````````````````````````````` example
  3966. - a
  3967. - b
  3968. [ref]: /url
  3969. - d
  3970. .
  3971. <ul>
  3972. <li>
  3973. <p>a</p>
  3974. </li>
  3975. <li>
  3976. <p>b</p>
  3977. </li>
  3978. <li>
  3979. <p>d</p>
  3980. </li>
  3981. </ul>
  3982. ````````````````````````````````
  3983. This is a tight list, because the blank lines are in a code block:
  3984. ```````````````````````````````` example
  3985. - a
  3986. - ```
  3987. b
  3988. ```
  3989. - c
  3990. .
  3991. <ul>
  3992. <li>a</li>
  3993. <li>
  3994. <pre><code>b
  3995. </code></pre>
  3996. </li>
  3997. <li>c</li>
  3998. </ul>
  3999. ````````````````````````````````
  4000. This is a tight list, because the blank line is between two
  4001. paragraphs of a sublist. So the sublist is loose while
  4002. the outer list is tight:
  4003. ```````````````````````````````` example
  4004. - a
  4005. - b
  4006. c
  4007. - d
  4008. .
  4009. <ul>
  4010. <li>a
  4011. <ul>
  4012. <li>
  4013. <p>b</p>
  4014. <p>c</p>
  4015. </li>
  4016. </ul>
  4017. </li>
  4018. <li>d</li>
  4019. </ul>
  4020. ````````````````````````````````
  4021. This is a tight list, because the blank line is inside the
  4022. block quote:
  4023. ```````````````````````````````` example
  4024. * a
  4025. > b
  4026. >
  4027. * c
  4028. .
  4029. <ul>
  4030. <li>a
  4031. <blockquote>
  4032. <p>b</p>
  4033. </blockquote>
  4034. </li>
  4035. <li>c</li>
  4036. </ul>
  4037. ````````````````````````````````
  4038. This list is tight, because the consecutive block elements
  4039. are not separated by blank lines:
  4040. ```````````````````````````````` example
  4041. - a
  4042. > b
  4043. ```
  4044. c
  4045. ```
  4046. - d
  4047. .
  4048. <ul>
  4049. <li>a
  4050. <blockquote>
  4051. <p>b</p>
  4052. </blockquote>
  4053. <pre><code>c
  4054. </code></pre>
  4055. </li>
  4056. <li>d</li>
  4057. </ul>
  4058. ````````````````````````````````
  4059. A single-paragraph list is tight:
  4060. ```````````````````````````````` example
  4061. - a
  4062. .
  4063. <ul>
  4064. <li>a</li>
  4065. </ul>
  4066. ````````````````````````````````
  4067. ```````````````````````````````` example
  4068. - a
  4069. - b
  4070. .
  4071. <ul>
  4072. <li>a
  4073. <ul>
  4074. <li>b</li>
  4075. </ul>
  4076. </li>
  4077. </ul>
  4078. ````````````````````````````````
  4079. This list is loose, because of the blank line between the
  4080. two block elements in the list item:
  4081. ```````````````````````````````` example
  4082. 1. ```
  4083. foo
  4084. ```
  4085. bar
  4086. .
  4087. <ol>
  4088. <li>
  4089. <pre><code>foo
  4090. </code></pre>
  4091. <p>bar</p>
  4092. </li>
  4093. </ol>
  4094. ````````````````````````````````
  4095. Here the outer list is loose, the inner list tight:
  4096. ```````````````````````````````` example
  4097. * foo
  4098. * bar
  4099. baz
  4100. .
  4101. <ul>
  4102. <li>
  4103. <p>foo</p>
  4104. <ul>
  4105. <li>bar</li>
  4106. </ul>
  4107. <p>baz</p>
  4108. </li>
  4109. </ul>
  4110. ````````````````````````````````
  4111. ```````````````````````````````` example
  4112. - a
  4113. - b
  4114. - c
  4115. - d
  4116. - e
  4117. - f
  4118. .
  4119. <ul>
  4120. <li>
  4121. <p>a</p>
  4122. <ul>
  4123. <li>b</li>
  4124. <li>c</li>
  4125. </ul>
  4126. </li>
  4127. <li>
  4128. <p>d</p>
  4129. <ul>
  4130. <li>e</li>
  4131. <li>f</li>
  4132. </ul>
  4133. </li>
  4134. </ul>
  4135. ````````````````````````````````
  4136. # Inlines
  4137. Inlines are parsed sequentially from the beginning of the character
  4138. stream to the end (left to right, in left-to-right languages).
  4139. Thus, for example, in
  4140. ```````````````````````````````` example
  4141. `hi`lo`
  4142. .
  4143. <p><code>hi</code>lo`</p>
  4144. ````````````````````````````````
  4145. `hi` is parsed as code, leaving the backtick at the end as a literal
  4146. backtick.
  4147. ## Backslash escapes
  4148. Any ASCII punctuation character may be backslash-escaped:
  4149. ```````````````````````````````` example
  4150. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4151. .
  4152. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4153. ````````````````````````````````
  4154. Backslashes before other characters are treated as literal
  4155. backslashes:
  4156. ```````````````````````````````` example
  4157. \→\A\a\ \3\φ\«
  4158. .
  4159. <p>\→\A\a\ \3\φ\«</p>
  4160. ````````````````````````````````
  4161. Escaped characters are treated as regular characters and do
  4162. not have their usual Markdown meanings:
  4163. ```````````````````````````````` example
  4164. \*not emphasized*
  4165. \<br/> not a tag
  4166. \[not a link](/foo)
  4167. \`not code`
  4168. 1\. not a list
  4169. \* not a list
  4170. \# not a heading
  4171. \[foo]: /url "not a reference"
  4172. .
  4173. <p>*not emphasized*
  4174. &lt;br/&gt; not a tag
  4175. [not a link](/foo)
  4176. `not code`
  4177. 1. not a list
  4178. * not a list
  4179. # not a heading
  4180. [foo]: /url &quot;not a reference&quot;</p>
  4181. ````````````````````````````````
  4182. If a backslash is itself escaped, the following character is not:
  4183. ```````````````````````````````` example
  4184. \\*emphasis*
  4185. .
  4186. <p>\<em>emphasis</em></p>
  4187. ````````````````````````````````
  4188. A backslash at the end of the line is a [hard line break]:
  4189. ```````````````````````````````` example
  4190. foo\
  4191. bar
  4192. .
  4193. <p>foo<br />
  4194. bar</p>
  4195. ````````````````````````````````
  4196. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4197. raw HTML:
  4198. ```````````````````````````````` example
  4199. `` \[\` ``
  4200. .
  4201. <p><code>\[\`</code></p>
  4202. ````````````````````````````````
  4203. ```````````````````````````````` example
  4204. \[\]
  4205. .
  4206. <pre><code>\[\]
  4207. </code></pre>
  4208. ````````````````````````````````
  4209. ```````````````````````````````` example
  4210. ~~~
  4211. \[\]
  4212. ~~~
  4213. .
  4214. <pre><code>\[\]
  4215. </code></pre>
  4216. ````````````````````````````````
  4217. ```````````````````````````````` example
  4218. <http://example.com?find=\*>
  4219. .
  4220. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4221. ````````````````````````````````
  4222. ```````````````````````````````` example
  4223. <a href="/bar\/)">
  4224. .
  4225. <a href="/bar\/)">
  4226. ````````````````````````````````
  4227. But they work in all other contexts, including URLs and link titles,
  4228. link references, and [info strings] in [fenced code blocks]:
  4229. ```````````````````````````````` example
  4230. [foo](/bar\* "ti\*tle")
  4231. .
  4232. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4233. ````````````````````````````````
  4234. ```````````````````````````````` example
  4235. [foo]
  4236. [foo]: /bar\* "ti\*tle"
  4237. .
  4238. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4239. ````````````````````````````````
  4240. ```````````````````````````````` example
  4241. ``` foo\+bar
  4242. foo
  4243. ```
  4244. .
  4245. <pre><code class="language-foo+bar">foo
  4246. </code></pre>
  4247. ````````````````````````````````
  4248. ## Entity and numeric character references
  4249. All valid HTML entity references and numeric character
  4250. references, except those occuring in code blocks and code spans,
  4251. are recognized as such and treated as equivalent to the
  4252. corresponding Unicode characters. Conforming CommonMark parsers
  4253. need not store information about whether a particular character
  4254. was represented in the source using a Unicode character or
  4255. an entity reference.
  4256. [Entity references](@) consist of `&` + any of the valid
  4257. HTML5 entity names + `;`. The
  4258. document <https://html.spec.whatwg.org/multipage/entities.json>
  4259. is used as an authoritative source for the valid entity
  4260. references and their corresponding code points.
  4261. ```````````````````````````````` example
  4262. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4263. &frac34; &HilbertSpace; &DifferentialD;
  4264. &ClockwiseContourIntegral; &ngE;
  4265. .
  4266. <p>  &amp; © Æ Ď
  4267. ¾ ℋ ⅆ
  4268. ∲ ≧̸</p>
  4269. ````````````````````````````````
  4270. [Decimal numeric character
  4271. references](@)
  4272. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4273. numeric character reference is parsed as the corresponding
  4274. Unicode character. Invalid Unicode code points will be replaced by
  4275. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4276. the code point `U+0000` will also be replaced by `U+FFFD`.
  4277. ```````````````````````````````` example
  4278. &#35; &#1234; &#992; &#98765432; &#0;
  4279. .
  4280. <p># Ӓ Ϡ � �</p>
  4281. ````````````````````````````````
  4282. [Hexadecimal numeric character
  4283. references](@) consist of `&#` +
  4284. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4285. They too are parsed as the corresponding Unicode character (this
  4286. time specified with a hexadecimal numeral instead of decimal).
  4287. ```````````````````````````````` example
  4288. &#X22; &#XD06; &#xcab;
  4289. .
  4290. <p>&quot; ആ ಫ</p>
  4291. ````````````````````````````````
  4292. Here are some nonentities:
  4293. ```````````````````````````````` example
  4294. &nbsp &x; &#; &#x;
  4295. &ThisIsNotDefined; &hi?;
  4296. .
  4297. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4298. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4299. ````````````````````````````````
  4300. Although HTML5 does accept some entity references
  4301. without a trailing semicolon (such as `&copy`), these are not
  4302. recognized here, because it makes the grammar too ambiguous:
  4303. ```````````````````````````````` example
  4304. &copy
  4305. .
  4306. <p>&amp;copy</p>
  4307. ````````````````````````````````
  4308. Strings that are not on the list of HTML5 named entities are not
  4309. recognized as entity references either:
  4310. ```````````````````````````````` example
  4311. &MadeUpEntity;
  4312. .
  4313. <p>&amp;MadeUpEntity;</p>
  4314. ````````````````````````````````
  4315. Entity and numeric character references are recognized in any
  4316. context besides code spans or code blocks, including
  4317. URLs, [link titles], and [fenced code block][] [info strings]:
  4318. ```````````````````````````````` example
  4319. <a href="&ouml;&ouml;.html">
  4320. .
  4321. <a href="&ouml;&ouml;.html">
  4322. ````````````````````````````````
  4323. ```````````````````````````````` example
  4324. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4325. .
  4326. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4327. ````````````````````````````````
  4328. ```````````````````````````````` example
  4329. [foo]
  4330. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4331. .
  4332. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4333. ````````````````````````````````
  4334. ```````````````````````````````` example
  4335. ``` f&ouml;&ouml;
  4336. foo
  4337. ```
  4338. .
  4339. <pre><code class="language-föö">foo
  4340. </code></pre>
  4341. ````````````````````````````````
  4342. Entity and numeric character references are treated as literal
  4343. text in code spans and code blocks:
  4344. ```````````````````````````````` example
  4345. `f&ouml;&ouml;`
  4346. .
  4347. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4348. ````````````````````````````````
  4349. ```````````````````````````````` example
  4350. f&ouml;f&ouml;
  4351. .
  4352. <pre><code>f&amp;ouml;f&amp;ouml;
  4353. </code></pre>
  4354. ````````````````````````````````
  4355. ## Code spans
  4356. A [backtick string](@)
  4357. is a string of one or more backtick characters (`` ` ``) that is neither
  4358. preceded nor followed by a backtick.
  4359. A [code span](@) begins with a backtick string and ends with
  4360. a backtick string of equal length. The contents of the code span are
  4361. the characters between the two backtick strings, with leading and
  4362. trailing spaces and [line endings] removed, and
  4363. [whitespace] collapsed to single spaces.
  4364. This is a simple code span:
  4365. ```````````````````````````````` example
  4366. `foo`
  4367. .
  4368. <p><code>foo</code></p>
  4369. ````````````````````````````````
  4370. Here two backticks are used, because the code contains a backtick.
  4371. This example also illustrates stripping of leading and trailing spaces:
  4372. ```````````````````````````````` example
  4373. `` foo ` bar ``
  4374. .
  4375. <p><code>foo ` bar</code></p>
  4376. ````````````````````````````````
  4377. This example shows the motivation for stripping leading and trailing
  4378. spaces:
  4379. ```````````````````````````````` example
  4380. ` `` `
  4381. .
  4382. <p><code>``</code></p>
  4383. ````````````````````````````````
  4384. [Line endings] are treated like spaces:
  4385. ```````````````````````````````` example
  4386. ``
  4387. foo
  4388. ``
  4389. .
  4390. <p><code>foo</code></p>
  4391. ````````````````````````````````
  4392. Interior spaces and [line endings] are collapsed into
  4393. single spaces, just as they would be by a browser:
  4394. ```````````````````````````````` example
  4395. `foo bar
  4396. baz`
  4397. .
  4398. <p><code>foo bar baz</code></p>
  4399. ````````````````````````````````
  4400. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4401. collapsed, however:
  4402. ```````````````````````````````` example
  4403. `a  b`
  4404. .
  4405. <p><code>a  b</code></p>
  4406. ````````````````````````````````
  4407. Q: Why not just leave the spaces, since browsers will collapse them
  4408. anyway? A: Because we might be targeting a non-HTML format, and we
  4409. shouldn't rely on HTML-specific rendering assumptions.
  4410. (Existing implementations differ in their treatment of internal
  4411. spaces and [line endings]. Some, including `Markdown.pl` and
  4412. `showdown`, convert an internal [line ending] into a
  4413. `<br />` tag. But this makes things difficult for those who like to
  4414. hard-wrap their paragraphs, since a line break in the midst of a code
  4415. span will cause an unintended line break in the output. Others just
  4416. leave internal spaces as they are, which is fine if only HTML is being
  4417. targeted.)
  4418. ```````````````````````````````` example
  4419. `foo `` bar`
  4420. .
  4421. <p><code>foo `` bar</code></p>
  4422. ````````````````````````````````
  4423. Note that backslash escapes do not work in code spans. All backslashes
  4424. are treated literally:
  4425. ```````````````````````````````` example
  4426. `foo\`bar`
  4427. .
  4428. <p><code>foo\</code>bar`</p>
  4429. ````````````````````````````````
  4430. Backslash escapes are never needed, because one can always choose a
  4431. string of *n* backtick characters as delimiters, where the code does
  4432. not contain any strings of exactly *n* backtick characters.
  4433. Code span backticks have higher precedence than any other inline
  4434. constructs except HTML tags and autolinks. Thus, for example, this is
  4435. not parsed as emphasized text, since the second `*` is part of a code
  4436. span:
  4437. ```````````````````````````````` example
  4438. *foo`*`
  4439. .
  4440. <p>*foo<code>*</code></p>
  4441. ````````````````````````````````
  4442. And this is not parsed as a link:
  4443. ```````````````````````````````` example
  4444. [not a `link](/foo`)
  4445. .
  4446. <p>[not a <code>link](/foo</code>)</p>
  4447. ````````````````````````````````
  4448. Code spans, HTML tags, and autolinks have the same precedence.
  4449. Thus, this is code:
  4450. ```````````````````````````````` example
  4451. `<a href="`">`
  4452. .
  4453. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4454. ````````````````````````````````
  4455. But this is an HTML tag:
  4456. ```````````````````````````````` example
  4457. <a href="`">`
  4458. .
  4459. <p><a href="`">`</p>
  4460. ````````````````````````````````
  4461. And this is code:
  4462. ```````````````````````````````` example
  4463. `<http://foo.bar.`baz>`
  4464. .
  4465. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4466. ````````````````````````````````
  4467. But this is an autolink:
  4468. ```````````````````````````````` example
  4469. <http://foo.bar.`baz>`
  4470. .
  4471. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4472. ````````````````````````````````
  4473. When a backtick string is not closed by a matching backtick string,
  4474. we just have literal backticks:
  4475. ```````````````````````````````` example
  4476. ```foo``
  4477. .
  4478. <p>```foo``</p>
  4479. ````````````````````````````````
  4480. ```````````````````````````````` example
  4481. `foo
  4482. .
  4483. <p>`foo</p>
  4484. ````````````````````````````````
  4485. The following case also illustrates the need for opening and
  4486. closing backtick strings to be equal in length:
  4487. ```````````````````````````````` example
  4488. `foo``bar``
  4489. .
  4490. <p>`foo<code>bar</code></p>
  4491. ````````````````````````````````
  4492. ## Emphasis and strong emphasis
  4493. John Gruber's original [Markdown syntax
  4494. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4495. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4496. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4497. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4498. > tag.
  4499. This is enough for most users, but these rules leave much undecided,
  4500. especially when it comes to nested emphasis. The original
  4501. `Markdown.pl` test suite makes it clear that triple `***` and
  4502. `___` delimiters can be used for strong emphasis, and most
  4503. implementations have also allowed the following patterns:
  4504. ``` markdown
  4505. ***strong emph***
  4506. ***strong** in emph*
  4507. ***emph* in strong**
  4508. **in strong *emph***
  4509. *in emph **strong***
  4510. ```
  4511. The following patterns are less widely supported, but the intent
  4512. is clear and they are useful (especially in contexts like bibliography
  4513. entries):
  4514. ``` markdown
  4515. *emph *with emph* in it*
  4516. **strong **with strong** in it**
  4517. ```
  4518. Many implementations have also restricted intraword emphasis to
  4519. the `*` forms, to avoid unwanted emphasis in words containing
  4520. internal underscores. (It is best practice to put these in code
  4521. spans, but users often do not.)
  4522. ``` markdown
  4523. internal emphasis: foo*bar*baz
  4524. no emphasis: foo_bar_baz
  4525. ```
  4526. The rules given below capture all of these patterns, while allowing
  4527. for efficient parsing strategies that do not backtrack.
  4528. First, some definitions. A [delimiter run](@) is either
  4529. a sequence of one or more `*` characters that is not preceded or
  4530. followed by a non-backslash-escaped `*` character, or a sequence
  4531. of one or more `_` characters that is not preceded or followed by
  4532. a non-backslash-escaped `_` character.
  4533. A [left-flanking delimiter run](@) is
  4534. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4535. and (b) not followed by a [punctuation character], or
  4536. preceded by [Unicode whitespace] or a [punctuation character].
  4537. For purposes of this definition, the beginning and the end of
  4538. the line count as Unicode whitespace.
  4539. A [right-flanking delimiter run](@) is
  4540. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4541. and (b) not preceded by a [punctuation character], or
  4542. followed by [Unicode whitespace] or a [punctuation character].
  4543. For purposes of this definition, the beginning and the end of
  4544. the line count as Unicode whitespace.
  4545. Here are some examples of delimiter runs.
  4546. - left-flanking but not right-flanking:
  4547. ```
  4548. ***abc
  4549. _abc
  4550. **"abc"
  4551. _"abc"
  4552. ```
  4553. - right-flanking but not left-flanking:
  4554. ```
  4555. abc***
  4556. abc_
  4557. "abc"**
  4558. "abc"_
  4559. ```
  4560. - Both left and right-flanking:
  4561. ```
  4562. abc***def
  4563. "abc"_"def"
  4564. ```
  4565. - Neither left nor right-flanking:
  4566. ```
  4567. abc *** def
  4568. a _ b
  4569. ```
  4570. (The idea of distinguishing left-flanking and right-flanking
  4571. delimiter runs based on the character before and the character
  4572. after comes from Roopesh Chander's
  4573. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4574. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4575. run," and its rules for distinguishing left- and right-flanking runs
  4576. are a bit more complex than the ones given here.)
  4577. The following rules define emphasis and strong emphasis:
  4578. 1. A single `*` character [can open emphasis](@)
  4579. iff (if and only if) it is part of a [left-flanking delimiter run].
  4580. 2. A single `_` character [can open emphasis] iff
  4581. it is part of a [left-flanking delimiter run]
  4582. and either (a) not part of a [right-flanking delimiter run]
  4583. or (b) part of a [right-flanking delimiter run]
  4584. preceded by punctuation.
  4585. 3. A single `*` character [can close emphasis](@)
  4586. iff it is part of a [right-flanking delimiter run].
  4587. 4. A single `_` character [can close emphasis] iff
  4588. it is part of a [right-flanking delimiter run]
  4589. and either (a) not part of a [left-flanking delimiter run]
  4590. or (b) part of a [left-flanking delimiter run]
  4591. followed by punctuation.
  4592. 5. A double `**` [can open strong emphasis](@)
  4593. iff it is part of a [left-flanking delimiter run].
  4594. 6. A double `__` [can open strong emphasis] iff
  4595. it is part of a [left-flanking delimiter run]
  4596. and either (a) not part of a [right-flanking delimiter run]
  4597. or (b) part of a [right-flanking delimiter run]
  4598. preceded by punctuation.
  4599. 7. A double `**` [can close strong emphasis](@)
  4600. iff it is part of a [right-flanking delimiter run].
  4601. 8. A double `__` [can close strong emphasis] iff
  4602. it is part of a [right-flanking delimiter run]
  4603. and either (a) not part of a [left-flanking delimiter run]
  4604. or (b) part of a [left-flanking delimiter run]
  4605. followed by punctuation.
  4606. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4607. with a delimiter that [can close emphasis], and that uses the same
  4608. character (`_` or `*`) as the opening delimiter. The
  4609. opening and closing delimiters must belong to separate
  4610. [delimiter runs]. If one of the delimiters can both
  4611. open and close emphasis, then the sum of the lengths of the
  4612. delimiter runs containing the opening and closing delimiters
  4613. must not be a multiple of 3.
  4614. 10. Strong emphasis begins with a delimiter that
  4615. [can open strong emphasis] and ends with a delimiter that
  4616. [can close strong emphasis], and that uses the same character
  4617. (`_` or `*`) as the opening delimiter. The
  4618. opening and closing delimiters must belong to separate
  4619. [delimiter runs]. If one of the delimiters can both open
  4620. and close strong emphasis, then the sum of the lengths of
  4621. the delimiter runs containing the opening and closing
  4622. delimiters must not be a multiple of 3.
  4623. 11. A literal `*` character cannot occur at the beginning or end of
  4624. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4625. is backslash-escaped.
  4626. 12. A literal `_` character cannot occur at the beginning or end of
  4627. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4628. is backslash-escaped.
  4629. Where rules 1--12 above are compatible with multiple parsings,
  4630. the following principles resolve ambiguity:
  4631. 13. The number of nestings should be minimized. Thus, for example,
  4632. an interpretation `<strong>...</strong>` is always preferred to
  4633. `<em><em>...</em></em>`.
  4634. 14. An interpretation `<em><strong>...</strong></em>` is always
  4635. preferred to `<strong><em>...</em></strong>`.
  4636. 15. When two potential emphasis or strong emphasis spans overlap,
  4637. so that the second begins before the first ends and ends after
  4638. the first ends, the first takes precedence. Thus, for example,
  4639. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4640. than `*foo <em>bar* baz</em>`.
  4641. 16. When there are two potential emphasis or strong emphasis spans
  4642. with the same closing delimiter, the shorter one (the one that
  4643. opens later) takes precedence. Thus, for example,
  4644. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4645. rather than `<strong>foo **bar baz</strong>`.
  4646. 17. Inline code spans, links, images, and HTML tags group more tightly
  4647. than emphasis. So, when there is a choice between an interpretation
  4648. that contains one of these elements and one that does not, the
  4649. former always wins. Thus, for example, `*[foo*](bar)` is
  4650. parsed as `*<a href="bar">foo*</a>` rather than as
  4651. `<em>[foo</em>](bar)`.
  4652. These rules can be illustrated through a series of examples.
  4653. Rule 1:
  4654. ```````````````````````````````` example
  4655. *foo bar*
  4656. .
  4657. <p><em>foo bar</em></p>
  4658. ````````````````````````````````
  4659. This is not emphasis, because the opening `*` is followed by
  4660. whitespace, and hence not part of a [left-flanking delimiter run]:
  4661. ```````````````````````````````` example
  4662. a * foo bar*
  4663. .
  4664. <p>a * foo bar*</p>
  4665. ````````````````````````````````
  4666. This is not emphasis, because the opening `*` is preceded
  4667. by an alphanumeric and followed by punctuation, and hence
  4668. not part of a [left-flanking delimiter run]:
  4669. ```````````````````````````````` example
  4670. a*"foo"*
  4671. .
  4672. <p>a*&quot;foo&quot;*</p>
  4673. ````````````````````````````````
  4674. Unicode nonbreaking spaces count as whitespace, too:
  4675. ```````````````````````````````` example
  4676. * a *
  4677. .
  4678. <p>* a *</p>
  4679. ````````````````````````````````
  4680. Intraword emphasis with `*` is permitted:
  4681. ```````````````````````````````` example
  4682. foo*bar*
  4683. .
  4684. <p>foo<em>bar</em></p>
  4685. ````````````````````````````````
  4686. ```````````````````````````````` example
  4687. 5*6*78
  4688. .
  4689. <p>5<em>6</em>78</p>
  4690. ````````````````````````````````
  4691. Rule 2:
  4692. ```````````````````````````````` example
  4693. _foo bar_
  4694. .
  4695. <p><em>foo bar</em></p>
  4696. ````````````````````````````````
  4697. This is not emphasis, because the opening `_` is followed by
  4698. whitespace:
  4699. ```````````````````````````````` example
  4700. _ foo bar_
  4701. .
  4702. <p>_ foo bar_</p>
  4703. ````````````````````````````````
  4704. This is not emphasis, because the opening `_` is preceded
  4705. by an alphanumeric and followed by punctuation:
  4706. ```````````````````````````````` example
  4707. a_"foo"_
  4708. .
  4709. <p>a_&quot;foo&quot;_</p>
  4710. ````````````````````````````````
  4711. Emphasis with `_` is not allowed inside words:
  4712. ```````````````````````````````` example
  4713. foo_bar_
  4714. .
  4715. <p>foo_bar_</p>
  4716. ````````````````````````````````
  4717. ```````````````````````````````` example
  4718. 5_6_78
  4719. .
  4720. <p>5_6_78</p>
  4721. ````````````````````````````````
  4722. ```````````````````````````````` example
  4723. пристаням_стремятся_
  4724. .
  4725. <p>пристаням_стремятся_</p>
  4726. ````````````````````````````````
  4727. Here `_` does not generate emphasis, because the first delimiter run
  4728. is right-flanking and the second left-flanking:
  4729. ```````````````````````````````` example
  4730. aa_"bb"_cc
  4731. .
  4732. <p>aa_&quot;bb&quot;_cc</p>
  4733. ````````````````````````````````
  4734. This is emphasis, even though the opening delimiter is
  4735. both left- and right-flanking, because it is preceded by
  4736. punctuation:
  4737. ```````````````````````````````` example
  4738. foo-_(bar)_
  4739. .
  4740. <p>foo-<em>(bar)</em></p>
  4741. ````````````````````````````````
  4742. Rule 3:
  4743. This is not emphasis, because the closing delimiter does
  4744. not match the opening delimiter:
  4745. ```````````````````````````````` example
  4746. _foo*
  4747. .
  4748. <p>_foo*</p>
  4749. ````````````````````````````````
  4750. This is not emphasis, because the closing `*` is preceded by
  4751. whitespace:
  4752. ```````````````````````````````` example
  4753. *foo bar *
  4754. .
  4755. <p>*foo bar *</p>
  4756. ````````````````````````````````
  4757. A newline also counts as whitespace:
  4758. ```````````````````````````````` example
  4759. *foo bar
  4760. *
  4761. .
  4762. <p>*foo bar
  4763. *</p>
  4764. ````````````````````````````````
  4765. This is not emphasis, because the second `*` is
  4766. preceded by punctuation and followed by an alphanumeric
  4767. (hence it is not part of a [right-flanking delimiter run]:
  4768. ```````````````````````````````` example
  4769. *(*foo)
  4770. .
  4771. <p>*(*foo)</p>
  4772. ````````````````````````````````
  4773. The point of this restriction is more easily appreciated
  4774. with this example:
  4775. ```````````````````````````````` example
  4776. *(*foo*)*
  4777. .
  4778. <p><em>(<em>foo</em>)</em></p>
  4779. ````````````````````````````````
  4780. Intraword emphasis with `*` is allowed:
  4781. ```````````````````````````````` example
  4782. *foo*bar
  4783. .
  4784. <p><em>foo</em>bar</p>
  4785. ````````````````````````````````
  4786. Rule 4:
  4787. This is not emphasis, because the closing `_` is preceded by
  4788. whitespace:
  4789. ```````````````````````````````` example
  4790. _foo bar _
  4791. .
  4792. <p>_foo bar _</p>
  4793. ````````````````````````````````
  4794. This is not emphasis, because the second `_` is
  4795. preceded by punctuation and followed by an alphanumeric:
  4796. ```````````````````````````````` example
  4797. _(_foo)
  4798. .
  4799. <p>_(_foo)</p>
  4800. ````````````````````````````````
  4801. This is emphasis within emphasis:
  4802. ```````````````````````````````` example
  4803. _(_foo_)_
  4804. .
  4805. <p><em>(<em>foo</em>)</em></p>
  4806. ````````````````````````````````
  4807. Intraword emphasis is disallowed for `_`:
  4808. ```````````````````````````````` example
  4809. _foo_bar
  4810. .
  4811. <p>_foo_bar</p>
  4812. ````````````````````````````````
  4813. ```````````````````````````````` example
  4814. _пристаням_стремятся
  4815. .
  4816. <p>_пристаням_стремятся</p>
  4817. ````````````````````````````````
  4818. ```````````````````````````````` example
  4819. _foo_bar_baz_
  4820. .
  4821. <p><em>foo_bar_baz</em></p>
  4822. ````````````````````````````````
  4823. This is emphasis, even though the closing delimiter is
  4824. both left- and right-flanking, because it is followed by
  4825. punctuation:
  4826. ```````````````````````````````` example
  4827. _(bar)_.
  4828. .
  4829. <p><em>(bar)</em>.</p>
  4830. ````````````````````````````````
  4831. Rule 5:
  4832. ```````````````````````````````` example
  4833. **foo bar**
  4834. .
  4835. <p><strong>foo bar</strong></p>
  4836. ````````````````````````````````
  4837. This is not strong emphasis, because the opening delimiter is
  4838. followed by whitespace:
  4839. ```````````````````````````````` example
  4840. ** foo bar**
  4841. .
  4842. <p>** foo bar**</p>
  4843. ````````````````````````````````
  4844. This is not strong emphasis, because the opening `**` is preceded
  4845. by an alphanumeric and followed by punctuation, and hence
  4846. not part of a [left-flanking delimiter run]:
  4847. ```````````````````````````````` example
  4848. a**"foo"**
  4849. .
  4850. <p>a**&quot;foo&quot;**</p>
  4851. ````````````````````````````````
  4852. Intraword strong emphasis with `**` is permitted:
  4853. ```````````````````````````````` example
  4854. foo**bar**
  4855. .
  4856. <p>foo<strong>bar</strong></p>
  4857. ````````````````````````````````
  4858. Rule 6:
  4859. ```````````````````````````````` example
  4860. __foo bar__
  4861. .
  4862. <p><strong>foo bar</strong></p>
  4863. ````````````````````````````````
  4864. This is not strong emphasis, because the opening delimiter is
  4865. followed by whitespace:
  4866. ```````````````````````````````` example
  4867. __ foo bar__
  4868. .
  4869. <p>__ foo bar__</p>
  4870. ````````````````````````````````
  4871. A newline counts as whitespace:
  4872. ```````````````````````````````` example
  4873. __
  4874. foo bar__
  4875. .
  4876. <p>__
  4877. foo bar__</p>
  4878. ````````````````````````````````
  4879. This is not strong emphasis, because the opening `__` is preceded
  4880. by an alphanumeric and followed by punctuation:
  4881. ```````````````````````````````` example
  4882. a__"foo"__
  4883. .
  4884. <p>a__&quot;foo&quot;__</p>
  4885. ````````````````````````````````
  4886. Intraword strong emphasis is forbidden with `__`:
  4887. ```````````````````````````````` example
  4888. foo__bar__
  4889. .
  4890. <p>foo__bar__</p>
  4891. ````````````````````````````````
  4892. ```````````````````````````````` example
  4893. 5__6__78
  4894. .
  4895. <p>5__6__78</p>
  4896. ````````````````````````````````
  4897. ```````````````````````````````` example
  4898. пристаням__стремятся__
  4899. .
  4900. <p>пристаням__стремятся__</p>
  4901. ````````````````````````````````
  4902. ```````````````````````````````` example
  4903. __foo, __bar__, baz__
  4904. .
  4905. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4906. ````````````````````````````````
  4907. This is strong emphasis, even though the opening delimiter is
  4908. both left- and right-flanking, because it is preceded by
  4909. punctuation:
  4910. ```````````````````````````````` example
  4911. foo-__(bar)__
  4912. .
  4913. <p>foo-<strong>(bar)</strong></p>
  4914. ````````````````````````````````
  4915. Rule 7:
  4916. This is not strong emphasis, because the closing delimiter is preceded
  4917. by whitespace:
  4918. ```````````````````````````````` example
  4919. **foo bar **
  4920. .
  4921. <p>**foo bar **</p>
  4922. ````````````````````````````````
  4923. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4924. Rule 11.)
  4925. This is not strong emphasis, because the second `**` is
  4926. preceded by punctuation and followed by an alphanumeric:
  4927. ```````````````````````````````` example
  4928. **(**foo)
  4929. .
  4930. <p>**(**foo)</p>
  4931. ````````````````````````````````
  4932. The point of this restriction is more easily appreciated
  4933. with these examples:
  4934. ```````````````````````````````` example
  4935. *(**foo**)*
  4936. .
  4937. <p><em>(<strong>foo</strong>)</em></p>
  4938. ````````````````````````````````
  4939. ```````````````````````````````` example
  4940. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4941. *Asclepias physocarpa*)**
  4942. .
  4943. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4944. <em>Asclepias physocarpa</em>)</strong></p>
  4945. ````````````````````````````````
  4946. ```````````````````````````````` example
  4947. **foo "*bar*" foo**
  4948. .
  4949. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4950. ````````````````````````````````
  4951. Intraword emphasis:
  4952. ```````````````````````````````` example
  4953. **foo**bar
  4954. .
  4955. <p><strong>foo</strong>bar</p>
  4956. ````````````````````````````````
  4957. Rule 8:
  4958. This is not strong emphasis, because the closing delimiter is
  4959. preceded by whitespace:
  4960. ```````````````````````````````` example
  4961. __foo bar __
  4962. .
  4963. <p>__foo bar __</p>
  4964. ````````````````````````````````
  4965. This is not strong emphasis, because the second `__` is
  4966. preceded by punctuation and followed by an alphanumeric:
  4967. ```````````````````````````````` example
  4968. __(__foo)
  4969. .
  4970. <p>__(__foo)</p>
  4971. ````````````````````````````````
  4972. The point of this restriction is more easily appreciated
  4973. with this example:
  4974. ```````````````````````````````` example
  4975. _(__foo__)_
  4976. .
  4977. <p><em>(<strong>foo</strong>)</em></p>
  4978. ````````````````````````````````
  4979. Intraword strong emphasis is forbidden with `__`:
  4980. ```````````````````````````````` example
  4981. __foo__bar
  4982. .
  4983. <p>__foo__bar</p>
  4984. ````````````````````````````````
  4985. ```````````````````````````````` example
  4986. __пристаням__стремятся
  4987. .
  4988. <p>__пристаням__стремятся</p>
  4989. ````````````````````````````````
  4990. ```````````````````````````````` example
  4991. __foo__bar__baz__
  4992. .
  4993. <p><strong>foo__bar__baz</strong></p>
  4994. ````````````````````````````````
  4995. This is strong emphasis, even though the closing delimiter is
  4996. both left- and right-flanking, because it is followed by
  4997. punctuation:
  4998. ```````````````````````````````` example
  4999. __(bar)__.
  5000. .
  5001. <p><strong>(bar)</strong>.</p>
  5002. ````````````````````````````````
  5003. Rule 9:
  5004. Any nonempty sequence of inline elements can be the contents of an
  5005. emphasized span.
  5006. ```````````````````````````````` example
  5007. *foo [bar](/url)*
  5008. .
  5009. <p><em>foo <a href="/url">bar</a></em></p>
  5010. ````````````````````````````````
  5011. ```````````````````````````````` example
  5012. *foo
  5013. bar*
  5014. .
  5015. <p><em>foo
  5016. bar</em></p>
  5017. ````````````````````````````````
  5018. In particular, emphasis and strong emphasis can be nested
  5019. inside emphasis:
  5020. ```````````````````````````````` example
  5021. _foo __bar__ baz_
  5022. .
  5023. <p><em>foo <strong>bar</strong> baz</em></p>
  5024. ````````````````````````````````
  5025. ```````````````````````````````` example
  5026. _foo _bar_ baz_
  5027. .
  5028. <p><em>foo <em>bar</em> baz</em></p>
  5029. ````````````````````````````````
  5030. ```````````````````````````````` example
  5031. __foo_ bar_
  5032. .
  5033. <p><em><em>foo</em> bar</em></p>
  5034. ````````````````````````````````
  5035. ```````````````````````````````` example
  5036. *foo *bar**
  5037. .
  5038. <p><em>foo <em>bar</em></em></p>
  5039. ````````````````````````````````
  5040. ```````````````````````````````` example
  5041. *foo **bar** baz*
  5042. .
  5043. <p><em>foo <strong>bar</strong> baz</em></p>
  5044. ````````````````````````````````
  5045. ```````````````````````````````` example
  5046. *foo**bar**baz*
  5047. .
  5048. <p><em>foo<strong>bar</strong>baz</em></p>
  5049. ````````````````````````````````
  5050. Note that in the preceding case, the interpretation
  5051. ``` markdown
  5052. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5053. ```
  5054. is precluded by the condition that a delimiter that
  5055. can both open and close (like the `*` after `foo`)
  5056. cannot form emphasis if the sum of the lengths of
  5057. the delimiter runs containing the opening and
  5058. closing delimiters is a multiple of 3.
  5059. The same condition ensures that the following
  5060. cases are all strong emphasis nested inside
  5061. emphasis, even when the interior spaces are
  5062. omitted:
  5063. ```````````````````````````````` example
  5064. ***foo** bar*
  5065. .
  5066. <p><em><strong>foo</strong> bar</em></p>
  5067. ````````````````````````````````
  5068. ```````````````````````````````` example
  5069. *foo **bar***
  5070. .
  5071. <p><em>foo <strong>bar</strong></em></p>
  5072. ````````````````````````````````
  5073. ```````````````````````````````` example
  5074. *foo**bar***
  5075. .
  5076. <p><em>foo<strong>bar</strong></em></p>
  5077. ````````````````````````````````
  5078. Indefinite levels of nesting are possible:
  5079. ```````````````````````````````` example
  5080. *foo **bar *baz* bim** bop*
  5081. .
  5082. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5083. ````````````````````````````````
  5084. ```````````````````````````````` example
  5085. *foo [*bar*](/url)*
  5086. .
  5087. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5088. ````````````````````````````````
  5089. There can be no empty emphasis or strong emphasis:
  5090. ```````````````````````````````` example
  5091. ** is not an empty emphasis
  5092. .
  5093. <p>** is not an empty emphasis</p>
  5094. ````````````````````````````````
  5095. ```````````````````````````````` example
  5096. **** is not an empty strong emphasis
  5097. .
  5098. <p>**** is not an empty strong emphasis</p>
  5099. ````````````````````````````````
  5100. Rule 10:
  5101. Any nonempty sequence of inline elements can be the contents of an
  5102. strongly emphasized span.
  5103. ```````````````````````````````` example
  5104. **foo [bar](/url)**
  5105. .
  5106. <p><strong>foo <a href="/url">bar</a></strong></p>
  5107. ````````````````````````````````
  5108. ```````````````````````````````` example
  5109. **foo
  5110. bar**
  5111. .
  5112. <p><strong>foo
  5113. bar</strong></p>
  5114. ````````````````````````````````
  5115. In particular, emphasis and strong emphasis can be nested
  5116. inside strong emphasis:
  5117. ```````````````````````````````` example
  5118. __foo _bar_ baz__
  5119. .
  5120. <p><strong>foo <em>bar</em> baz</strong></p>
  5121. ````````````````````````````````
  5122. ```````````````````````````````` example
  5123. __foo __bar__ baz__
  5124. .
  5125. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5126. ````````````````````````````````
  5127. ```````````````````````````````` example
  5128. ____foo__ bar__
  5129. .
  5130. <p><strong><strong>foo</strong> bar</strong></p>
  5131. ````````````````````````````````
  5132. ```````````````````````````````` example
  5133. **foo **bar****
  5134. .
  5135. <p><strong>foo <strong>bar</strong></strong></p>
  5136. ````````````````````````````````
  5137. ```````````````````````````````` example
  5138. **foo *bar* baz**
  5139. .
  5140. <p><strong>foo <em>bar</em> baz</strong></p>
  5141. ````````````````````````````````
  5142. ```````````````````````````````` example
  5143. **foo*bar*baz**
  5144. .
  5145. <p><strong>foo<em>bar</em>baz</strong></p>
  5146. ````````````````````````````````
  5147. ```````````````````````````````` example
  5148. ***foo* bar**
  5149. .
  5150. <p><strong><em>foo</em> bar</strong></p>
  5151. ````````````````````````````````
  5152. ```````````````````````````````` example
  5153. **foo *bar***
  5154. .
  5155. <p><strong>foo <em>bar</em></strong></p>
  5156. ````````````````````````````````
  5157. Indefinite levels of nesting are possible:
  5158. ```````````````````````````````` example
  5159. **foo *bar **baz**
  5160. bim* bop**
  5161. .
  5162. <p><strong>foo <em>bar <strong>baz</strong>
  5163. bim</em> bop</strong></p>
  5164. ````````````````````````````````
  5165. ```````````````````````````````` example
  5166. **foo [*bar*](/url)**
  5167. .
  5168. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5169. ````````````````````````````````
  5170. There can be no empty emphasis or strong emphasis:
  5171. ```````````````````````````````` example
  5172. __ is not an empty emphasis
  5173. .
  5174. <p>__ is not an empty emphasis</p>
  5175. ````````````````````````````````
  5176. ```````````````````````````````` example
  5177. ____ is not an empty strong emphasis
  5178. .
  5179. <p>____ is not an empty strong emphasis</p>
  5180. ````````````````````````````````
  5181. Rule 11:
  5182. ```````````````````````````````` example
  5183. foo ***
  5184. .
  5185. <p>foo ***</p>
  5186. ````````````````````````````````
  5187. ```````````````````````````````` example
  5188. foo *\**
  5189. .
  5190. <p>foo <em>*</em></p>
  5191. ````````````````````````````````
  5192. ```````````````````````````````` example
  5193. foo *_*
  5194. .
  5195. <p>foo <em>_</em></p>
  5196. ````````````````````````````````
  5197. ```````````````````````````````` example
  5198. foo *****
  5199. .
  5200. <p>foo *****</p>
  5201. ````````````````````````````````
  5202. ```````````````````````````````` example
  5203. foo **\***
  5204. .
  5205. <p>foo <strong>*</strong></p>
  5206. ````````````````````````````````
  5207. ```````````````````````````````` example
  5208. foo **_**
  5209. .
  5210. <p>foo <strong>_</strong></p>
  5211. ````````````````````````````````
  5212. Note that when delimiters do not match evenly, Rule 11 determines
  5213. that the excess literal `*` characters will appear outside of the
  5214. emphasis, rather than inside it:
  5215. ```````````````````````````````` example
  5216. **foo*
  5217. .
  5218. <p>*<em>foo</em></p>
  5219. ````````````````````````````````
  5220. ```````````````````````````````` example
  5221. *foo**
  5222. .
  5223. <p><em>foo</em>*</p>
  5224. ````````````````````````````````
  5225. ```````````````````````````````` example
  5226. ***foo**
  5227. .
  5228. <p>*<strong>foo</strong></p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. ****foo*
  5232. .
  5233. <p>***<em>foo</em></p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. **foo***
  5237. .
  5238. <p><strong>foo</strong>*</p>
  5239. ````````````````````````````````
  5240. ```````````````````````````````` example
  5241. *foo****
  5242. .
  5243. <p><em>foo</em>***</p>
  5244. ````````````````````````````````
  5245. Rule 12:
  5246. ```````````````````````````````` example
  5247. foo ___
  5248. .
  5249. <p>foo ___</p>
  5250. ````````````````````````````````
  5251. ```````````````````````````````` example
  5252. foo _\__
  5253. .
  5254. <p>foo <em>_</em></p>
  5255. ````````````````````````````````
  5256. ```````````````````````````````` example
  5257. foo _*_
  5258. .
  5259. <p>foo <em>*</em></p>
  5260. ````````````````````````````````
  5261. ```````````````````````````````` example
  5262. foo _____
  5263. .
  5264. <p>foo _____</p>
  5265. ````````````````````````````````
  5266. ```````````````````````````````` example
  5267. foo __\___
  5268. .
  5269. <p>foo <strong>_</strong></p>
  5270. ````````````````````````````````
  5271. ```````````````````````````````` example
  5272. foo __*__
  5273. .
  5274. <p>foo <strong>*</strong></p>
  5275. ````````````````````````````````
  5276. ```````````````````````````````` example
  5277. __foo_
  5278. .
  5279. <p>_<em>foo</em></p>
  5280. ````````````````````````````````
  5281. Note that when delimiters do not match evenly, Rule 12 determines
  5282. that the excess literal `_` characters will appear outside of the
  5283. emphasis, rather than inside it:
  5284. ```````````````````````````````` example
  5285. _foo__
  5286. .
  5287. <p><em>foo</em>_</p>
  5288. ````````````````````````````````
  5289. ```````````````````````````````` example
  5290. ___foo__
  5291. .
  5292. <p>_<strong>foo</strong></p>
  5293. ````````````````````````````````
  5294. ```````````````````````````````` example
  5295. ____foo_
  5296. .
  5297. <p>___<em>foo</em></p>
  5298. ````````````````````````````````
  5299. ```````````````````````````````` example
  5300. __foo___
  5301. .
  5302. <p><strong>foo</strong>_</p>
  5303. ````````````````````````````````
  5304. ```````````````````````````````` example
  5305. _foo____
  5306. .
  5307. <p><em>foo</em>___</p>
  5308. ````````````````````````````````
  5309. Rule 13 implies that if you want emphasis nested directly inside
  5310. emphasis, you must use different delimiters:
  5311. ```````````````````````````````` example
  5312. **foo**
  5313. .
  5314. <p><strong>foo</strong></p>
  5315. ````````````````````````````````
  5316. ```````````````````````````````` example
  5317. *_foo_*
  5318. .
  5319. <p><em><em>foo</em></em></p>
  5320. ````````````````````````````````
  5321. ```````````````````````````````` example
  5322. __foo__
  5323. .
  5324. <p><strong>foo</strong></p>
  5325. ````````````````````````````````
  5326. ```````````````````````````````` example
  5327. _*foo*_
  5328. .
  5329. <p><em><em>foo</em></em></p>
  5330. ````````````````````````````````
  5331. However, strong emphasis within strong emphasis is possible without
  5332. switching delimiters:
  5333. ```````````````````````````````` example
  5334. ****foo****
  5335. .
  5336. <p><strong><strong>foo</strong></strong></p>
  5337. ````````````````````````````````
  5338. ```````````````````````````````` example
  5339. ____foo____
  5340. .
  5341. <p><strong><strong>foo</strong></strong></p>
  5342. ````````````````````````````````
  5343. Rule 13 can be applied to arbitrarily long sequences of
  5344. delimiters:
  5345. ```````````````````````````````` example
  5346. ******foo******
  5347. .
  5348. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5349. ````````````````````````````````
  5350. Rule 14:
  5351. ```````````````````````````````` example
  5352. ***foo***
  5353. .
  5354. <p><em><strong>foo</strong></em></p>
  5355. ````````````````````````````````
  5356. ```````````````````````````````` example
  5357. _____foo_____
  5358. .
  5359. <p><em><strong><strong>foo</strong></strong></em></p>
  5360. ````````````````````````````````
  5361. Rule 15:
  5362. ```````````````````````````````` example
  5363. *foo _bar* baz_
  5364. .
  5365. <p><em>foo _bar</em> baz_</p>
  5366. ````````````````````````````````
  5367. ```````````````````````````````` example
  5368. *foo __bar *baz bim__ bam*
  5369. .
  5370. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5371. ````````````````````````````````
  5372. Rule 16:
  5373. ```````````````````````````````` example
  5374. **foo **bar baz**
  5375. .
  5376. <p>**foo <strong>bar baz</strong></p>
  5377. ````````````````````````````````
  5378. ```````````````````````````````` example
  5379. *foo *bar baz*
  5380. .
  5381. <p>*foo <em>bar baz</em></p>
  5382. ````````````````````````````````
  5383. Rule 17:
  5384. ```````````````````````````````` example
  5385. *[bar*](/url)
  5386. .
  5387. <p>*<a href="/url">bar*</a></p>
  5388. ````````````````````````````````
  5389. ```````````````````````````````` example
  5390. _foo [bar_](/url)
  5391. .
  5392. <p>_foo <a href="/url">bar_</a></p>
  5393. ````````````````````````````````
  5394. ```````````````````````````````` example
  5395. *<img src="foo" title="*"/>
  5396. .
  5397. <p>*<img src="foo" title="*"/></p>
  5398. ````````````````````````````````
  5399. ```````````````````````````````` example
  5400. **<a href="**">
  5401. .
  5402. <p>**<a href="**"></p>
  5403. ````````````````````````````````
  5404. ```````````````````````````````` example
  5405. __<a href="__">
  5406. .
  5407. <p>__<a href="__"></p>
  5408. ````````````````````````````````
  5409. ```````````````````````````````` example
  5410. *a `*`*
  5411. .
  5412. <p><em>a <code>*</code></em></p>
  5413. ````````````````````````````````
  5414. ```````````````````````````````` example
  5415. _a `_`_
  5416. .
  5417. <p><em>a <code>_</code></em></p>
  5418. ````````````````````````````````
  5419. ```````````````````````````````` example
  5420. **a<http://foo.bar/?q=**>
  5421. .
  5422. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5423. ````````````````````````````````
  5424. ```````````````````````````````` example
  5425. __a<http://foo.bar/?q=__>
  5426. .
  5427. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5428. ````````````````````````````````
  5429. ## Links
  5430. A link contains [link text] (the visible text), a [link destination]
  5431. (the URI that is the link destination), and optionally a [link title].
  5432. There are two basic kinds of links in Markdown. In [inline links] the
  5433. destination and title are given immediately after the link text. In
  5434. [reference links] the destination and title are defined elsewhere in
  5435. the document.
  5436. A [link text](@) consists of a sequence of zero or more
  5437. inline elements enclosed by square brackets (`[` and `]`). The
  5438. following rules apply:
  5439. - Links may not contain other links, at any level of nesting. If
  5440. multiple otherwise valid link definitions appear nested inside each
  5441. other, the inner-most definition is used.
  5442. - Brackets are allowed in the [link text] only if (a) they
  5443. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5444. with an open bracket `[`, a sequence of zero or more inlines, and
  5445. a close bracket `]`.
  5446. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5447. than the brackets in link text. Thus, for example,
  5448. `` [foo`]` `` could not be a link text, since the second `]`
  5449. is part of a code span.
  5450. - The brackets in link text bind more tightly than markers for
  5451. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5452. A [link destination](@) consists of either
  5453. - a sequence of zero or more characters between an opening `<` and a
  5454. closing `>` that contains no spaces, line breaks, or unescaped
  5455. `<` or `>` characters, or
  5456. - a nonempty sequence of characters that does not include
  5457. ASCII space or control characters, and includes parentheses
  5458. only if (a) they are backslash-escaped or (b) they are part of
  5459. a balanced pair of unescaped parentheses. (Implementations
  5460. may impose limits on parentheses nesting to avoid performance
  5461. issues, but at least three levels of nesting should be supported.)
  5462. A [link title](@) consists of either
  5463. - a sequence of zero or more characters between straight double-quote
  5464. characters (`"`), including a `"` character only if it is
  5465. backslash-escaped, or
  5466. - a sequence of zero or more characters between straight single-quote
  5467. characters (`'`), including a `'` character only if it is
  5468. backslash-escaped, or
  5469. - a sequence of zero or more characters between matching parentheses
  5470. (`(...)`), including a `)` character only if it is backslash-escaped.
  5471. Although [link titles] may span multiple lines, they may not contain
  5472. a [blank line].
  5473. An [inline link](@) consists of a [link text] followed immediately
  5474. by a left parenthesis `(`, optional [whitespace], an optional
  5475. [link destination], an optional [link title] separated from the link
  5476. destination by [whitespace], optional [whitespace], and a right
  5477. parenthesis `)`. The link's text consists of the inlines contained
  5478. in the [link text] (excluding the enclosing square brackets).
  5479. The link's URI consists of the link destination, excluding enclosing
  5480. `<...>` if present, with backslash-escapes in effect as described
  5481. above. The link's title consists of the link title, excluding its
  5482. enclosing delimiters, with backslash-escapes in effect as described
  5483. above.
  5484. Here is a simple inline link:
  5485. ```````````````````````````````` example
  5486. [link](/uri "title")
  5487. .
  5488. <p><a href="/uri" title="title">link</a></p>
  5489. ````````````````````````````````
  5490. The title may be omitted:
  5491. ```````````````````````````````` example
  5492. [link](/uri)
  5493. .
  5494. <p><a href="/uri">link</a></p>
  5495. ````````````````````````````````
  5496. Both the title and the destination may be omitted:
  5497. ```````````````````````````````` example
  5498. [link]()
  5499. .
  5500. <p><a href="">link</a></p>
  5501. ````````````````````````````````
  5502. ```````````````````````````````` example
  5503. [link](<>)
  5504. .
  5505. <p><a href="">link</a></p>
  5506. ````````````````````````````````
  5507. The destination cannot contain spaces or line breaks,
  5508. even if enclosed in pointy brackets:
  5509. ```````````````````````````````` example
  5510. [link](/my uri)
  5511. .
  5512. <p>[link](/my uri)</p>
  5513. ````````````````````````````````
  5514. ```````````````````````````````` example
  5515. [link](</my uri>)
  5516. .
  5517. <p>[link](&lt;/my uri&gt;)</p>
  5518. ````````````````````````````````
  5519. ```````````````````````````````` example
  5520. [link](foo
  5521. bar)
  5522. .
  5523. <p>[link](foo
  5524. bar)</p>
  5525. ````````````````````````````````
  5526. ```````````````````````````````` example
  5527. [link](<foo
  5528. bar>)
  5529. .
  5530. <p>[link](<foo
  5531. bar>)</p>
  5532. ````````````````````````````````
  5533. Parentheses inside the link destination may be escaped:
  5534. ```````````````````````````````` example
  5535. [link](\(foo\))
  5536. .
  5537. <p><a href="(foo)">link</a></p>
  5538. ````````````````````````````````
  5539. Any number of parentheses are allowed without escaping, as long as they are
  5540. balanced:
  5541. ```````````````````````````````` example
  5542. [link](foo(and(bar)))
  5543. .
  5544. <p><a href="foo(and(bar))">link</a></p>
  5545. ````````````````````````````````
  5546. However, if you have unbalanced parentheses, you need to escape or use the
  5547. `<...>` form:
  5548. ```````````````````````````````` example
  5549. [link](foo\(and\(bar\))
  5550. .
  5551. <p><a href="foo(and(bar)">link</a></p>
  5552. ````````````````````````````````
  5553. ```````````````````````````````` example
  5554. [link](<foo(and(bar)>)
  5555. .
  5556. <p><a href="foo(and(bar)">link</a></p>
  5557. ````````````````````````````````
  5558. Parentheses and other symbols can also be escaped, as usual
  5559. in Markdown:
  5560. ```````````````````````````````` example
  5561. [link](foo\)\:)
  5562. .
  5563. <p><a href="foo):">link</a></p>
  5564. ````````````````````````````````
  5565. A link can contain fragment identifiers and queries:
  5566. ```````````````````````````````` example
  5567. [link](#fragment)
  5568. [link](http://example.com#fragment)
  5569. [link](http://example.com?foo=3#frag)
  5570. .
  5571. <p><a href="#fragment">link</a></p>
  5572. <p><a href="http://example.com#fragment">link</a></p>
  5573. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5574. ````````````````````````````````
  5575. Note that a backslash before a non-escapable character is
  5576. just a backslash:
  5577. ```````````````````````````````` example
  5578. [link](foo\bar)
  5579. .
  5580. <p><a href="foo%5Cbar">link</a></p>
  5581. ````````````````````````````````
  5582. URL-escaping should be left alone inside the destination, as all
  5583. URL-escaped characters are also valid URL characters. Entity and
  5584. numerical character references in the destination will be parsed
  5585. into the corresponding Unicode code points, as usual. These may
  5586. be optionally URL-escaped when written as HTML, but this spec
  5587. does not enforce any particular policy for rendering URLs in
  5588. HTML or other formats. Renderers may make different decisions
  5589. about how to escape or normalize URLs in the output.
  5590. ```````````````````````````````` example
  5591. [link](foo%20b&auml;)
  5592. .
  5593. <p><a href="foo%20b%C3%A4">link</a></p>
  5594. ````````````````````````````````
  5595. Note that, because titles can often be parsed as destinations,
  5596. if you try to omit the destination and keep the title, you'll
  5597. get unexpected results:
  5598. ```````````````````````````````` example
  5599. [link]("title")
  5600. .
  5601. <p><a href="%22title%22">link</a></p>
  5602. ````````````````````````````````
  5603. Titles may be in single quotes, double quotes, or parentheses:
  5604. ```````````````````````````````` example
  5605. [link](/url "title")
  5606. [link](/url 'title')
  5607. [link](/url (title))
  5608. .
  5609. <p><a href="/url" title="title">link</a>
  5610. <a href="/url" title="title">link</a>
  5611. <a href="/url" title="title">link</a></p>
  5612. ````````````````````````````````
  5613. Backslash escapes and entity and numeric character references
  5614. may be used in titles:
  5615. ```````````````````````````````` example
  5616. [link](/url "title \"&quot;")
  5617. .
  5618. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5619. ````````````````````````````````
  5620. Titles must be separated from the link using a [whitespace].
  5621. Other [Unicode whitespace] like non-breaking space doesn't work.
  5622. ```````````````````````````````` example
  5623. [link](/url "title")
  5624. .
  5625. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5626. ````````````````````````````````
  5627. Nested balanced quotes are not allowed without escaping:
  5628. ```````````````````````````````` example
  5629. [link](/url "title "and" title")
  5630. .
  5631. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5632. ````````````````````````````````
  5633. But it is easy to work around this by using a different quote type:
  5634. ```````````````````````````````` example
  5635. [link](/url 'title "and" title')
  5636. .
  5637. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5638. ````````````````````````````````
  5639. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5640. title, and its test suite included a test demonstrating this.
  5641. But it is hard to see a good rationale for the extra complexity this
  5642. brings, since there are already many ways---backslash escaping,
  5643. entity and numeric character references, or using a different
  5644. quote type for the enclosing title---to write titles containing
  5645. double quotes. `Markdown.pl`'s handling of titles has a number
  5646. of other strange features. For example, it allows single-quoted
  5647. titles in inline links, but not reference links. And, in
  5648. reference links but not inline links, it allows a title to begin
  5649. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5650. titles with no closing quotation mark, though 1.0.2b8 does not.
  5651. It seems preferable to adopt a simple, rational rule that works
  5652. the same way in inline links and link reference definitions.)
  5653. [Whitespace] is allowed around the destination and title:
  5654. ```````````````````````````````` example
  5655. [link]( /uri
  5656. "title" )
  5657. .
  5658. <p><a href="/uri" title="title">link</a></p>
  5659. ````````````````````````````````
  5660. But it is not allowed between the link text and the
  5661. following parenthesis:
  5662. ```````````````````````````````` example
  5663. [link] (/uri)
  5664. .
  5665. <p>[link] (/uri)</p>
  5666. ````````````````````````````````
  5667. The link text may contain balanced brackets, but not unbalanced ones,
  5668. unless they are escaped:
  5669. ```````````````````````````````` example
  5670. [link [foo [bar]]](/uri)
  5671. .
  5672. <p><a href="/uri">link [foo [bar]]</a></p>
  5673. ````````````````````````````````
  5674. ```````````````````````````````` example
  5675. [link] bar](/uri)
  5676. .
  5677. <p>[link] bar](/uri)</p>
  5678. ````````````````````````````````
  5679. ```````````````````````````````` example
  5680. [link [bar](/uri)
  5681. .
  5682. <p>[link <a href="/uri">bar</a></p>
  5683. ````````````````````````````````
  5684. ```````````````````````````````` example
  5685. [link \[bar](/uri)
  5686. .
  5687. <p><a href="/uri">link [bar</a></p>
  5688. ````````````````````````````````
  5689. The link text may contain inline content:
  5690. ```````````````````````````````` example
  5691. [link *foo **bar** `#`*](/uri)
  5692. .
  5693. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5694. ````````````````````````````````
  5695. ```````````````````````````````` example
  5696. [![moon](moon.jpg)](/uri)
  5697. .
  5698. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5699. ````````````````````````````````
  5700. However, links may not contain other links, at any level of nesting.
  5701. ```````````````````````````````` example
  5702. [foo [bar](/uri)](/uri)
  5703. .
  5704. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5705. ````````````````````````````````
  5706. ```````````````````````````````` example
  5707. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5708. .
  5709. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5710. ````````````````````````````````
  5711. ```````````````````````````````` example
  5712. ![[[foo](uri1)](uri2)](uri3)
  5713. .
  5714. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5715. ````````````````````````````````
  5716. These cases illustrate the precedence of link text grouping over
  5717. emphasis grouping:
  5718. ```````````````````````````````` example
  5719. *[foo*](/uri)
  5720. .
  5721. <p>*<a href="/uri">foo*</a></p>
  5722. ````````````````````````````````
  5723. ```````````````````````````````` example
  5724. [foo *bar](baz*)
  5725. .
  5726. <p><a href="baz*">foo *bar</a></p>
  5727. ````````````````````````````````
  5728. Note that brackets that *aren't* part of links do not take
  5729. precedence:
  5730. ```````````````````````````````` example
  5731. *foo [bar* baz]
  5732. .
  5733. <p><em>foo [bar</em> baz]</p>
  5734. ````````````````````````````````
  5735. These cases illustrate the precedence of HTML tags, code spans,
  5736. and autolinks over link grouping:
  5737. ```````````````````````````````` example
  5738. [foo <bar attr="](baz)">
  5739. .
  5740. <p>[foo <bar attr="](baz)"></p>
  5741. ````````````````````````````````
  5742. ```````````````````````````````` example
  5743. [foo`](/uri)`
  5744. .
  5745. <p>[foo<code>](/uri)</code></p>
  5746. ````````````````````````````````
  5747. ```````````````````````````````` example
  5748. [foo<http://example.com/?search=](uri)>
  5749. .
  5750. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5751. ````````````````````````````````
  5752. There are three kinds of [reference link](@)s:
  5753. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5754. and [shortcut](#shortcut-reference-link).
  5755. A [full reference link](@)
  5756. consists of a [link text] immediately followed by a [link label]
  5757. that [matches] a [link reference definition] elsewhere in the document.
  5758. A [link label](@) begins with a left bracket (`[`) and ends
  5759. with the first right bracket (`]`) that is not backslash-escaped.
  5760. Between these brackets there must be at least one [non-whitespace character].
  5761. Unescaped square bracket characters are not allowed in
  5762. [link labels]. A link label can have at most 999
  5763. characters inside the square brackets.
  5764. One label [matches](@)
  5765. another just in case their normalized forms are equal. To normalize a
  5766. label, perform the *Unicode case fold* and collapse consecutive internal
  5767. [whitespace] to a single space. If there are multiple
  5768. matching reference link definitions, the one that comes first in the
  5769. document is used. (It is desirable in such cases to emit a warning.)
  5770. The contents of the first link label are parsed as inlines, which are
  5771. used as the link's text. The link's URI and title are provided by the
  5772. matching [link reference definition].
  5773. Here is a simple example:
  5774. ```````````````````````````````` example
  5775. [foo][bar]
  5776. [bar]: /url "title"
  5777. .
  5778. <p><a href="/url" title="title">foo</a></p>
  5779. ````````````````````````````````
  5780. The rules for the [link text] are the same as with
  5781. [inline links]. Thus:
  5782. The link text may contain balanced brackets, but not unbalanced ones,
  5783. unless they are escaped:
  5784. ```````````````````````````````` example
  5785. [link [foo [bar]]][ref]
  5786. [ref]: /uri
  5787. .
  5788. <p><a href="/uri">link [foo [bar]]</a></p>
  5789. ````````````````````````````````
  5790. ```````````````````````````````` example
  5791. [link \[bar][ref]
  5792. [ref]: /uri
  5793. .
  5794. <p><a href="/uri">link [bar</a></p>
  5795. ````````````````````````````````
  5796. The link text may contain inline content:
  5797. ```````````````````````````````` example
  5798. [link *foo **bar** `#`*][ref]
  5799. [ref]: /uri
  5800. .
  5801. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5802. ````````````````````````````````
  5803. ```````````````````````````````` example
  5804. [![moon](moon.jpg)][ref]
  5805. [ref]: /uri
  5806. .
  5807. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5808. ````````````````````````````````
  5809. However, links may not contain other links, at any level of nesting.
  5810. ```````````````````````````````` example
  5811. [foo [bar](/uri)][ref]
  5812. [ref]: /uri
  5813. .
  5814. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5815. ````````````````````````````````
  5816. ```````````````````````````````` example
  5817. [foo *bar [baz][ref]*][ref]
  5818. [ref]: /uri
  5819. .
  5820. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5821. ````````````````````````````````
  5822. (In the examples above, we have two [shortcut reference links]
  5823. instead of one [full reference link].)
  5824. The following cases illustrate the precedence of link text grouping over
  5825. emphasis grouping:
  5826. ```````````````````````````````` example
  5827. *[foo*][ref]
  5828. [ref]: /uri
  5829. .
  5830. <p>*<a href="/uri">foo*</a></p>
  5831. ````````````````````````````````
  5832. ```````````````````````````````` example
  5833. [foo *bar][ref]
  5834. [ref]: /uri
  5835. .
  5836. <p><a href="/uri">foo *bar</a></p>
  5837. ````````````````````````````````
  5838. These cases illustrate the precedence of HTML tags, code spans,
  5839. and autolinks over link grouping:
  5840. ```````````````````````````````` example
  5841. [foo <bar attr="][ref]">
  5842. [ref]: /uri
  5843. .
  5844. <p>[foo <bar attr="][ref]"></p>
  5845. ````````````````````````````````
  5846. ```````````````````````````````` example
  5847. [foo`][ref]`
  5848. [ref]: /uri
  5849. .
  5850. <p>[foo<code>][ref]</code></p>
  5851. ````````````````````````````````
  5852. ```````````````````````````````` example
  5853. [foo<http://example.com/?search=][ref]>
  5854. [ref]: /uri
  5855. .
  5856. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5857. ````````````````````````````````
  5858. Matching is case-insensitive:
  5859. ```````````````````````````````` example
  5860. [foo][BaR]
  5861. [bar]: /url "title"
  5862. .
  5863. <p><a href="/url" title="title">foo</a></p>
  5864. ````````````````````````````````
  5865. Unicode case fold is used:
  5866. ```````````````````````````````` example
  5867. [Толпой][Толпой] is a Russian word.
  5868. [ТОЛПОЙ]: /url
  5869. .
  5870. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5871. ````````````````````````````````
  5872. Consecutive internal [whitespace] is treated as one space for
  5873. purposes of determining matching:
  5874. ```````````````````````````````` example
  5875. [Foo
  5876. bar]: /url
  5877. [Baz][Foo bar]
  5878. .
  5879. <p><a href="/url">Baz</a></p>
  5880. ````````````````````````````````
  5881. No [whitespace] is allowed between the [link text] and the
  5882. [link label]:
  5883. ```````````````````````````````` example
  5884. [foo] [bar]
  5885. [bar]: /url "title"
  5886. .
  5887. <p>[foo] <a href="/url" title="title">bar</a></p>
  5888. ````````````````````````````````
  5889. ```````````````````````````````` example
  5890. [foo]
  5891. [bar]
  5892. [bar]: /url "title"
  5893. .
  5894. <p>[foo]
  5895. <a href="/url" title="title">bar</a></p>
  5896. ````````````````````````````````
  5897. This is a departure from John Gruber's original Markdown syntax
  5898. description, which explicitly allows whitespace between the link
  5899. text and the link label. It brings reference links in line with
  5900. [inline links], which (according to both original Markdown and
  5901. this spec) cannot have whitespace after the link text. More
  5902. importantly, it prevents inadvertent capture of consecutive
  5903. [shortcut reference links]. If whitespace is allowed between the
  5904. link text and the link label, then in the following we will have
  5905. a single reference link, not two shortcut reference links, as
  5906. intended:
  5907. ``` markdown
  5908. [foo]
  5909. [bar]
  5910. [foo]: /url1
  5911. [bar]: /url2
  5912. ```
  5913. (Note that [shortcut reference links] were introduced by Gruber
  5914. himself in a beta version of `Markdown.pl`, but never included
  5915. in the official syntax description. Without shortcut reference
  5916. links, it is harmless to allow space between the link text and
  5917. link label; but once shortcut references are introduced, it is
  5918. too dangerous to allow this, as it frequently leads to
  5919. unintended results.)
  5920. When there are multiple matching [link reference definitions],
  5921. the first is used:
  5922. ```````````````````````````````` example
  5923. [foo]: /url1
  5924. [foo]: /url2
  5925. [bar][foo]
  5926. .
  5927. <p><a href="/url1">bar</a></p>
  5928. ````````````````````````````````
  5929. Note that matching is performed on normalized strings, not parsed
  5930. inline content. So the following does not match, even though the
  5931. labels define equivalent inline content:
  5932. ```````````````````````````````` example
  5933. [bar][foo\!]
  5934. [foo!]: /url
  5935. .
  5936. <p>[bar][foo!]</p>
  5937. ````````````````````````````````
  5938. [Link labels] cannot contain brackets, unless they are
  5939. backslash-escaped:
  5940. ```````````````````````````````` example
  5941. [foo][ref[]
  5942. [ref[]: /uri
  5943. .
  5944. <p>[foo][ref[]</p>
  5945. <p>[ref[]: /uri</p>
  5946. ````````````````````````````````
  5947. ```````````````````````````````` example
  5948. [foo][ref[bar]]
  5949. [ref[bar]]: /uri
  5950. .
  5951. <p>[foo][ref[bar]]</p>
  5952. <p>[ref[bar]]: /uri</p>
  5953. ````````````````````````````````
  5954. ```````````````````````````````` example
  5955. [[[foo]]]
  5956. [[[foo]]]: /url
  5957. .
  5958. <p>[[[foo]]]</p>
  5959. <p>[[[foo]]]: /url</p>
  5960. ````````````````````````````````
  5961. ```````````````````````````````` example
  5962. [foo][ref\[]
  5963. [ref\[]: /uri
  5964. .
  5965. <p><a href="/uri">foo</a></p>
  5966. ````````````````````````````````
  5967. Note that in this example `]` is not backslash-escaped:
  5968. ```````````````````````````````` example
  5969. [bar\\]: /uri
  5970. [bar\\]
  5971. .
  5972. <p><a href="/uri">bar\</a></p>
  5973. ````````````````````````````````
  5974. A [link label] must contain at least one [non-whitespace character]:
  5975. ```````````````````````````````` example
  5976. []
  5977. []: /uri
  5978. .
  5979. <p>[]</p>
  5980. <p>[]: /uri</p>
  5981. ````````````````````````````````
  5982. ```````````````````````````````` example
  5983. [
  5984. ]
  5985. [
  5986. ]: /uri
  5987. .
  5988. <p>[
  5989. ]</p>
  5990. <p>[
  5991. ]: /uri</p>
  5992. ````````````````````````````````
  5993. A [collapsed reference link](@)
  5994. consists of a [link label] that [matches] a
  5995. [link reference definition] elsewhere in the
  5996. document, followed by the string `[]`.
  5997. The contents of the first link label are parsed as inlines,
  5998. which are used as the link's text. The link's URI and title are
  5999. provided by the matching reference link definition. Thus,
  6000. `[foo][]` is equivalent to `[foo][foo]`.
  6001. ```````````````````````````````` example
  6002. [foo][]
  6003. [foo]: /url "title"
  6004. .
  6005. <p><a href="/url" title="title">foo</a></p>
  6006. ````````````````````````````````
  6007. ```````````````````````````````` example
  6008. [*foo* bar][]
  6009. [*foo* bar]: /url "title"
  6010. .
  6011. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6012. ````````````````````````````````
  6013. The link labels are case-insensitive:
  6014. ```````````````````````````````` example
  6015. [Foo][]
  6016. [foo]: /url "title"
  6017. .
  6018. <p><a href="/url" title="title">Foo</a></p>
  6019. ````````````````````````````````
  6020. As with full reference links, [whitespace] is not
  6021. allowed between the two sets of brackets:
  6022. ```````````````````````````````` example
  6023. [foo]
  6024. []
  6025. [foo]: /url "title"
  6026. .
  6027. <p><a href="/url" title="title">foo</a>
  6028. []</p>
  6029. ````````````````````````````````
  6030. A [shortcut reference link](@)
  6031. consists of a [link label] that [matches] a
  6032. [link reference definition] elsewhere in the
  6033. document and is not followed by `[]` or a link label.
  6034. The contents of the first link label are parsed as inlines,
  6035. which are used as the link's text. The link's URI and title
  6036. are provided by the matching link reference definition.
  6037. Thus, `[foo]` is equivalent to `[foo][]`.
  6038. ```````````````````````````````` example
  6039. [foo]
  6040. [foo]: /url "title"
  6041. .
  6042. <p><a href="/url" title="title">foo</a></p>
  6043. ````````````````````````````````
  6044. ```````````````````````````````` example
  6045. [*foo* bar]
  6046. [*foo* bar]: /url "title"
  6047. .
  6048. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6049. ````````````````````````````````
  6050. ```````````````````````````````` example
  6051. [[*foo* bar]]
  6052. [*foo* bar]: /url "title"
  6053. .
  6054. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6055. ````````````````````````````````
  6056. ```````````````````````````````` example
  6057. [[bar [foo]
  6058. [foo]: /url
  6059. .
  6060. <p>[[bar <a href="/url">foo</a></p>
  6061. ````````````````````````````````
  6062. The link labels are case-insensitive:
  6063. ```````````````````````````````` example
  6064. [Foo]
  6065. [foo]: /url "title"
  6066. .
  6067. <p><a href="/url" title="title">Foo</a></p>
  6068. ````````````````````````````````
  6069. A space after the link text should be preserved:
  6070. ```````````````````````````````` example
  6071. [foo] bar
  6072. [foo]: /url
  6073. .
  6074. <p><a href="/url">foo</a> bar</p>
  6075. ````````````````````````````````
  6076. If you just want bracketed text, you can backslash-escape the
  6077. opening bracket to avoid links:
  6078. ```````````````````````````````` example
  6079. \[foo]
  6080. [foo]: /url "title"
  6081. .
  6082. <p>[foo]</p>
  6083. ````````````````````````````````
  6084. Note that this is a link, because a link label ends with the first
  6085. following closing bracket:
  6086. ```````````````````````````````` example
  6087. [foo*]: /url
  6088. *[foo*]
  6089. .
  6090. <p>*<a href="/url">foo*</a></p>
  6091. ````````````````````````````````
  6092. Full and compact references take precedence over shortcut
  6093. references:
  6094. ```````````````````````````````` example
  6095. [foo][bar]
  6096. [foo]: /url1
  6097. [bar]: /url2
  6098. .
  6099. <p><a href="/url2">foo</a></p>
  6100. ````````````````````````````````
  6101. ```````````````````````````````` example
  6102. [foo][]
  6103. [foo]: /url1
  6104. .
  6105. <p><a href="/url1">foo</a></p>
  6106. ````````````````````````````````
  6107. Inline links also take precedence:
  6108. ```````````````````````````````` example
  6109. [foo]()
  6110. [foo]: /url1
  6111. .
  6112. <p><a href="">foo</a></p>
  6113. ````````````````````````````````
  6114. ```````````````````````````````` example
  6115. [foo](not a link)
  6116. [foo]: /url1
  6117. .
  6118. <p><a href="/url1">foo</a>(not a link)</p>
  6119. ````````````````````````````````
  6120. In the following case `[bar][baz]` is parsed as a reference,
  6121. `[foo]` as normal text:
  6122. ```````````````````````````````` example
  6123. [foo][bar][baz]
  6124. [baz]: /url
  6125. .
  6126. <p>[foo]<a href="/url">bar</a></p>
  6127. ````````````````````````````````
  6128. Here, though, `[foo][bar]` is parsed as a reference, since
  6129. `[bar]` is defined:
  6130. ```````````````````````````````` example
  6131. [foo][bar][baz]
  6132. [baz]: /url1
  6133. [bar]: /url2
  6134. .
  6135. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6136. ````````````````````````````````
  6137. Here `[foo]` is not parsed as a shortcut reference, because it
  6138. is followed by a link label (even though `[bar]` is not defined):
  6139. ```````````````````````````````` example
  6140. [foo][bar][baz]
  6141. [baz]: /url1
  6142. [foo]: /url2
  6143. .
  6144. <p>[foo]<a href="/url1">bar</a></p>
  6145. ````````````````````````````````
  6146. ## Images
  6147. Syntax for images is like the syntax for links, with one
  6148. difference. Instead of [link text], we have an
  6149. [image description](@). The rules for this are the
  6150. same as for [link text], except that (a) an
  6151. image description starts with `![` rather than `[`, and
  6152. (b) an image description may contain links.
  6153. An image description has inline elements
  6154. as its contents. When an image is rendered to HTML,
  6155. this is standardly used as the image's `alt` attribute.
  6156. ```````````````````````````````` example
  6157. ![foo](/url "title")
  6158. .
  6159. <p><img src="/url" alt="foo" title="title" /></p>
  6160. ````````````````````````````````
  6161. ```````````````````````````````` example
  6162. ![foo *bar*]
  6163. [foo *bar*]: train.jpg "train & tracks"
  6164. .
  6165. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6166. ````````````````````````````````
  6167. ```````````````````````````````` example
  6168. ![foo ![bar](/url)](/url2)
  6169. .
  6170. <p><img src="/url2" alt="foo bar" /></p>
  6171. ````````````````````````````````
  6172. ```````````````````````````````` example
  6173. ![foo [bar](/url)](/url2)
  6174. .
  6175. <p><img src="/url2" alt="foo bar" /></p>
  6176. ````````````````````````````````
  6177. Though this spec is concerned with parsing, not rendering, it is
  6178. recommended that in rendering to HTML, only the plain string content
  6179. of the [image description] be used. Note that in
  6180. the above example, the alt attribute's value is `foo bar`, not `foo
  6181. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6182. content is rendered, without formatting.
  6183. ```````````````````````````````` example
  6184. ![foo *bar*][]
  6185. [foo *bar*]: train.jpg "train & tracks"
  6186. .
  6187. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6188. ````````````````````````````````
  6189. ```````````````````````````````` example
  6190. ![foo *bar*][foobar]
  6191. [FOOBAR]: train.jpg "train & tracks"
  6192. .
  6193. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6194. ````````````````````````````````
  6195. ```````````````````````````````` example
  6196. ![foo](train.jpg)
  6197. .
  6198. <p><img src="train.jpg" alt="foo" /></p>
  6199. ````````````````````````````````
  6200. ```````````````````````````````` example
  6201. My ![foo bar](/path/to/train.jpg "title" )
  6202. .
  6203. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6204. ````````````````````````````````
  6205. ```````````````````````````````` example
  6206. ![foo](<url>)
  6207. .
  6208. <p><img src="url" alt="foo" /></p>
  6209. ````````````````````````````````
  6210. ```````````````````````````````` example
  6211. ![](/url)
  6212. .
  6213. <p><img src="/url" alt="" /></p>
  6214. ````````````````````````````````
  6215. Reference-style:
  6216. ```````````````````````````````` example
  6217. ![foo][bar]
  6218. [bar]: /url
  6219. .
  6220. <p><img src="/url" alt="foo" /></p>
  6221. ````````````````````````````````
  6222. ```````````````````````````````` example
  6223. ![foo][bar]
  6224. [BAR]: /url
  6225. .
  6226. <p><img src="/url" alt="foo" /></p>
  6227. ````````````````````````````````
  6228. Collapsed:
  6229. ```````````````````````````````` example
  6230. ![foo][]
  6231. [foo]: /url "title"
  6232. .
  6233. <p><img src="/url" alt="foo" title="title" /></p>
  6234. ````````````````````````````````
  6235. ```````````````````````````````` example
  6236. ![*foo* bar][]
  6237. [*foo* bar]: /url "title"
  6238. .
  6239. <p><img src="/url" alt="foo bar" title="title" /></p>
  6240. ````````````````````````````````
  6241. The labels are case-insensitive:
  6242. ```````````````````````````````` example
  6243. ![Foo][]
  6244. [foo]: /url "title"
  6245. .
  6246. <p><img src="/url" alt="Foo" title="title" /></p>
  6247. ````````````````````````````````
  6248. As with reference links, [whitespace] is not allowed
  6249. between the two sets of brackets:
  6250. ```````````````````````````````` example
  6251. ![foo]
  6252. []
  6253. [foo]: /url "title"
  6254. .
  6255. <p><img src="/url" alt="foo" title="title" />
  6256. []</p>
  6257. ````````````````````````````````
  6258. Shortcut:
  6259. ```````````````````````````````` example
  6260. ![foo]
  6261. [foo]: /url "title"
  6262. .
  6263. <p><img src="/url" alt="foo" title="title" /></p>
  6264. ````````````````````````````````
  6265. ```````````````````````````````` example
  6266. ![*foo* bar]
  6267. [*foo* bar]: /url "title"
  6268. .
  6269. <p><img src="/url" alt="foo bar" title="title" /></p>
  6270. ````````````````````````````````
  6271. Note that link labels cannot contain unescaped brackets:
  6272. ```````````````````````````````` example
  6273. ![[foo]]
  6274. [[foo]]: /url "title"
  6275. .
  6276. <p>![[foo]]</p>
  6277. <p>[[foo]]: /url &quot;title&quot;</p>
  6278. ````````````````````````````````
  6279. The link labels are case-insensitive:
  6280. ```````````````````````````````` example
  6281. ![Foo]
  6282. [foo]: /url "title"
  6283. .
  6284. <p><img src="/url" alt="Foo" title="title" /></p>
  6285. ````````````````````````````````
  6286. If you just want a literal `!` followed by bracketed text, you can
  6287. backslash-escape the opening `[`:
  6288. ```````````````````````````````` example
  6289. !\[foo]
  6290. [foo]: /url "title"
  6291. .
  6292. <p>![foo]</p>
  6293. ````````````````````````````````
  6294. If you want a link after a literal `!`, backslash-escape the
  6295. `!`:
  6296. ```````````````````````````````` example
  6297. \![foo]
  6298. [foo]: /url "title"
  6299. .
  6300. <p>!<a href="/url" title="title">foo</a></p>
  6301. ````````````````````````````````
  6302. ## Autolinks
  6303. [Autolink](@)s are absolute URIs and email addresses inside
  6304. `<` and `>`. They are parsed as links, with the URL or email address
  6305. as the link label.
  6306. A [URI autolink](@) consists of `<`, followed by an
  6307. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6308. a link to the URI, with the URI as the link's label.
  6309. An [absolute URI](@),
  6310. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6311. followed by zero or more characters other than ASCII
  6312. [whitespace] and control characters, `<`, and `>`. If
  6313. the URI includes these characters, they must be percent-encoded
  6314. (e.g. `%20` for a space).
  6315. For purposes of this spec, a [scheme](@) is any sequence
  6316. of 2--32 characters beginning with an ASCII letter and followed
  6317. by any combination of ASCII letters, digits, or the symbols plus
  6318. ("+"), period ("."), or hyphen ("-").
  6319. Here are some valid autolinks:
  6320. ```````````````````````````````` example
  6321. <http://foo.bar.baz>
  6322. .
  6323. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6324. ````````````````````````````````
  6325. ```````````````````````````````` example
  6326. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6327. .
  6328. <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>
  6329. ````````````````````````````````
  6330. ```````````````````````````````` example
  6331. <irc://foo.bar:2233/baz>
  6332. .
  6333. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6334. ````````````````````````````````
  6335. Uppercase is also fine:
  6336. ```````````````````````````````` example
  6337. <MAILTO:FOO@BAR.BAZ>
  6338. .
  6339. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6340. ````````````````````````````````
  6341. Note that many strings that count as [absolute URIs] for
  6342. purposes of this spec are not valid URIs, because their
  6343. schemes are not registered or because of other problems
  6344. with their syntax:
  6345. ```````````````````````````````` example
  6346. <a+b+c:d>
  6347. .
  6348. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6349. ````````````````````````````````
  6350. ```````````````````````````````` example
  6351. <made-up-scheme://foo,bar>
  6352. .
  6353. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6354. ````````````````````````````````
  6355. ```````````````````````````````` example
  6356. <http://../>
  6357. .
  6358. <p><a href="http://../">http://../</a></p>
  6359. ````````````````````````````````
  6360. ```````````````````````````````` example
  6361. <localhost:5001/foo>
  6362. .
  6363. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6364. ````````````````````````````````
  6365. Spaces are not allowed in autolinks:
  6366. ```````````````````````````````` example
  6367. <http://foo.bar/baz bim>
  6368. .
  6369. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6370. ````````````````````````````````
  6371. Backslash-escapes do not work inside autolinks:
  6372. ```````````````````````````````` example
  6373. <http://example.com/\[\>
  6374. .
  6375. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6376. ````````````````````````````````
  6377. An [email autolink](@)
  6378. consists of `<`, followed by an [email address],
  6379. followed by `>`. The link's label is the email address,
  6380. and the URL is `mailto:` followed by the email address.
  6381. An [email address](@),
  6382. for these purposes, is anything that matches
  6383. the [non-normative regex from the HTML5
  6384. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6385. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6386. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6387. Examples of email autolinks:
  6388. ```````````````````````````````` example
  6389. <foo@bar.example.com>
  6390. .
  6391. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6392. ````````````````````````````````
  6393. ```````````````````````````````` example
  6394. <foo+special@Bar.baz-bar0.com>
  6395. .
  6396. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6397. ````````````````````````````````
  6398. Backslash-escapes do not work inside email autolinks:
  6399. ```````````````````````````````` example
  6400. <foo\+@bar.example.com>
  6401. .
  6402. <p>&lt;foo+@bar.example.com&gt;</p>
  6403. ````````````````````````````````
  6404. These are not autolinks:
  6405. ```````````````````````````````` example
  6406. <>
  6407. .
  6408. <p>&lt;&gt;</p>
  6409. ````````````````````````````````
  6410. ```````````````````````````````` example
  6411. < http://foo.bar >
  6412. .
  6413. <p>&lt; http://foo.bar &gt;</p>
  6414. ````````````````````````````````
  6415. ```````````````````````````````` example
  6416. <m:abc>
  6417. .
  6418. <p>&lt;m:abc&gt;</p>
  6419. ````````````````````````````````
  6420. ```````````````````````````````` example
  6421. <foo.bar.baz>
  6422. .
  6423. <p>&lt;foo.bar.baz&gt;</p>
  6424. ````````````````````````````````
  6425. ```````````````````````````````` example
  6426. http://example.com
  6427. .
  6428. <p>http://example.com</p>
  6429. ````````````````````````````````
  6430. ```````````````````````````````` example
  6431. foo@bar.example.com
  6432. .
  6433. <p>foo@bar.example.com</p>
  6434. ````````````````````````````````
  6435. ## Raw HTML
  6436. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6437. raw HTML tag and will be rendered in HTML without escaping.
  6438. Tag and attribute names are not limited to current HTML tags,
  6439. so custom tags (and even, say, DocBook tags) may be used.
  6440. Here is the grammar for tags:
  6441. A [tag name](@) consists of an ASCII letter
  6442. followed by zero or more ASCII letters, digits, or
  6443. hyphens (`-`).
  6444. An [attribute](@) consists of [whitespace],
  6445. an [attribute name], and an optional
  6446. [attribute value specification].
  6447. An [attribute name](@)
  6448. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6449. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6450. specification restricted to ASCII. HTML5 is laxer.)
  6451. An [attribute value specification](@)
  6452. consists of optional [whitespace],
  6453. a `=` character, optional [whitespace], and an [attribute
  6454. value].
  6455. An [attribute value](@)
  6456. consists of an [unquoted attribute value],
  6457. a [single-quoted attribute value], or a [double-quoted attribute value].
  6458. An [unquoted attribute value](@)
  6459. is a nonempty string of characters not
  6460. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6461. A [single-quoted attribute value](@)
  6462. consists of `'`, zero or more
  6463. characters not including `'`, and a final `'`.
  6464. A [double-quoted attribute value](@)
  6465. consists of `"`, zero or more
  6466. characters not including `"`, and a final `"`.
  6467. An [open tag](@) consists of a `<` character, a [tag name],
  6468. zero or more [attributes], optional [whitespace], an optional `/`
  6469. character, and a `>` character.
  6470. A [closing tag](@) consists of the string `</`, a
  6471. [tag name], optional [whitespace], and the character `>`.
  6472. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6473. where *text* does not start with `>` or `->`, does not end with `-`,
  6474. and does not contain `--`. (See the
  6475. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6476. A [processing instruction](@)
  6477. consists of the string `<?`, a string
  6478. of characters not including the string `?>`, and the string
  6479. `?>`.
  6480. A [declaration](@) consists of the
  6481. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6482. [whitespace], a string of characters not including the
  6483. character `>`, and the character `>`.
  6484. A [CDATA section](@) consists of
  6485. the string `<![CDATA[`, a string of characters not including the string
  6486. `]]>`, and the string `]]>`.
  6487. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6488. an [HTML comment], a [processing instruction], a [declaration],
  6489. or a [CDATA section].
  6490. Here are some simple open tags:
  6491. ```````````````````````````````` example
  6492. <a><bab><c2c>
  6493. .
  6494. <p><a><bab><c2c></p>
  6495. ````````````````````````````````
  6496. Empty elements:
  6497. ```````````````````````````````` example
  6498. <a/><b2/>
  6499. .
  6500. <p><a/><b2/></p>
  6501. ````````````````````````````````
  6502. [Whitespace] is allowed:
  6503. ```````````````````````````````` example
  6504. <a /><b2
  6505. data="foo" >
  6506. .
  6507. <p><a /><b2
  6508. data="foo" ></p>
  6509. ````````````````````````````````
  6510. With attributes:
  6511. ```````````````````````````````` example
  6512. <a foo="bar" bam = 'baz <em>"</em>'
  6513. _boolean zoop:33=zoop:33 />
  6514. .
  6515. <p><a foo="bar" bam = 'baz <em>"</em>'
  6516. _boolean zoop:33=zoop:33 /></p>
  6517. ````````````````````````````````
  6518. Custom tag names can be used:
  6519. ```````````````````````````````` example
  6520. Foo <responsive-image src="foo.jpg" />
  6521. .
  6522. <p>Foo <responsive-image src="foo.jpg" /></p>
  6523. ````````````````````````````````
  6524. Illegal tag names, not parsed as HTML:
  6525. ```````````````````````````````` example
  6526. <33> <__>
  6527. .
  6528. <p>&lt;33&gt; &lt;__&gt;</p>
  6529. ````````````````````````````````
  6530. Illegal attribute names:
  6531. ```````````````````````````````` example
  6532. <a h*#ref="hi">
  6533. .
  6534. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6535. ````````````````````````````````
  6536. Illegal attribute values:
  6537. ```````````````````````````````` example
  6538. <a href="hi'> <a href=hi'>
  6539. .
  6540. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6541. ````````````````````````````````
  6542. Illegal [whitespace]:
  6543. ```````````````````````````````` example
  6544. < a><
  6545. foo><bar/ >
  6546. .
  6547. <p>&lt; a&gt;&lt;
  6548. foo&gt;&lt;bar/ &gt;</p>
  6549. ````````````````````````````````
  6550. Missing [whitespace]:
  6551. ```````````````````````````````` example
  6552. <a href='bar'title=title>
  6553. .
  6554. <p>&lt;a href='bar'title=title&gt;</p>
  6555. ````````````````````````````````
  6556. Closing tags:
  6557. ```````````````````````````````` example
  6558. </a></foo >
  6559. .
  6560. <p></a></foo ></p>
  6561. ````````````````````````````````
  6562. Illegal attributes in closing tag:
  6563. ```````````````````````````````` example
  6564. </a href="foo">
  6565. .
  6566. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6567. ````````````````````````````````
  6568. Comments:
  6569. ```````````````````````````````` example
  6570. foo <!-- this is a
  6571. comment - with hyphen -->
  6572. .
  6573. <p>foo <!-- this is a
  6574. comment - with hyphen --></p>
  6575. ````````````````````````````````
  6576. ```````````````````````````````` example
  6577. foo <!-- not a comment -- two hyphens -->
  6578. .
  6579. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6580. ````````````````````````````````
  6581. Not comments:
  6582. ```````````````````````````````` example
  6583. foo <!--> foo -->
  6584. foo <!-- foo--->
  6585. .
  6586. <p>foo &lt;!--&gt; foo --&gt;</p>
  6587. <p>foo &lt;!-- foo---&gt;</p>
  6588. ````````````````````````````````
  6589. Processing instructions:
  6590. ```````````````````````````````` example
  6591. foo <?php echo $a; ?>
  6592. .
  6593. <p>foo <?php echo $a; ?></p>
  6594. ````````````````````````````````
  6595. Declarations:
  6596. ```````````````````````````````` example
  6597. foo <!ELEMENT br EMPTY>
  6598. .
  6599. <p>foo <!ELEMENT br EMPTY></p>
  6600. ````````````````````````````````
  6601. CDATA sections:
  6602. ```````````````````````````````` example
  6603. foo <![CDATA[>&<]]>
  6604. .
  6605. <p>foo <![CDATA[>&<]]></p>
  6606. ````````````````````````````````
  6607. Entity and numeric character references are preserved in HTML
  6608. attributes:
  6609. ```````````````````````````````` example
  6610. foo <a href="&ouml;">
  6611. .
  6612. <p>foo <a href="&ouml;"></p>
  6613. ````````````````````````````````
  6614. Backslash escapes do not work in HTML attributes:
  6615. ```````````````````````````````` example
  6616. foo <a href="\*">
  6617. .
  6618. <p>foo <a href="\*"></p>
  6619. ````````````````````````````````
  6620. ```````````````````````````````` example
  6621. <a href="\"">
  6622. .
  6623. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6624. ````````````````````````````````
  6625. ## Hard line breaks
  6626. A line break (not in a code span or HTML tag) that is preceded
  6627. by two or more spaces and does not occur at the end of a block
  6628. is parsed as a [hard line break](@) (rendered
  6629. in HTML as a `<br />` tag):
  6630. ```````````````````````````````` example
  6631. foo
  6632. baz
  6633. .
  6634. <p>foo<br />
  6635. baz</p>
  6636. ````````````````````````````````
  6637. For a more visible alternative, a backslash before the
  6638. [line ending] may be used instead of two spaces:
  6639. ```````````````````````````````` example
  6640. foo\
  6641. baz
  6642. .
  6643. <p>foo<br />
  6644. baz</p>
  6645. ````````````````````````````````
  6646. More than two spaces can be used:
  6647. ```````````````````````````````` example
  6648. foo
  6649. baz
  6650. .
  6651. <p>foo<br />
  6652. baz</p>
  6653. ````````````````````````````````
  6654. Leading spaces at the beginning of the next line are ignored:
  6655. ```````````````````````````````` example
  6656. foo
  6657. bar
  6658. .
  6659. <p>foo<br />
  6660. bar</p>
  6661. ````````````````````````````````
  6662. ```````````````````````````````` example
  6663. foo\
  6664. bar
  6665. .
  6666. <p>foo<br />
  6667. bar</p>
  6668. ````````````````````````````````
  6669. Line breaks can occur inside emphasis, links, and other constructs
  6670. that allow inline content:
  6671. ```````````````````````````````` example
  6672. *foo
  6673. bar*
  6674. .
  6675. <p><em>foo<br />
  6676. bar</em></p>
  6677. ````````````````````````````````
  6678. ```````````````````````````````` example
  6679. *foo\
  6680. bar*
  6681. .
  6682. <p><em>foo<br />
  6683. bar</em></p>
  6684. ````````````````````````````````
  6685. Line breaks do not occur inside code spans
  6686. ```````````````````````````````` example
  6687. `code
  6688. span`
  6689. .
  6690. <p><code>code span</code></p>
  6691. ````````````````````````````````
  6692. ```````````````````````````````` example
  6693. `code\
  6694. span`
  6695. .
  6696. <p><code>code\ span</code></p>
  6697. ````````````````````````````````
  6698. or HTML tags:
  6699. ```````````````````````````````` example
  6700. <a href="foo
  6701. bar">
  6702. .
  6703. <p><a href="foo
  6704. bar"></p>
  6705. ````````````````````````````````
  6706. ```````````````````````````````` example
  6707. <a href="foo\
  6708. bar">
  6709. .
  6710. <p><a href="foo\
  6711. bar"></p>
  6712. ````````````````````````````````
  6713. Hard line breaks are for separating inline content within a block.
  6714. Neither syntax for hard line breaks works at the end of a paragraph or
  6715. other block element:
  6716. ```````````````````````````````` example
  6717. foo\
  6718. .
  6719. <p>foo\</p>
  6720. ````````````````````````````````
  6721. ```````````````````````````````` example
  6722. foo
  6723. .
  6724. <p>foo</p>
  6725. ````````````````````````````````
  6726. ```````````````````````````````` example
  6727. ### foo\
  6728. .
  6729. <h3>foo\</h3>
  6730. ````````````````````````````````
  6731. ```````````````````````````````` example
  6732. ### foo
  6733. .
  6734. <h3>foo</h3>
  6735. ````````````````````````````````
  6736. ## Soft line breaks
  6737. A regular line break (not in a code span or HTML tag) that is not
  6738. preceded by two or more spaces or a backslash is parsed as a
  6739. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6740. [line ending] or as a space. The result will be the same in
  6741. browsers. In the examples here, a [line ending] will be used.)
  6742. ```````````````````````````````` example
  6743. foo
  6744. baz
  6745. .
  6746. <p>foo
  6747. baz</p>
  6748. ````````````````````````````````
  6749. Spaces at the end of the line and beginning of the next line are
  6750. removed:
  6751. ```````````````````````````````` example
  6752. foo
  6753. baz
  6754. .
  6755. <p>foo
  6756. baz</p>
  6757. ````````````````````````````````
  6758. A conforming parser may render a soft line break in HTML either as a
  6759. line break or as a space.
  6760. A renderer may also provide an option to render soft line breaks
  6761. as hard line breaks.
  6762. ## Textual content
  6763. Any characters not given an interpretation by the above rules will
  6764. be parsed as plain textual content.
  6765. ```````````````````````````````` example
  6766. hello $.;'there
  6767. .
  6768. <p>hello $.;'there</p>
  6769. ````````````````````````````````
  6770. ```````````````````````````````` example
  6771. Foo χρῆν
  6772. .
  6773. <p>Foo χρῆν</p>
  6774. ````````````````````````````````
  6775. Internal spaces are preserved verbatim:
  6776. ```````````````````````````````` example
  6777. Multiple spaces
  6778. .
  6779. <p>Multiple spaces</p>
  6780. ````````````````````````````````
  6781. <!-- END TESTS -->
  6782. # Appendix: A parsing strategy
  6783. In this appendix we describe some features of the parsing strategy
  6784. used in the CommonMark reference implementations.
  6785. ## Overview
  6786. Parsing has two phases:
  6787. 1. In the first phase, lines of input are consumed and the block
  6788. structure of the document---its division into paragraphs, block quotes,
  6789. list items, and so on---is constructed. Text is assigned to these
  6790. blocks but not parsed. Link reference definitions are parsed and a
  6791. map of links is constructed.
  6792. 2. In the second phase, the raw text contents of paragraphs and headings
  6793. are parsed into sequences of Markdown inline elements (strings,
  6794. code spans, links, emphasis, and so on), using the map of link
  6795. references constructed in phase 1.
  6796. At each point in processing, the document is represented as a tree of
  6797. **blocks**. The root of the tree is a `document` block. The `document`
  6798. may have any number of other blocks as **children**. These children
  6799. may, in turn, have other blocks as children. The last child of a block
  6800. is normally considered **open**, meaning that subsequent lines of input
  6801. can alter its contents. (Blocks that are not open are **closed**.)
  6802. Here, for example, is a possible document tree, with the open blocks
  6803. marked by arrows:
  6804. ``` tree
  6805. -> document
  6806. -> block_quote
  6807. paragraph
  6808. "Lorem ipsum dolor\nsit amet."
  6809. -> list (type=bullet tight=true bullet_char=-)
  6810. list_item
  6811. paragraph
  6812. "Qui *quodsi iracundia*"
  6813. -> list_item
  6814. -> paragraph
  6815. "aliquando id"
  6816. ```
  6817. ## Phase 1: block structure
  6818. Each line that is processed has an effect on this tree. The line is
  6819. analyzed and, depending on its contents, the document may be altered
  6820. in one or more of the following ways:
  6821. 1. One or more open blocks may be closed.
  6822. 2. One or more new blocks may be created as children of the
  6823. last open block.
  6824. 3. Text may be added to the last (deepest) open block remaining
  6825. on the tree.
  6826. Once a line has been incorporated into the tree in this way,
  6827. it can be discarded, so input can be read in a stream.
  6828. For each line, we follow this procedure:
  6829. 1. First we iterate through the open blocks, starting with the
  6830. root document, and descending through last children down to the last
  6831. open block. Each block imposes a condition that the line must satisfy
  6832. if the block is to remain open. For example, a block quote requires a
  6833. `>` character. A paragraph requires a non-blank line.
  6834. In this phase we may match all or just some of the open
  6835. blocks. But we cannot close unmatched blocks yet, because we may have a
  6836. [lazy continuation line].
  6837. 2. Next, after consuming the continuation markers for existing
  6838. blocks, we look for new block starts (e.g. `>` for a block quote).
  6839. If we encounter a new block start, we close any blocks unmatched
  6840. in step 1 before creating the new block as a child of the last
  6841. matched block.
  6842. 3. Finally, we look at the remainder of the line (after block
  6843. markers like `>`, list markers, and indentation have been consumed).
  6844. This is text that can be incorporated into the last open
  6845. block (a paragraph, code block, heading, or raw HTML).
  6846. Setext headings are formed when we see a line of a paragraph
  6847. that is a [setext heading underline].
  6848. Reference link definitions are detected when a paragraph is closed;
  6849. the accumulated text lines are parsed to see if they begin with
  6850. one or more reference link definitions. Any remainder becomes a
  6851. normal paragraph.
  6852. We can see how this works by considering how the tree above is
  6853. generated by four lines of Markdown:
  6854. ``` markdown
  6855. > Lorem ipsum dolor
  6856. sit amet.
  6857. > - Qui *quodsi iracundia*
  6858. > - aliquando id
  6859. ```
  6860. At the outset, our document model is just
  6861. ``` tree
  6862. -> document
  6863. ```
  6864. The first line of our text,
  6865. ``` markdown
  6866. > Lorem ipsum dolor
  6867. ```
  6868. causes a `block_quote` block to be created as a child of our
  6869. open `document` block, and a `paragraph` block as a child of
  6870. the `block_quote`. Then the text is added to the last open
  6871. block, the `paragraph`:
  6872. ``` tree
  6873. -> document
  6874. -> block_quote
  6875. -> paragraph
  6876. "Lorem ipsum dolor"
  6877. ```
  6878. The next line,
  6879. ``` markdown
  6880. sit amet.
  6881. ```
  6882. is a "lazy continuation" of the open `paragraph`, so it gets added
  6883. to the paragraph's text:
  6884. ``` tree
  6885. -> document
  6886. -> block_quote
  6887. -> paragraph
  6888. "Lorem ipsum dolor\nsit amet."
  6889. ```
  6890. The third line,
  6891. ``` markdown
  6892. > - Qui *quodsi iracundia*
  6893. ```
  6894. causes the `paragraph` block to be closed, and a new `list` block
  6895. opened as a child of the `block_quote`. A `list_item` is also
  6896. added as a child of the `list`, and a `paragraph` as a child of
  6897. the `list_item`. The text is then added to the new `paragraph`:
  6898. ``` tree
  6899. -> document
  6900. -> block_quote
  6901. paragraph
  6902. "Lorem ipsum dolor\nsit amet."
  6903. -> list (type=bullet tight=true bullet_char=-)
  6904. -> list_item
  6905. -> paragraph
  6906. "Qui *quodsi iracundia*"
  6907. ```
  6908. The fourth line,
  6909. ``` markdown
  6910. > - aliquando id
  6911. ```
  6912. causes the `list_item` (and its child the `paragraph`) to be closed,
  6913. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6914. is added as a child of the new `list_item`, to contain the text.
  6915. We thus obtain the final tree:
  6916. ``` tree
  6917. -> document
  6918. -> block_quote
  6919. paragraph
  6920. "Lorem ipsum dolor\nsit amet."
  6921. -> list (type=bullet tight=true bullet_char=-)
  6922. list_item
  6923. paragraph
  6924. "Qui *quodsi iracundia*"
  6925. -> list_item
  6926. -> paragraph
  6927. "aliquando id"
  6928. ```
  6929. ## Phase 2: inline structure
  6930. Once all of the input has been parsed, all open blocks are closed.
  6931. We then "walk the tree," visiting every node, and parse raw
  6932. string contents of paragraphs and headings as inlines. At this
  6933. point we have seen all the link reference definitions, so we can
  6934. resolve reference links as we go.
  6935. ``` tree
  6936. document
  6937. block_quote
  6938. paragraph
  6939. str "Lorem ipsum dolor"
  6940. softbreak
  6941. str "sit amet."
  6942. list (type=bullet tight=true bullet_char=-)
  6943. list_item
  6944. paragraph
  6945. str "Qui "
  6946. emph
  6947. str "quodsi iracundia"
  6948. list_item
  6949. paragraph
  6950. str "aliquando id"
  6951. ```
  6952. Notice how the [line ending] in the first paragraph has
  6953. been parsed as a `softbreak`, and the asterisks in the first list item
  6954. have become an `emph`.
  6955. ### An algorithm for parsing nested emphasis and links
  6956. By far the trickiest part of inline parsing is handling emphasis,
  6957. strong emphasis, links, and images. This is done using the following
  6958. algorithm.
  6959. When we're parsing inlines and we hit either
  6960. - a run of `*` or `_` characters, or
  6961. - a `[` or `![`
  6962. we insert a text node with these symbols as its literal content, and we
  6963. add a pointer to this text node to the [delimiter stack](@).
  6964. The [delimiter stack] is a doubly linked list. Each
  6965. element contains a pointer to a text node, plus information about
  6966. - the type of delimiter (`[`, `![`, `*`, `_`)
  6967. - the number of delimiters,
  6968. - whether the delimiter is "active" (all are active to start), and
  6969. - whether the delimiter is a potential opener, a potential closer,
  6970. or both (which depends on what sort of characters precede
  6971. and follow the delimiters).
  6972. When we hit a `]` character, we call the *look for link or image*
  6973. procedure (see below).
  6974. When we hit the end of the input, we call the *process emphasis*
  6975. procedure (see below), with `stack_bottom` = NULL.
  6976. #### *look for link or image*
  6977. Starting at the top of the delimiter stack, we look backwards
  6978. through the stack for an opening `[` or `![` delimiter.
  6979. - If we don't find one, we return a literal text node `]`.
  6980. - If we do find one, but it's not *active*, we remove the inactive
  6981. delimiter from the stack, and return a literal text node `]`.
  6982. - If we find one and it's active, then we parse ahead to see if
  6983. we have an inline link/image, reference link/image, compact reference
  6984. link/image, or shortcut reference link/image.
  6985. + If we don't, then we remove the opening delimiter from the
  6986. delimiter stack and return a literal text node `]`.
  6987. + If we do, then
  6988. * We return a link or image node whose children are the inlines
  6989. after the text node pointed to by the opening delimiter.
  6990. * We run *process emphasis* on these inlines, with the `[` opener
  6991. as `stack_bottom`.
  6992. * We remove the opening delimiter.
  6993. * If we have a link (and not an image), we also set all
  6994. `[` delimiters before the opening delimiter to *inactive*. (This
  6995. will prevent us from getting links within links.)
  6996. #### *process emphasis*
  6997. Parameter `stack_bottom` sets a lower bound to how far we
  6998. descend in the [delimiter stack]. If it is NULL, we can
  6999. go all the way to the bottom. Otherwise, we stop before
  7000. visiting `stack_bottom`.
  7001. Let `current_position` point to the element on the [delimiter stack]
  7002. just above `stack_bottom` (or the first element if `stack_bottom`
  7003. is NULL).
  7004. We keep track of the `openers_bottom` for each delimiter
  7005. type (`*`, `_`). Initialize this to `stack_bottom`.
  7006. Then we repeat the following until we run out of potential
  7007. closers:
  7008. - Move `current_position` forward in the delimiter stack (if needed)
  7009. until we find the first potential closer with delimiter `*` or `_`.
  7010. (This will be the potential closer closest
  7011. to the beginning of the input -- the first one in parse order.)
  7012. - Now, look back in the stack (staying above `stack_bottom` and
  7013. the `openers_bottom` for this delimiter type) for the
  7014. first matching potential opener ("matching" means same delimiter).
  7015. - If one is found:
  7016. + Figure out whether we have emphasis or strong emphasis:
  7017. if both closer and opener spans have length >= 2, we have
  7018. strong, otherwise regular.
  7019. + Insert an emph or strong emph node accordingly, after
  7020. the text node corresponding to the opener.
  7021. + Remove any delimiters between the opener and closer from
  7022. the delimiter stack.
  7023. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7024. from the opening and closing text nodes. If they become empty
  7025. as a result, remove them and remove the corresponding element
  7026. of the delimiter stack. If the closing node is removed, reset
  7027. `current_position` to the next element in the stack.
  7028. - If none in found:
  7029. + Set `openers_bottom` to the element before `current_position`.
  7030. (We know that there are no openers for this kind of closer up to and
  7031. including this point, so this puts a lower bound on future searches.)
  7032. + If the closer at `current_position` is not a potential opener,
  7033. remove it from the delimiter stack (since we know it can't
  7034. be a closer either).
  7035. + Advance `current_position` to the next element in the stack.
  7036. After we're done, we remove all delimiters above `stack_bottom` from the
  7037. delimiter stack.