aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: f0ba35059053ff175822611d64adcdd428d97a5f (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:
  2787. 1. When the first list item in a [list] interrupts
  2788. a paragraph---that is, when it starts on a line that would
  2789. otherwise count as [paragraph continuation text]---then (a)
  2790. the lines *Ls* must not begin with a blank line, and (b) if
  2791. the list item is ordered, the start number must be 1.
  2792. 2. If any line is a [thematic break][thematic breaks] then
  2793. that line is not a list item.
  2794. For example, let *Ls* be the lines
  2795. ```````````````````````````````` example
  2796. A paragraph
  2797. with two lines.
  2798. indented code
  2799. > A block quote.
  2800. .
  2801. <p>A paragraph
  2802. with two lines.</p>
  2803. <pre><code>indented code
  2804. </code></pre>
  2805. <blockquote>
  2806. <p>A block quote.</p>
  2807. </blockquote>
  2808. ````````````````````````````````
  2809. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2810. that the following is an ordered list item with start number 1,
  2811. and the same contents as *Ls*:
  2812. ```````````````````````````````` example
  2813. 1. A paragraph
  2814. with two lines.
  2815. indented code
  2816. > A block quote.
  2817. .
  2818. <ol>
  2819. <li>
  2820. <p>A paragraph
  2821. with two lines.</p>
  2822. <pre><code>indented code
  2823. </code></pre>
  2824. <blockquote>
  2825. <p>A block quote.</p>
  2826. </blockquote>
  2827. </li>
  2828. </ol>
  2829. ````````````````````````````````
  2830. The most important thing to notice is that the position of
  2831. the text after the list marker determines how much indentation
  2832. is needed in subsequent blocks in the list item. If the list
  2833. marker takes up two spaces, and there are three spaces between
  2834. the list marker and the next [non-whitespace character], then blocks
  2835. must be indented five spaces in order to fall under the list
  2836. item.
  2837. Here are some examples showing how far content must be indented to be
  2838. put under the list item:
  2839. ```````````````````````````````` example
  2840. - one
  2841. two
  2842. .
  2843. <ul>
  2844. <li>one</li>
  2845. </ul>
  2846. <p>two</p>
  2847. ````````````````````````````````
  2848. ```````````````````````````````` example
  2849. - one
  2850. two
  2851. .
  2852. <ul>
  2853. <li>
  2854. <p>one</p>
  2855. <p>two</p>
  2856. </li>
  2857. </ul>
  2858. ````````````````````````````````
  2859. ```````````````````````````````` example
  2860. - one
  2861. two
  2862. .
  2863. <ul>
  2864. <li>one</li>
  2865. </ul>
  2866. <pre><code> two
  2867. </code></pre>
  2868. ````````````````````````````````
  2869. ```````````````````````````````` example
  2870. - one
  2871. two
  2872. .
  2873. <ul>
  2874. <li>
  2875. <p>one</p>
  2876. <p>two</p>
  2877. </li>
  2878. </ul>
  2879. ````````````````````````````````
  2880. It is tempting to think of this in terms of columns: the continuation
  2881. blocks must be indented at least to the column of the first
  2882. [non-whitespace character] after the list marker. However, that is not quite right.
  2883. The spaces after the list marker determine how much relative indentation
  2884. is needed. Which column this indentation reaches will depend on
  2885. how the list item is embedded in other constructions, as shown by
  2886. this example:
  2887. ```````````````````````````````` example
  2888. > > 1. one
  2889. >>
  2890. >> two
  2891. .
  2892. <blockquote>
  2893. <blockquote>
  2894. <ol>
  2895. <li>
  2896. <p>one</p>
  2897. <p>two</p>
  2898. </li>
  2899. </ol>
  2900. </blockquote>
  2901. </blockquote>
  2902. ````````````````````````````````
  2903. Here `two` occurs in the same column as the list marker `1.`,
  2904. but is actually contained in the list item, because there is
  2905. sufficient indentation after the last containing blockquote marker.
  2906. The converse is also possible. In the following example, the word `two`
  2907. occurs far to the right of the initial text of the list item, `one`, but
  2908. it is not considered part of the list item, because it is not indented
  2909. far enough past the blockquote marker:
  2910. ```````````````````````````````` example
  2911. >>- one
  2912. >>
  2913. > > two
  2914. .
  2915. <blockquote>
  2916. <blockquote>
  2917. <ul>
  2918. <li>one</li>
  2919. </ul>
  2920. <p>two</p>
  2921. </blockquote>
  2922. </blockquote>
  2923. ````````````````````````````````
  2924. Note that at least one space is needed between the list marker and
  2925. any following content, so these are not list items:
  2926. ```````````````````````````````` example
  2927. -one
  2928. 2.two
  2929. .
  2930. <p>-one</p>
  2931. <p>2.two</p>
  2932. ````````````````````````````````
  2933. A list item may contain blocks that are separated by more than
  2934. one blank line.
  2935. ```````````````````````````````` example
  2936. - foo
  2937. bar
  2938. .
  2939. <ul>
  2940. <li>
  2941. <p>foo</p>
  2942. <p>bar</p>
  2943. </li>
  2944. </ul>
  2945. ````````````````````````````````
  2946. A list item may contain any kind of block:
  2947. ```````````````````````````````` example
  2948. 1. foo
  2949. ```
  2950. bar
  2951. ```
  2952. baz
  2953. > bam
  2954. .
  2955. <ol>
  2956. <li>
  2957. <p>foo</p>
  2958. <pre><code>bar
  2959. </code></pre>
  2960. <p>baz</p>
  2961. <blockquote>
  2962. <p>bam</p>
  2963. </blockquote>
  2964. </li>
  2965. </ol>
  2966. ````````````````````````````````
  2967. A list item that contains an indented code block will preserve
  2968. empty lines within the code block verbatim.
  2969. ```````````````````````````````` example
  2970. - Foo
  2971. bar
  2972. baz
  2973. .
  2974. <ul>
  2975. <li>
  2976. <p>Foo</p>
  2977. <pre><code>bar
  2978. baz
  2979. </code></pre>
  2980. </li>
  2981. </ul>
  2982. ````````````````````````````````
  2983. Note that ordered list start numbers must be nine digits or less:
  2984. ```````````````````````````````` example
  2985. 123456789. ok
  2986. .
  2987. <ol start="123456789">
  2988. <li>ok</li>
  2989. </ol>
  2990. ````````````````````````````````
  2991. ```````````````````````````````` example
  2992. 1234567890. not ok
  2993. .
  2994. <p>1234567890. not ok</p>
  2995. ````````````````````````````````
  2996. A start number may begin with 0s:
  2997. ```````````````````````````````` example
  2998. 0. ok
  2999. .
  3000. <ol start="0">
  3001. <li>ok</li>
  3002. </ol>
  3003. ````````````````````````````````
  3004. ```````````````````````````````` example
  3005. 003. ok
  3006. .
  3007. <ol start="3">
  3008. <li>ok</li>
  3009. </ol>
  3010. ````````````````````````````````
  3011. A start number may not be negative:
  3012. ```````````````````````````````` example
  3013. -1. not ok
  3014. .
  3015. <p>-1. not ok</p>
  3016. ````````````````````````````````
  3017. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3018. constitute a sequence of blocks *Bs* starting with an indented code
  3019. block and not separated from each other by more than one blank line,
  3020. and *M* is a list marker of width *W* followed by
  3021. one space, then the result of prepending *M* and the following
  3022. space to the first line of *Ls*, and indenting subsequent lines of
  3023. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3024. If a line is empty, then it need not be indented. The type of the
  3025. list item (bullet or ordered) is determined by the type of its list
  3026. marker. If the list item is ordered, then it is also assigned a
  3027. start number, based on the ordered list marker.
  3028. An indented code block will have to be indented four spaces beyond
  3029. the edge of the region where text will be included in the list item.
  3030. In the following case that is 6 spaces:
  3031. ```````````````````````````````` example
  3032. - foo
  3033. bar
  3034. .
  3035. <ul>
  3036. <li>
  3037. <p>foo</p>
  3038. <pre><code>bar
  3039. </code></pre>
  3040. </li>
  3041. </ul>
  3042. ````````````````````````````````
  3043. And in this case it is 11 spaces:
  3044. ```````````````````````````````` example
  3045. 10. foo
  3046. bar
  3047. .
  3048. <ol start="10">
  3049. <li>
  3050. <p>foo</p>
  3051. <pre><code>bar
  3052. </code></pre>
  3053. </li>
  3054. </ol>
  3055. ````````````````````````````````
  3056. If the *first* block in the list item is an indented code block,
  3057. then by rule #2, the contents must be indented *one* space after the
  3058. list marker:
  3059. ```````````````````````````````` example
  3060. indented code
  3061. paragraph
  3062. more code
  3063. .
  3064. <pre><code>indented code
  3065. </code></pre>
  3066. <p>paragraph</p>
  3067. <pre><code>more code
  3068. </code></pre>
  3069. ````````````````````````````````
  3070. ```````````````````````````````` example
  3071. 1. indented code
  3072. paragraph
  3073. more code
  3074. .
  3075. <ol>
  3076. <li>
  3077. <pre><code>indented code
  3078. </code></pre>
  3079. <p>paragraph</p>
  3080. <pre><code>more code
  3081. </code></pre>
  3082. </li>
  3083. </ol>
  3084. ````````````````````````````````
  3085. Note that an additional space indent is interpreted as space
  3086. inside the code block:
  3087. ```````````````````````````````` example
  3088. 1. indented code
  3089. paragraph
  3090. more code
  3091. .
  3092. <ol>
  3093. <li>
  3094. <pre><code> indented code
  3095. </code></pre>
  3096. <p>paragraph</p>
  3097. <pre><code>more code
  3098. </code></pre>
  3099. </li>
  3100. </ol>
  3101. ````````````````````````````````
  3102. Note that rules #1 and #2 only apply to two cases: (a) cases
  3103. in which the lines to be included in a list item begin with a
  3104. [non-whitespace character], and (b) cases in which
  3105. they begin with an indented code
  3106. block. In a case like the following, where the first block begins with
  3107. a three-space indent, the rules do not allow us to form a list item by
  3108. indenting the whole thing and prepending a list marker:
  3109. ```````````````````````````````` example
  3110. foo
  3111. bar
  3112. .
  3113. <p>foo</p>
  3114. <p>bar</p>
  3115. ````````````````````````````````
  3116. ```````````````````````````````` example
  3117. - foo
  3118. bar
  3119. .
  3120. <ul>
  3121. <li>foo</li>
  3122. </ul>
  3123. <p>bar</p>
  3124. ````````````````````````````````
  3125. This is not a significant restriction, because when a block begins
  3126. with 1-3 spaces indent, the indentation can always be removed without
  3127. a change in interpretation, allowing rule #1 to be applied. So, in
  3128. the above case:
  3129. ```````````````````````````````` example
  3130. - foo
  3131. bar
  3132. .
  3133. <ul>
  3134. <li>
  3135. <p>foo</p>
  3136. <p>bar</p>
  3137. </li>
  3138. </ul>
  3139. ````````````````````````````````
  3140. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3141. starting with a single [blank line] constitute a (possibly empty)
  3142. sequence of blocks *Bs*, not separated from each other by more than
  3143. one blank line, and *M* is a list marker of width *W*,
  3144. then the result of prepending *M* to the first line of *Ls*, and
  3145. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3146. item with *Bs* as its contents.
  3147. If a line is empty, then it need not be indented. The type of the
  3148. list item (bullet or ordered) is determined by the type of its list
  3149. marker. If the list item is ordered, then it is also assigned a
  3150. start number, based on the ordered list marker.
  3151. Here are some list items that start with a blank line but are not empty:
  3152. ```````````````````````````````` example
  3153. -
  3154. foo
  3155. -
  3156. ```
  3157. bar
  3158. ```
  3159. -
  3160. baz
  3161. .
  3162. <ul>
  3163. <li>foo</li>
  3164. <li>
  3165. <pre><code>bar
  3166. </code></pre>
  3167. </li>
  3168. <li>
  3169. <pre><code>baz
  3170. </code></pre>
  3171. </li>
  3172. </ul>
  3173. ````````````````````````````````
  3174. When the list item starts with a blank line, the number of spaces
  3175. following the list marker doesn't change the required indentation:
  3176. ```````````````````````````````` example
  3177. -
  3178. foo
  3179. .
  3180. <ul>
  3181. <li>foo</li>
  3182. </ul>
  3183. ````````````````````````````````
  3184. A list item can begin with at most one blank line.
  3185. In the following example, `foo` is not part of the list
  3186. item:
  3187. ```````````````````````````````` example
  3188. -
  3189. foo
  3190. .
  3191. <ul>
  3192. <li></li>
  3193. </ul>
  3194. <p>foo</p>
  3195. ````````````````````````````````
  3196. Here is an empty bullet list item:
  3197. ```````````````````````````````` example
  3198. - foo
  3199. -
  3200. - bar
  3201. .
  3202. <ul>
  3203. <li>foo</li>
  3204. <li></li>
  3205. <li>bar</li>
  3206. </ul>
  3207. ````````````````````````````````
  3208. It does not matter whether there are spaces following the [list marker]:
  3209. ```````````````````````````````` example
  3210. - foo
  3211. -
  3212. - bar
  3213. .
  3214. <ul>
  3215. <li>foo</li>
  3216. <li></li>
  3217. <li>bar</li>
  3218. </ul>
  3219. ````````````````````````````````
  3220. Here is an empty ordered list item:
  3221. ```````````````````````````````` example
  3222. 1. foo
  3223. 2.
  3224. 3. bar
  3225. .
  3226. <ol>
  3227. <li>foo</li>
  3228. <li></li>
  3229. <li>bar</li>
  3230. </ol>
  3231. ````````````````````````````````
  3232. A list may start or end with an empty list item:
  3233. ```````````````````````````````` example
  3234. *
  3235. .
  3236. <ul>
  3237. <li></li>
  3238. </ul>
  3239. ````````````````````````````````
  3240. However, an empty list item cannot interrupt a paragraph:
  3241. ```````````````````````````````` example
  3242. foo
  3243. *
  3244. foo
  3245. 1.
  3246. .
  3247. <p>foo
  3248. *</p>
  3249. <p>foo
  3250. 1.</p>
  3251. ````````````````````````````````
  3252. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3253. according to rule #1, #2, or #3, then the result of indenting each line
  3254. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3255. list item with the same contents and attributes. If a line is
  3256. empty, then it need not be indented.
  3257. Indented one space:
  3258. ```````````````````````````````` example
  3259. 1. A paragraph
  3260. with two lines.
  3261. indented code
  3262. > A block quote.
  3263. .
  3264. <ol>
  3265. <li>
  3266. <p>A paragraph
  3267. with two lines.</p>
  3268. <pre><code>indented code
  3269. </code></pre>
  3270. <blockquote>
  3271. <p>A block quote.</p>
  3272. </blockquote>
  3273. </li>
  3274. </ol>
  3275. ````````````````````````````````
  3276. Indented two spaces:
  3277. ```````````````````````````````` example
  3278. 1. A paragraph
  3279. with two lines.
  3280. indented code
  3281. > A block quote.
  3282. .
  3283. <ol>
  3284. <li>
  3285. <p>A paragraph
  3286. with two lines.</p>
  3287. <pre><code>indented code
  3288. </code></pre>
  3289. <blockquote>
  3290. <p>A block quote.</p>
  3291. </blockquote>
  3292. </li>
  3293. </ol>
  3294. ````````````````````````````````
  3295. Indented three spaces:
  3296. ```````````````````````````````` example
  3297. 1. A paragraph
  3298. with two lines.
  3299. indented code
  3300. > A block quote.
  3301. .
  3302. <ol>
  3303. <li>
  3304. <p>A paragraph
  3305. with two lines.</p>
  3306. <pre><code>indented code
  3307. </code></pre>
  3308. <blockquote>
  3309. <p>A block quote.</p>
  3310. </blockquote>
  3311. </li>
  3312. </ol>
  3313. ````````````````````````````````
  3314. Four spaces indent gives a code block:
  3315. ```````````````````````````````` example
  3316. 1. A paragraph
  3317. with two lines.
  3318. indented code
  3319. > A block quote.
  3320. .
  3321. <pre><code>1. A paragraph
  3322. with two lines.
  3323. indented code
  3324. &gt; A block quote.
  3325. </code></pre>
  3326. ````````````````````````````````
  3327. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3328. item](#list-items) with contents *Bs*, then the result of deleting
  3329. some or all of the indentation from one or more lines in which the
  3330. next [non-whitespace character] after the indentation is
  3331. [paragraph continuation text] is a
  3332. list item with the same contents and attributes. The unindented
  3333. lines are called
  3334. [lazy continuation line](@)s.
  3335. Here is an example with [lazy continuation lines]:
  3336. ```````````````````````````````` example
  3337. 1. A paragraph
  3338. with two lines.
  3339. indented code
  3340. > A block quote.
  3341. .
  3342. <ol>
  3343. <li>
  3344. <p>A paragraph
  3345. with two lines.</p>
  3346. <pre><code>indented code
  3347. </code></pre>
  3348. <blockquote>
  3349. <p>A block quote.</p>
  3350. </blockquote>
  3351. </li>
  3352. </ol>
  3353. ````````````````````````````````
  3354. Indentation can be partially deleted:
  3355. ```````````````````````````````` example
  3356. 1. A paragraph
  3357. with two lines.
  3358. .
  3359. <ol>
  3360. <li>A paragraph
  3361. with two lines.</li>
  3362. </ol>
  3363. ````````````````````````````````
  3364. These examples show how laziness can work in nested structures:
  3365. ```````````````````````````````` example
  3366. > 1. > Blockquote
  3367. continued here.
  3368. .
  3369. <blockquote>
  3370. <ol>
  3371. <li>
  3372. <blockquote>
  3373. <p>Blockquote
  3374. continued here.</p>
  3375. </blockquote>
  3376. </li>
  3377. </ol>
  3378. </blockquote>
  3379. ````````````````````````````````
  3380. ```````````````````````````````` example
  3381. > 1. > Blockquote
  3382. > continued here.
  3383. .
  3384. <blockquote>
  3385. <ol>
  3386. <li>
  3387. <blockquote>
  3388. <p>Blockquote
  3389. continued here.</p>
  3390. </blockquote>
  3391. </li>
  3392. </ol>
  3393. </blockquote>
  3394. ````````````````````````````````
  3395. 6. **That's all.** Nothing that is not counted as a list item by rules
  3396. #1--5 counts as a [list item](#list-items).
  3397. The rules for sublists follow from the general rules above. A sublist
  3398. must be indented the same number of spaces a paragraph would need to be
  3399. in order to be included in the list item.
  3400. So, in this case we need two spaces indent:
  3401. ```````````````````````````````` example
  3402. - foo
  3403. - bar
  3404. - baz
  3405. - boo
  3406. .
  3407. <ul>
  3408. <li>foo
  3409. <ul>
  3410. <li>bar
  3411. <ul>
  3412. <li>baz
  3413. <ul>
  3414. <li>boo</li>
  3415. </ul>
  3416. </li>
  3417. </ul>
  3418. </li>
  3419. </ul>
  3420. </li>
  3421. </ul>
  3422. ````````````````````````````````
  3423. One is not enough:
  3424. ```````````````````````````````` example
  3425. - foo
  3426. - bar
  3427. - baz
  3428. - boo
  3429. .
  3430. <ul>
  3431. <li>foo</li>
  3432. <li>bar</li>
  3433. <li>baz</li>
  3434. <li>boo</li>
  3435. </ul>
  3436. ````````````````````````````````
  3437. Here we need four, because the list marker is wider:
  3438. ```````````````````````````````` example
  3439. 10) foo
  3440. - bar
  3441. .
  3442. <ol start="10">
  3443. <li>foo
  3444. <ul>
  3445. <li>bar</li>
  3446. </ul>
  3447. </li>
  3448. </ol>
  3449. ````````````````````````````````
  3450. Three is not enough:
  3451. ```````````````````````````````` example
  3452. 10) foo
  3453. - bar
  3454. .
  3455. <ol start="10">
  3456. <li>foo</li>
  3457. </ol>
  3458. <ul>
  3459. <li>bar</li>
  3460. </ul>
  3461. ````````````````````````````````
  3462. A list may be the first block in a list item:
  3463. ```````````````````````````````` example
  3464. - - foo
  3465. .
  3466. <ul>
  3467. <li>
  3468. <ul>
  3469. <li>foo</li>
  3470. </ul>
  3471. </li>
  3472. </ul>
  3473. ````````````````````````````````
  3474. ```````````````````````````````` example
  3475. 1. - 2. foo
  3476. .
  3477. <ol>
  3478. <li>
  3479. <ul>
  3480. <li>
  3481. <ol start="2">
  3482. <li>foo</li>
  3483. </ol>
  3484. </li>
  3485. </ul>
  3486. </li>
  3487. </ol>
  3488. ````````````````````````````````
  3489. A list item can contain a heading:
  3490. ```````````````````````````````` example
  3491. - # Foo
  3492. - Bar
  3493. ---
  3494. baz
  3495. .
  3496. <ul>
  3497. <li>
  3498. <h1>Foo</h1>
  3499. </li>
  3500. <li>
  3501. <h2>Bar</h2>
  3502. baz</li>
  3503. </ul>
  3504. ````````````````````````````````
  3505. ### Motivation
  3506. John Gruber's Markdown spec says the following about list items:
  3507. 1. "List markers typically start at the left margin, but may be indented
  3508. by up to three spaces. List markers must be followed by one or more
  3509. spaces or a tab."
  3510. 2. "To make lists look nice, you can wrap items with hanging indents....
  3511. But if you don't want to, you don't have to."
  3512. 3. "List items may consist of multiple paragraphs. Each subsequent
  3513. paragraph in a list item must be indented by either 4 spaces or one
  3514. tab."
  3515. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3516. but here again, Markdown will allow you to be lazy."
  3517. 5. "To put a blockquote within a list item, the blockquote's `>`
  3518. delimiters need to be indented."
  3519. 6. "To put a code block within a list item, the code block needs to be
  3520. indented twice — 8 spaces or two tabs."
  3521. These rules specify that a paragraph under a list item must be indented
  3522. four spaces (presumably, from the left margin, rather than the start of
  3523. the list marker, but this is not said), and that code under a list item
  3524. must be indented eight spaces instead of the usual four. They also say
  3525. that a block quote must be indented, but not by how much; however, the
  3526. example given has four spaces indentation. Although nothing is said
  3527. about other kinds of block-level content, it is certainly reasonable to
  3528. infer that *all* block elements under a list item, including other
  3529. lists, must be indented four spaces. This principle has been called the
  3530. *four-space rule*.
  3531. The four-space rule is clear and principled, and if the reference
  3532. implementation `Markdown.pl` had followed it, it probably would have
  3533. become the standard. However, `Markdown.pl` allowed paragraphs and
  3534. sublists to start with only two spaces indentation, at least on the
  3535. outer level. Worse, its behavior was inconsistent: a sublist of an
  3536. outer-level list needed two spaces indentation, but a sublist of this
  3537. sublist needed three spaces. It is not surprising, then, that different
  3538. implementations of Markdown have developed very different rules for
  3539. determining what comes under a list item. (Pandoc and python-Markdown,
  3540. for example, stuck with Gruber's syntax description and the four-space
  3541. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3542. followed `Markdown.pl`'s behavior more closely.)
  3543. Unfortunately, given the divergences between implementations, there
  3544. is no way to give a spec for list items that will be guaranteed not
  3545. to break any existing documents. However, the spec given here should
  3546. correctly handle lists formatted with either the four-space rule or
  3547. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3548. in a way that is natural for a human to read.
  3549. The strategy here is to let the width and indentation of the list marker
  3550. determine the indentation necessary for blocks to fall under the list
  3551. item, rather than having a fixed and arbitrary number. The writer can
  3552. think of the body of the list item as a unit which gets indented to the
  3553. right enough to fit the list marker (and any indentation on the list
  3554. marker). (The laziness rule, #5, then allows continuation lines to be
  3555. unindented if needed.)
  3556. This rule is superior, we claim, to any rule requiring a fixed level of
  3557. indentation from the margin. The four-space rule is clear but
  3558. unnatural. It is quite unintuitive that
  3559. ``` markdown
  3560. - foo
  3561. bar
  3562. - baz
  3563. ```
  3564. should be parsed as two lists with an intervening paragraph,
  3565. ``` html
  3566. <ul>
  3567. <li>foo</li>
  3568. </ul>
  3569. <p>bar</p>
  3570. <ul>
  3571. <li>baz</li>
  3572. </ul>
  3573. ```
  3574. as the four-space rule demands, rather than a single list,
  3575. ``` html
  3576. <ul>
  3577. <li>
  3578. <p>foo</p>
  3579. <p>bar</p>
  3580. <ul>
  3581. <li>baz</li>
  3582. </ul>
  3583. </li>
  3584. </ul>
  3585. ```
  3586. The choice of four spaces is arbitrary. It can be learned, but it is
  3587. not likely to be guessed, and it trips up beginners regularly.
  3588. Would it help to adopt a two-space rule? The problem is that such
  3589. a rule, together with the rule allowing 1--3 spaces indentation of the
  3590. initial list marker, allows text that is indented *less than* the
  3591. original list marker to be included in the list item. For example,
  3592. `Markdown.pl` parses
  3593. ``` markdown
  3594. - one
  3595. two
  3596. ```
  3597. as a single list item, with `two` a continuation paragraph:
  3598. ``` html
  3599. <ul>
  3600. <li>
  3601. <p>one</p>
  3602. <p>two</p>
  3603. </li>
  3604. </ul>
  3605. ```
  3606. and similarly
  3607. ``` markdown
  3608. > - one
  3609. >
  3610. > two
  3611. ```
  3612. as
  3613. ``` html
  3614. <blockquote>
  3615. <ul>
  3616. <li>
  3617. <p>one</p>
  3618. <p>two</p>
  3619. </li>
  3620. </ul>
  3621. </blockquote>
  3622. ```
  3623. This is extremely unintuitive.
  3624. Rather than requiring a fixed indent from the margin, we could require
  3625. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3626. may itself be indented). This proposal would remove the last anomaly
  3627. discussed. Unlike the spec presented above, it would count the following
  3628. as a list item with a subparagraph, even though the paragraph `bar`
  3629. is not indented as far as the first paragraph `foo`:
  3630. ``` markdown
  3631. 10. foo
  3632. bar
  3633. ```
  3634. Arguably this text does read like a list item with `bar` as a subparagraph,
  3635. which may count in favor of the proposal. However, on this proposal indented
  3636. code would have to be indented six spaces after the list marker. And this
  3637. would break a lot of existing Markdown, which has the pattern:
  3638. ``` markdown
  3639. 1. foo
  3640. indented code
  3641. ```
  3642. where the code is indented eight spaces. The spec above, by contrast, will
  3643. parse this text as expected, since the code block's indentation is measured
  3644. from the beginning of `foo`.
  3645. The one case that needs special treatment is a list item that *starts*
  3646. with indented code. How much indentation is required in that case, since
  3647. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3648. that in such cases, we require one space indentation from the list marker
  3649. (and then the normal four spaces for the indented code). This will match the
  3650. four-space rule in cases where the list marker plus its initial indentation
  3651. takes four spaces (a common case), but diverge in other cases.
  3652. ## Lists
  3653. A [list](@) is a sequence of one or more
  3654. list items [of the same type]. The list items
  3655. may be separated by any number of blank lines.
  3656. Two list items are [of the same type](@)
  3657. if they begin with a [list marker] of the same type.
  3658. Two list markers are of the
  3659. same type if (a) they are bullet list markers using the same character
  3660. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3661. delimiter (either `.` or `)`).
  3662. A list is an [ordered list](@)
  3663. if its constituent list items begin with
  3664. [ordered list markers], and a
  3665. [bullet list](@) if its constituent list
  3666. items begin with [bullet list markers].
  3667. The [start number](@)
  3668. of an [ordered list] is determined by the list number of
  3669. its initial list item. The numbers of subsequent list items are
  3670. disregarded.
  3671. A list is [loose](@) if any of its constituent
  3672. list items are separated by blank lines, or if any of its constituent
  3673. list items directly contain two block-level elements with a blank line
  3674. between them. Otherwise a list is [tight](@).
  3675. (The difference in HTML output is that paragraphs in a loose list are
  3676. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3677. Changing the bullet or ordered list delimiter starts a new list:
  3678. ```````````````````````````````` example
  3679. - foo
  3680. - bar
  3681. + baz
  3682. .
  3683. <ul>
  3684. <li>foo</li>
  3685. <li>bar</li>
  3686. </ul>
  3687. <ul>
  3688. <li>baz</li>
  3689. </ul>
  3690. ````````````````````````````````
  3691. ```````````````````````````````` example
  3692. 1. foo
  3693. 2. bar
  3694. 3) baz
  3695. .
  3696. <ol>
  3697. <li>foo</li>
  3698. <li>bar</li>
  3699. </ol>
  3700. <ol start="3">
  3701. <li>baz</li>
  3702. </ol>
  3703. ````````````````````````````````
  3704. In CommonMark, a list can interrupt a paragraph. That is,
  3705. no blank line is needed to separate a paragraph from a following
  3706. list:
  3707. ```````````````````````````````` example
  3708. Foo
  3709. - bar
  3710. - baz
  3711. .
  3712. <p>Foo</p>
  3713. <ul>
  3714. <li>bar</li>
  3715. <li>baz</li>
  3716. </ul>
  3717. ````````````````````````````````
  3718. `Markdown.pl` does not allow this, through fear of triggering a list
  3719. via a numeral in a hard-wrapped line:
  3720. ``` markdown
  3721. The number of windows in my house is
  3722. 14. The number of doors is 6.
  3723. ```
  3724. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3725. interrupt a paragraph, even though the same considerations might
  3726. apply.
  3727. In CommonMark, we do allow lists to interrupt paragraphs, for
  3728. two reasons. First, it is natural and not uncommon for people
  3729. to start lists without blank lines:
  3730. ``` markdown
  3731. I need to buy
  3732. - new shoes
  3733. - a coat
  3734. - a plane ticket
  3735. ```
  3736. Second, we are attracted to a
  3737. > [principle of uniformity](@):
  3738. > if a chunk of text has a certain
  3739. > meaning, it will continue to have the same meaning when put into a
  3740. > container block (such as a list item or blockquote).
  3741. (Indeed, the spec for [list items] and [block quotes] presupposes
  3742. this principle.) This principle implies that if
  3743. ``` markdown
  3744. * I need to buy
  3745. - new shoes
  3746. - a coat
  3747. - a plane ticket
  3748. ```
  3749. is a list item containing a paragraph followed by a nested sublist,
  3750. as all Markdown implementations agree it is (though the paragraph
  3751. may be rendered without `<p>` tags, since the list is "tight"),
  3752. then
  3753. ``` markdown
  3754. I need to buy
  3755. - new shoes
  3756. - a coat
  3757. - a plane ticket
  3758. ```
  3759. by itself should be a paragraph followed by a nested sublist.
  3760. Since it is well established Markdown practice to allow lists to
  3761. interrupt paragraphs inside list items, the [principle of
  3762. uniformity] requires us to allow this outside list items as
  3763. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3764. takes a different approach, requiring blank lines before lists
  3765. even inside other list items.)
  3766. In order to solve of unwanted lists in paragraphs with
  3767. hard-wrapped numerals, we allow only lists starting with `1` to
  3768. interrupt paragraphs. Thus,
  3769. ```````````````````````````````` example
  3770. The number of windows in my house is
  3771. 14. The number of doors is 6.
  3772. .
  3773. <p>The number of windows in my house is
  3774. 14. The number of doors is 6.</p>
  3775. ````````````````````````````````
  3776. We may still get an unintended result in cases like
  3777. ```````````````````````````````` example
  3778. The number of windows in my house is
  3779. 1. The number of doors is 6.
  3780. .
  3781. <p>The number of windows in my house is</p>
  3782. <ol>
  3783. <li>The number of doors is 6.</li>
  3784. </ol>
  3785. ````````````````````````````````
  3786. but this rule should prevent most spurious list captures.
  3787. There can be any number of blank lines between items:
  3788. ```````````````````````````````` example
  3789. - foo
  3790. - bar
  3791. - baz
  3792. .
  3793. <ul>
  3794. <li>
  3795. <p>foo</p>
  3796. </li>
  3797. <li>
  3798. <p>bar</p>
  3799. </li>
  3800. <li>
  3801. <p>baz</p>
  3802. </li>
  3803. </ul>
  3804. ````````````````````````````````
  3805. ```````````````````````````````` example
  3806. - foo
  3807. - bar
  3808. - baz
  3809. bim
  3810. .
  3811. <ul>
  3812. <li>foo
  3813. <ul>
  3814. <li>bar
  3815. <ul>
  3816. <li>
  3817. <p>baz</p>
  3818. <p>bim</p>
  3819. </li>
  3820. </ul>
  3821. </li>
  3822. </ul>
  3823. </li>
  3824. </ul>
  3825. ````````````````````````````````
  3826. To separate consecutive lists of the same type, or to separate a
  3827. list from an indented code block that would otherwise be parsed
  3828. as a subparagraph of the final list item, you can insert a blank HTML
  3829. comment:
  3830. ```````````````````````````````` example
  3831. - foo
  3832. - bar
  3833. <!-- -->
  3834. - baz
  3835. - bim
  3836. .
  3837. <ul>
  3838. <li>foo</li>
  3839. <li>bar</li>
  3840. </ul>
  3841. <!-- -->
  3842. <ul>
  3843. <li>baz</li>
  3844. <li>bim</li>
  3845. </ul>
  3846. ````````````````````````````````
  3847. ```````````````````````````````` example
  3848. - foo
  3849. notcode
  3850. - foo
  3851. <!-- -->
  3852. code
  3853. .
  3854. <ul>
  3855. <li>
  3856. <p>foo</p>
  3857. <p>notcode</p>
  3858. </li>
  3859. <li>
  3860. <p>foo</p>
  3861. </li>
  3862. </ul>
  3863. <!-- -->
  3864. <pre><code>code
  3865. </code></pre>
  3866. ````````````````````````````````
  3867. List items need not be indented to the same level. The following
  3868. list items will be treated as items at the same list level,
  3869. since none is indented enough to belong to the previous list
  3870. item:
  3871. ```````````````````````````````` example
  3872. - a
  3873. - b
  3874. - c
  3875. - d
  3876. - e
  3877. - f
  3878. - g
  3879. - h
  3880. - i
  3881. .
  3882. <ul>
  3883. <li>a</li>
  3884. <li>b</li>
  3885. <li>c</li>
  3886. <li>d</li>
  3887. <li>e</li>
  3888. <li>f</li>
  3889. <li>g</li>
  3890. <li>h</li>
  3891. <li>i</li>
  3892. </ul>
  3893. ````````````````````````````````
  3894. ```````````````````````````````` example
  3895. 1. a
  3896. 2. b
  3897. 3. c
  3898. .
  3899. <ol>
  3900. <li>
  3901. <p>a</p>
  3902. </li>
  3903. <li>
  3904. <p>b</p>
  3905. </li>
  3906. <li>
  3907. <p>c</p>
  3908. </li>
  3909. </ol>
  3910. ````````````````````````````````
  3911. This is a loose list, because there is a blank line between
  3912. two of the list items:
  3913. ```````````````````````````````` example
  3914. - a
  3915. - b
  3916. - c
  3917. .
  3918. <ul>
  3919. <li>
  3920. <p>a</p>
  3921. </li>
  3922. <li>
  3923. <p>b</p>
  3924. </li>
  3925. <li>
  3926. <p>c</p>
  3927. </li>
  3928. </ul>
  3929. ````````````````````````````````
  3930. So is this, with a empty second item:
  3931. ```````````````````````````````` example
  3932. * a
  3933. *
  3934. * c
  3935. .
  3936. <ul>
  3937. <li>
  3938. <p>a</p>
  3939. </li>
  3940. <li></li>
  3941. <li>
  3942. <p>c</p>
  3943. </li>
  3944. </ul>
  3945. ````````````````````````````````
  3946. These are loose lists, even though there is no space between the items,
  3947. because one of the items directly contains two block-level elements
  3948. with a blank line between them:
  3949. ```````````````````````````````` example
  3950. - a
  3951. - b
  3952. c
  3953. - d
  3954. .
  3955. <ul>
  3956. <li>
  3957. <p>a</p>
  3958. </li>
  3959. <li>
  3960. <p>b</p>
  3961. <p>c</p>
  3962. </li>
  3963. <li>
  3964. <p>d</p>
  3965. </li>
  3966. </ul>
  3967. ````````````````````````````````
  3968. ```````````````````````````````` example
  3969. - a
  3970. - b
  3971. [ref]: /url
  3972. - d
  3973. .
  3974. <ul>
  3975. <li>
  3976. <p>a</p>
  3977. </li>
  3978. <li>
  3979. <p>b</p>
  3980. </li>
  3981. <li>
  3982. <p>d</p>
  3983. </li>
  3984. </ul>
  3985. ````````````````````````````````
  3986. This is a tight list, because the blank lines are in a code block:
  3987. ```````````````````````````````` example
  3988. - a
  3989. - ```
  3990. b
  3991. ```
  3992. - c
  3993. .
  3994. <ul>
  3995. <li>a</li>
  3996. <li>
  3997. <pre><code>b
  3998. </code></pre>
  3999. </li>
  4000. <li>c</li>
  4001. </ul>
  4002. ````````````````````````````````
  4003. This is a tight list, because the blank line is between two
  4004. paragraphs of a sublist. So the sublist is loose while
  4005. the outer list is tight:
  4006. ```````````````````````````````` example
  4007. - a
  4008. - b
  4009. c
  4010. - d
  4011. .
  4012. <ul>
  4013. <li>a
  4014. <ul>
  4015. <li>
  4016. <p>b</p>
  4017. <p>c</p>
  4018. </li>
  4019. </ul>
  4020. </li>
  4021. <li>d</li>
  4022. </ul>
  4023. ````````````````````````````````
  4024. This is a tight list, because the blank line is inside the
  4025. block quote:
  4026. ```````````````````````````````` example
  4027. * a
  4028. > b
  4029. >
  4030. * c
  4031. .
  4032. <ul>
  4033. <li>a
  4034. <blockquote>
  4035. <p>b</p>
  4036. </blockquote>
  4037. </li>
  4038. <li>c</li>
  4039. </ul>
  4040. ````````````````````````````````
  4041. This list is tight, because the consecutive block elements
  4042. are not separated by blank lines:
  4043. ```````````````````````````````` example
  4044. - a
  4045. > b
  4046. ```
  4047. c
  4048. ```
  4049. - d
  4050. .
  4051. <ul>
  4052. <li>a
  4053. <blockquote>
  4054. <p>b</p>
  4055. </blockquote>
  4056. <pre><code>c
  4057. </code></pre>
  4058. </li>
  4059. <li>d</li>
  4060. </ul>
  4061. ````````````````````````````````
  4062. A single-paragraph list is tight:
  4063. ```````````````````````````````` example
  4064. - a
  4065. .
  4066. <ul>
  4067. <li>a</li>
  4068. </ul>
  4069. ````````````````````````````````
  4070. ```````````````````````````````` example
  4071. - a
  4072. - b
  4073. .
  4074. <ul>
  4075. <li>a
  4076. <ul>
  4077. <li>b</li>
  4078. </ul>
  4079. </li>
  4080. </ul>
  4081. ````````````````````````````````
  4082. This list is loose, because of the blank line between the
  4083. two block elements in the list item:
  4084. ```````````````````````````````` example
  4085. 1. ```
  4086. foo
  4087. ```
  4088. bar
  4089. .
  4090. <ol>
  4091. <li>
  4092. <pre><code>foo
  4093. </code></pre>
  4094. <p>bar</p>
  4095. </li>
  4096. </ol>
  4097. ````````````````````````````````
  4098. Here the outer list is loose, the inner list tight:
  4099. ```````````````````````````````` example
  4100. * foo
  4101. * bar
  4102. baz
  4103. .
  4104. <ul>
  4105. <li>
  4106. <p>foo</p>
  4107. <ul>
  4108. <li>bar</li>
  4109. </ul>
  4110. <p>baz</p>
  4111. </li>
  4112. </ul>
  4113. ````````````````````````````````
  4114. ```````````````````````````````` example
  4115. - a
  4116. - b
  4117. - c
  4118. - d
  4119. - e
  4120. - f
  4121. .
  4122. <ul>
  4123. <li>
  4124. <p>a</p>
  4125. <ul>
  4126. <li>b</li>
  4127. <li>c</li>
  4128. </ul>
  4129. </li>
  4130. <li>
  4131. <p>d</p>
  4132. <ul>
  4133. <li>e</li>
  4134. <li>f</li>
  4135. </ul>
  4136. </li>
  4137. </ul>
  4138. ````````````````````````````````
  4139. # Inlines
  4140. Inlines are parsed sequentially from the beginning of the character
  4141. stream to the end (left to right, in left-to-right languages).
  4142. Thus, for example, in
  4143. ```````````````````````````````` example
  4144. `hi`lo`
  4145. .
  4146. <p><code>hi</code>lo`</p>
  4147. ````````````````````````````````
  4148. `hi` is parsed as code, leaving the backtick at the end as a literal
  4149. backtick.
  4150. ## Backslash escapes
  4151. Any ASCII punctuation character may be backslash-escaped:
  4152. ```````````````````````````````` example
  4153. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4154. .
  4155. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4156. ````````````````````````````````
  4157. Backslashes before other characters are treated as literal
  4158. backslashes:
  4159. ```````````````````````````````` example
  4160. \→\A\a\ \3\φ\«
  4161. .
  4162. <p>\→\A\a\ \3\φ\«</p>
  4163. ````````````````````````````````
  4164. Escaped characters are treated as regular characters and do
  4165. not have their usual Markdown meanings:
  4166. ```````````````````````````````` example
  4167. \*not emphasized*
  4168. \<br/> not a tag
  4169. \[not a link](/foo)
  4170. \`not code`
  4171. 1\. not a list
  4172. \* not a list
  4173. \# not a heading
  4174. \[foo]: /url "not a reference"
  4175. .
  4176. <p>*not emphasized*
  4177. &lt;br/&gt; not a tag
  4178. [not a link](/foo)
  4179. `not code`
  4180. 1. not a list
  4181. * not a list
  4182. # not a heading
  4183. [foo]: /url &quot;not a reference&quot;</p>
  4184. ````````````````````````````````
  4185. If a backslash is itself escaped, the following character is not:
  4186. ```````````````````````````````` example
  4187. \\*emphasis*
  4188. .
  4189. <p>\<em>emphasis</em></p>
  4190. ````````````````````````````````
  4191. A backslash at the end of the line is a [hard line break]:
  4192. ```````````````````````````````` example
  4193. foo\
  4194. bar
  4195. .
  4196. <p>foo<br />
  4197. bar</p>
  4198. ````````````````````````````````
  4199. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4200. raw HTML:
  4201. ```````````````````````````````` example
  4202. `` \[\` ``
  4203. .
  4204. <p><code>\[\`</code></p>
  4205. ````````````````````````````````
  4206. ```````````````````````````````` example
  4207. \[\]
  4208. .
  4209. <pre><code>\[\]
  4210. </code></pre>
  4211. ````````````````````````````````
  4212. ```````````````````````````````` example
  4213. ~~~
  4214. \[\]
  4215. ~~~
  4216. .
  4217. <pre><code>\[\]
  4218. </code></pre>
  4219. ````````````````````````````````
  4220. ```````````````````````````````` example
  4221. <http://example.com?find=\*>
  4222. .
  4223. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4224. ````````````````````````````````
  4225. ```````````````````````````````` example
  4226. <a href="/bar\/)">
  4227. .
  4228. <a href="/bar\/)">
  4229. ````````````````````````````````
  4230. But they work in all other contexts, including URLs and link titles,
  4231. link references, and [info strings] in [fenced code blocks]:
  4232. ```````````````````````````````` example
  4233. [foo](/bar\* "ti\*tle")
  4234. .
  4235. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4236. ````````````````````````````````
  4237. ```````````````````````````````` example
  4238. [foo]
  4239. [foo]: /bar\* "ti\*tle"
  4240. .
  4241. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4242. ````````````````````````````````
  4243. ```````````````````````````````` example
  4244. ``` foo\+bar
  4245. foo
  4246. ```
  4247. .
  4248. <pre><code class="language-foo+bar">foo
  4249. </code></pre>
  4250. ````````````````````````````````
  4251. ## Entity and numeric character references
  4252. All valid HTML entity references and numeric character
  4253. references, except those occuring in code blocks and code spans,
  4254. are recognized as such and treated as equivalent to the
  4255. corresponding Unicode characters. Conforming CommonMark parsers
  4256. need not store information about whether a particular character
  4257. was represented in the source using a Unicode character or
  4258. an entity reference.
  4259. [Entity references](@) consist of `&` + any of the valid
  4260. HTML5 entity names + `;`. The
  4261. document <https://html.spec.whatwg.org/multipage/entities.json>
  4262. is used as an authoritative source for the valid entity
  4263. references and their corresponding code points.
  4264. ```````````````````````````````` example
  4265. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4266. &frac34; &HilbertSpace; &DifferentialD;
  4267. &ClockwiseContourIntegral; &ngE;
  4268. .
  4269. <p>  &amp; © Æ Ď
  4270. ¾ ℋ ⅆ
  4271. ∲ ≧̸</p>
  4272. ````````````````````````````````
  4273. [Decimal numeric character
  4274. references](@)
  4275. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4276. numeric character reference is parsed as the corresponding
  4277. Unicode character. Invalid Unicode code points will be replaced by
  4278. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4279. the code point `U+0000` will also be replaced by `U+FFFD`.
  4280. ```````````````````````````````` example
  4281. &#35; &#1234; &#992; &#98765432; &#0;
  4282. .
  4283. <p># Ӓ Ϡ � �</p>
  4284. ````````````````````````````````
  4285. [Hexadecimal numeric character
  4286. references](@) consist of `&#` +
  4287. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4288. They too are parsed as the corresponding Unicode character (this
  4289. time specified with a hexadecimal numeral instead of decimal).
  4290. ```````````````````````````````` example
  4291. &#X22; &#XD06; &#xcab;
  4292. .
  4293. <p>&quot; ആ ಫ</p>
  4294. ````````````````````````````````
  4295. Here are some nonentities:
  4296. ```````````````````````````````` example
  4297. &nbsp &x; &#; &#x;
  4298. &ThisIsNotDefined; &hi?;
  4299. .
  4300. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4301. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4302. ````````````````````````````````
  4303. Although HTML5 does accept some entity references
  4304. without a trailing semicolon (such as `&copy`), these are not
  4305. recognized here, because it makes the grammar too ambiguous:
  4306. ```````````````````````````````` example
  4307. &copy
  4308. .
  4309. <p>&amp;copy</p>
  4310. ````````````````````````````````
  4311. Strings that are not on the list of HTML5 named entities are not
  4312. recognized as entity references either:
  4313. ```````````````````````````````` example
  4314. &MadeUpEntity;
  4315. .
  4316. <p>&amp;MadeUpEntity;</p>
  4317. ````````````````````````````````
  4318. Entity and numeric character references are recognized in any
  4319. context besides code spans or code blocks, including
  4320. URLs, [link titles], and [fenced code block][] [info strings]:
  4321. ```````````````````````````````` example
  4322. <a href="&ouml;&ouml;.html">
  4323. .
  4324. <a href="&ouml;&ouml;.html">
  4325. ````````````````````````````````
  4326. ```````````````````````````````` example
  4327. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4328. .
  4329. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4330. ````````````````````````````````
  4331. ```````````````````````````````` example
  4332. [foo]
  4333. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4334. .
  4335. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4336. ````````````````````````````````
  4337. ```````````````````````````````` example
  4338. ``` f&ouml;&ouml;
  4339. foo
  4340. ```
  4341. .
  4342. <pre><code class="language-föö">foo
  4343. </code></pre>
  4344. ````````````````````````````````
  4345. Entity and numeric character references are treated as literal
  4346. text in code spans and code blocks:
  4347. ```````````````````````````````` example
  4348. `f&ouml;&ouml;`
  4349. .
  4350. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4351. ````````````````````````````````
  4352. ```````````````````````````````` example
  4353. f&ouml;f&ouml;
  4354. .
  4355. <pre><code>f&amp;ouml;f&amp;ouml;
  4356. </code></pre>
  4357. ````````````````````````````````
  4358. ## Code spans
  4359. A [backtick string](@)
  4360. is a string of one or more backtick characters (`` ` ``) that is neither
  4361. preceded nor followed by a backtick.
  4362. A [code span](@) begins with a backtick string and ends with
  4363. a backtick string of equal length. The contents of the code span are
  4364. the characters between the two backtick strings, with leading and
  4365. trailing spaces and [line endings] removed, and
  4366. [whitespace] collapsed to single spaces.
  4367. This is a simple code span:
  4368. ```````````````````````````````` example
  4369. `foo`
  4370. .
  4371. <p><code>foo</code></p>
  4372. ````````````````````````````````
  4373. Here two backticks are used, because the code contains a backtick.
  4374. This example also illustrates stripping of leading and trailing spaces:
  4375. ```````````````````````````````` example
  4376. `` foo ` bar ``
  4377. .
  4378. <p><code>foo ` bar</code></p>
  4379. ````````````````````````````````
  4380. This example shows the motivation for stripping leading and trailing
  4381. spaces:
  4382. ```````````````````````````````` example
  4383. ` `` `
  4384. .
  4385. <p><code>``</code></p>
  4386. ````````````````````````````````
  4387. [Line endings] are treated like spaces:
  4388. ```````````````````````````````` example
  4389. ``
  4390. foo
  4391. ``
  4392. .
  4393. <p><code>foo</code></p>
  4394. ````````````````````````````````
  4395. Interior spaces and [line endings] are collapsed into
  4396. single spaces, just as they would be by a browser:
  4397. ```````````````````````````````` example
  4398. `foo bar
  4399. baz`
  4400. .
  4401. <p><code>foo bar baz</code></p>
  4402. ````````````````````````````````
  4403. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4404. collapsed, however:
  4405. ```````````````````````````````` example
  4406. `a  b`
  4407. .
  4408. <p><code>a  b</code></p>
  4409. ````````````````````````````````
  4410. Q: Why not just leave the spaces, since browsers will collapse them
  4411. anyway? A: Because we might be targeting a non-HTML format, and we
  4412. shouldn't rely on HTML-specific rendering assumptions.
  4413. (Existing implementations differ in their treatment of internal
  4414. spaces and [line endings]. Some, including `Markdown.pl` and
  4415. `showdown`, convert an internal [line ending] into a
  4416. `<br />` tag. But this makes things difficult for those who like to
  4417. hard-wrap their paragraphs, since a line break in the midst of a code
  4418. span will cause an unintended line break in the output. Others just
  4419. leave internal spaces as they are, which is fine if only HTML is being
  4420. targeted.)
  4421. ```````````````````````````````` example
  4422. `foo `` bar`
  4423. .
  4424. <p><code>foo `` bar</code></p>
  4425. ````````````````````````````````
  4426. Note that backslash escapes do not work in code spans. All backslashes
  4427. are treated literally:
  4428. ```````````````````````````````` example
  4429. `foo\`bar`
  4430. .
  4431. <p><code>foo\</code>bar`</p>
  4432. ````````````````````````````````
  4433. Backslash escapes are never needed, because one can always choose a
  4434. string of *n* backtick characters as delimiters, where the code does
  4435. not contain any strings of exactly *n* backtick characters.
  4436. Code span backticks have higher precedence than any other inline
  4437. constructs except HTML tags and autolinks. Thus, for example, this is
  4438. not parsed as emphasized text, since the second `*` is part of a code
  4439. span:
  4440. ```````````````````````````````` example
  4441. *foo`*`
  4442. .
  4443. <p>*foo<code>*</code></p>
  4444. ````````````````````````````````
  4445. And this is not parsed as a link:
  4446. ```````````````````````````````` example
  4447. [not a `link](/foo`)
  4448. .
  4449. <p>[not a <code>link](/foo</code>)</p>
  4450. ````````````````````````````````
  4451. Code spans, HTML tags, and autolinks have the same precedence.
  4452. Thus, this is code:
  4453. ```````````````````````````````` example
  4454. `<a href="`">`
  4455. .
  4456. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4457. ````````````````````````````````
  4458. But this is an HTML tag:
  4459. ```````````````````````````````` example
  4460. <a href="`">`
  4461. .
  4462. <p><a href="`">`</p>
  4463. ````````````````````````````````
  4464. And this is code:
  4465. ```````````````````````````````` example
  4466. `<http://foo.bar.`baz>`
  4467. .
  4468. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4469. ````````````````````````````````
  4470. But this is an autolink:
  4471. ```````````````````````````````` example
  4472. <http://foo.bar.`baz>`
  4473. .
  4474. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4475. ````````````````````````````````
  4476. When a backtick string is not closed by a matching backtick string,
  4477. we just have literal backticks:
  4478. ```````````````````````````````` example
  4479. ```foo``
  4480. .
  4481. <p>```foo``</p>
  4482. ````````````````````````````````
  4483. ```````````````````````````````` example
  4484. `foo
  4485. .
  4486. <p>`foo</p>
  4487. ````````````````````````````````
  4488. The following case also illustrates the need for opening and
  4489. closing backtick strings to be equal in length:
  4490. ```````````````````````````````` example
  4491. `foo``bar``
  4492. .
  4493. <p>`foo<code>bar</code></p>
  4494. ````````````````````````````````
  4495. ## Emphasis and strong emphasis
  4496. John Gruber's original [Markdown syntax
  4497. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4498. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4499. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4500. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4501. > tag.
  4502. This is enough for most users, but these rules leave much undecided,
  4503. especially when it comes to nested emphasis. The original
  4504. `Markdown.pl` test suite makes it clear that triple `***` and
  4505. `___` delimiters can be used for strong emphasis, and most
  4506. implementations have also allowed the following patterns:
  4507. ``` markdown
  4508. ***strong emph***
  4509. ***strong** in emph*
  4510. ***emph* in strong**
  4511. **in strong *emph***
  4512. *in emph **strong***
  4513. ```
  4514. The following patterns are less widely supported, but the intent
  4515. is clear and they are useful (especially in contexts like bibliography
  4516. entries):
  4517. ``` markdown
  4518. *emph *with emph* in it*
  4519. **strong **with strong** in it**
  4520. ```
  4521. Many implementations have also restricted intraword emphasis to
  4522. the `*` forms, to avoid unwanted emphasis in words containing
  4523. internal underscores. (It is best practice to put these in code
  4524. spans, but users often do not.)
  4525. ``` markdown
  4526. internal emphasis: foo*bar*baz
  4527. no emphasis: foo_bar_baz
  4528. ```
  4529. The rules given below capture all of these patterns, while allowing
  4530. for efficient parsing strategies that do not backtrack.
  4531. First, some definitions. A [delimiter run](@) is either
  4532. a sequence of one or more `*` characters that is not preceded or
  4533. followed by a non-backslash-escaped `*` character, or a sequence
  4534. of one or more `_` characters that is not preceded or followed by
  4535. a non-backslash-escaped `_` character.
  4536. A [left-flanking delimiter run](@) is
  4537. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4538. and (b) not followed by a [punctuation character], or
  4539. preceded by [Unicode whitespace] or a [punctuation character].
  4540. For purposes of this definition, the beginning and the end of
  4541. the line count as Unicode whitespace.
  4542. A [right-flanking delimiter run](@) is
  4543. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4544. and (b) not preceded by a [punctuation character], or
  4545. followed by [Unicode whitespace] or a [punctuation character].
  4546. For purposes of this definition, the beginning and the end of
  4547. the line count as Unicode whitespace.
  4548. Here are some examples of delimiter runs.
  4549. - left-flanking but not right-flanking:
  4550. ```
  4551. ***abc
  4552. _abc
  4553. **"abc"
  4554. _"abc"
  4555. ```
  4556. - right-flanking but not left-flanking:
  4557. ```
  4558. abc***
  4559. abc_
  4560. "abc"**
  4561. "abc"_
  4562. ```
  4563. - Both left and right-flanking:
  4564. ```
  4565. abc***def
  4566. "abc"_"def"
  4567. ```
  4568. - Neither left nor right-flanking:
  4569. ```
  4570. abc *** def
  4571. a _ b
  4572. ```
  4573. (The idea of distinguishing left-flanking and right-flanking
  4574. delimiter runs based on the character before and the character
  4575. after comes from Roopesh Chander's
  4576. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4577. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4578. run," and its rules for distinguishing left- and right-flanking runs
  4579. are a bit more complex than the ones given here.)
  4580. The following rules define emphasis and strong emphasis:
  4581. 1. A single `*` character [can open emphasis](@)
  4582. iff (if and only if) it is part of a [left-flanking delimiter run].
  4583. 2. A single `_` character [can open emphasis] iff
  4584. it is part of a [left-flanking delimiter run]
  4585. and either (a) not part of a [right-flanking delimiter run]
  4586. or (b) part of a [right-flanking delimiter run]
  4587. preceded by punctuation.
  4588. 3. A single `*` character [can close emphasis](@)
  4589. iff it is part of a [right-flanking delimiter run].
  4590. 4. A single `_` character [can close emphasis] iff
  4591. it is part of a [right-flanking delimiter run]
  4592. and either (a) not part of a [left-flanking delimiter run]
  4593. or (b) part of a [left-flanking delimiter run]
  4594. followed by punctuation.
  4595. 5. A double `**` [can open strong emphasis](@)
  4596. iff it is part of a [left-flanking delimiter run].
  4597. 6. A double `__` [can open strong emphasis] iff
  4598. it is part of a [left-flanking delimiter run]
  4599. and either (a) not part of a [right-flanking delimiter run]
  4600. or (b) part of a [right-flanking delimiter run]
  4601. preceded by punctuation.
  4602. 7. A double `**` [can close strong emphasis](@)
  4603. iff it is part of a [right-flanking delimiter run].
  4604. 8. A double `__` [can close strong emphasis] iff
  4605. it is part of a [right-flanking delimiter run]
  4606. and either (a) not part of a [left-flanking delimiter run]
  4607. or (b) part of a [left-flanking delimiter run]
  4608. followed by punctuation.
  4609. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4610. with a delimiter that [can close emphasis], and that uses the same
  4611. character (`_` or `*`) as the opening delimiter. The
  4612. opening and closing delimiters must belong to separate
  4613. [delimiter runs]. If one of the delimiters can both
  4614. open and close emphasis, then the sum of the lengths of the
  4615. delimiter runs containing the opening and closing delimiters
  4616. must not be a multiple of 3.
  4617. 10. Strong emphasis begins with a delimiter that
  4618. [can open strong emphasis] and ends with a delimiter that
  4619. [can close strong emphasis], and that uses the same character
  4620. (`_` or `*`) as the opening delimiter. The
  4621. opening and closing delimiters must belong to separate
  4622. [delimiter runs]. If one of the delimiters can both open
  4623. and close strong emphasis, then the sum of the lengths of
  4624. the delimiter runs containing the opening and closing
  4625. delimiters must not be a multiple of 3.
  4626. 11. 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. 12. A literal `_` character cannot occur at the beginning or end of
  4630. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4631. is backslash-escaped.
  4632. Where rules 1--12 above are compatible with multiple parsings,
  4633. the following principles resolve ambiguity:
  4634. 13. The number of nestings should be minimized. Thus, for example,
  4635. an interpretation `<strong>...</strong>` is always preferred to
  4636. `<em><em>...</em></em>`.
  4637. 14. An interpretation `<em><strong>...</strong></em>` is always
  4638. preferred to `<strong><em>...</em></strong>`.
  4639. 15. When two potential emphasis or strong emphasis spans overlap,
  4640. so that the second begins before the first ends and ends after
  4641. the first ends, the first takes precedence. Thus, for example,
  4642. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4643. than `*foo <em>bar* baz</em>`.
  4644. 16. When there are two potential emphasis or strong emphasis spans
  4645. with the same closing delimiter, the shorter one (the one that
  4646. opens later) takes precedence. Thus, for example,
  4647. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4648. rather than `<strong>foo **bar baz</strong>`.
  4649. 17. Inline code spans, links, images, and HTML tags group more tightly
  4650. than emphasis. So, when there is a choice between an interpretation
  4651. that contains one of these elements and one that does not, the
  4652. former always wins. Thus, for example, `*[foo*](bar)` is
  4653. parsed as `*<a href="bar">foo*</a>` rather than as
  4654. `<em>[foo</em>](bar)`.
  4655. These rules can be illustrated through a series of examples.
  4656. Rule 1:
  4657. ```````````````````````````````` example
  4658. *foo bar*
  4659. .
  4660. <p><em>foo bar</em></p>
  4661. ````````````````````````````````
  4662. This is not emphasis, because the opening `*` is followed by
  4663. whitespace, and hence not part of a [left-flanking delimiter run]:
  4664. ```````````````````````````````` example
  4665. a * foo bar*
  4666. .
  4667. <p>a * foo bar*</p>
  4668. ````````````````````````````````
  4669. This is not emphasis, because the opening `*` is preceded
  4670. by an alphanumeric and followed by punctuation, and hence
  4671. not part of a [left-flanking delimiter run]:
  4672. ```````````````````````````````` example
  4673. a*"foo"*
  4674. .
  4675. <p>a*&quot;foo&quot;*</p>
  4676. ````````````````````````````````
  4677. Unicode nonbreaking spaces count as whitespace, too:
  4678. ```````````````````````````````` example
  4679. * a *
  4680. .
  4681. <p>* a *</p>
  4682. ````````````````````````````````
  4683. Intraword emphasis with `*` is permitted:
  4684. ```````````````````````````````` example
  4685. foo*bar*
  4686. .
  4687. <p>foo<em>bar</em></p>
  4688. ````````````````````````````````
  4689. ```````````````````````````````` example
  4690. 5*6*78
  4691. .
  4692. <p>5<em>6</em>78</p>
  4693. ````````````````````````````````
  4694. Rule 2:
  4695. ```````````````````````````````` example
  4696. _foo bar_
  4697. .
  4698. <p><em>foo bar</em></p>
  4699. ````````````````````````````````
  4700. This is not emphasis, because the opening `_` is followed by
  4701. whitespace:
  4702. ```````````````````````````````` example
  4703. _ foo bar_
  4704. .
  4705. <p>_ foo bar_</p>
  4706. ````````````````````````````````
  4707. This is not emphasis, because the opening `_` is preceded
  4708. by an alphanumeric and followed by punctuation:
  4709. ```````````````````````````````` example
  4710. a_"foo"_
  4711. .
  4712. <p>a_&quot;foo&quot;_</p>
  4713. ````````````````````````````````
  4714. Emphasis with `_` is not allowed inside words:
  4715. ```````````````````````````````` example
  4716. foo_bar_
  4717. .
  4718. <p>foo_bar_</p>
  4719. ````````````````````````````````
  4720. ```````````````````````````````` example
  4721. 5_6_78
  4722. .
  4723. <p>5_6_78</p>
  4724. ````````````````````````````````
  4725. ```````````````````````````````` example
  4726. пристаням_стремятся_
  4727. .
  4728. <p>пристаням_стремятся_</p>
  4729. ````````````````````````````````
  4730. Here `_` does not generate emphasis, because the first delimiter run
  4731. is right-flanking and the second left-flanking:
  4732. ```````````````````````````````` example
  4733. aa_"bb"_cc
  4734. .
  4735. <p>aa_&quot;bb&quot;_cc</p>
  4736. ````````````````````````````````
  4737. This is emphasis, even though the opening delimiter is
  4738. both left- and right-flanking, because it is preceded by
  4739. punctuation:
  4740. ```````````````````````````````` example
  4741. foo-_(bar)_
  4742. .
  4743. <p>foo-<em>(bar)</em></p>
  4744. ````````````````````````````````
  4745. Rule 3:
  4746. This is not emphasis, because the closing delimiter does
  4747. not match the opening delimiter:
  4748. ```````````````````````````````` example
  4749. _foo*
  4750. .
  4751. <p>_foo*</p>
  4752. ````````````````````````````````
  4753. This is not emphasis, because the closing `*` is preceded by
  4754. whitespace:
  4755. ```````````````````````````````` example
  4756. *foo bar *
  4757. .
  4758. <p>*foo bar *</p>
  4759. ````````````````````````````````
  4760. A newline also counts as whitespace:
  4761. ```````````````````````````````` example
  4762. *foo bar
  4763. *
  4764. .
  4765. <p>*foo bar
  4766. *</p>
  4767. ````````````````````````````````
  4768. This is not emphasis, because the second `*` is
  4769. preceded by punctuation and followed by an alphanumeric
  4770. (hence it is not part of a [right-flanking delimiter run]:
  4771. ```````````````````````````````` example
  4772. *(*foo)
  4773. .
  4774. <p>*(*foo)</p>
  4775. ````````````````````````````````
  4776. The point of this restriction is more easily appreciated
  4777. with this example:
  4778. ```````````````````````````````` example
  4779. *(*foo*)*
  4780. .
  4781. <p><em>(<em>foo</em>)</em></p>
  4782. ````````````````````````````````
  4783. Intraword emphasis with `*` is allowed:
  4784. ```````````````````````````````` example
  4785. *foo*bar
  4786. .
  4787. <p><em>foo</em>bar</p>
  4788. ````````````````````````````````
  4789. Rule 4:
  4790. This is not emphasis, because the closing `_` is preceded by
  4791. whitespace:
  4792. ```````````````````````````````` example
  4793. _foo bar _
  4794. .
  4795. <p>_foo bar _</p>
  4796. ````````````````````````````````
  4797. This is not emphasis, because the second `_` is
  4798. preceded by punctuation and followed by an alphanumeric:
  4799. ```````````````````````````````` example
  4800. _(_foo)
  4801. .
  4802. <p>_(_foo)</p>
  4803. ````````````````````````````````
  4804. This is emphasis within emphasis:
  4805. ```````````````````````````````` example
  4806. _(_foo_)_
  4807. .
  4808. <p><em>(<em>foo</em>)</em></p>
  4809. ````````````````````````````````
  4810. Intraword emphasis is disallowed for `_`:
  4811. ```````````````````````````````` example
  4812. _foo_bar
  4813. .
  4814. <p>_foo_bar</p>
  4815. ````````````````````````````````
  4816. ```````````````````````````````` example
  4817. _пристаням_стремятся
  4818. .
  4819. <p>_пристаням_стремятся</p>
  4820. ````````````````````````````````
  4821. ```````````````````````````````` example
  4822. _foo_bar_baz_
  4823. .
  4824. <p><em>foo_bar_baz</em></p>
  4825. ````````````````````````````````
  4826. This is emphasis, even though the closing delimiter is
  4827. both left- and right-flanking, because it is followed by
  4828. punctuation:
  4829. ```````````````````````````````` example
  4830. _(bar)_.
  4831. .
  4832. <p><em>(bar)</em>.</p>
  4833. ````````````````````````````````
  4834. Rule 5:
  4835. ```````````````````````````````` example
  4836. **foo bar**
  4837. .
  4838. <p><strong>foo bar</strong></p>
  4839. ````````````````````````````````
  4840. This is not strong emphasis, because the opening delimiter is
  4841. followed by whitespace:
  4842. ```````````````````````````````` example
  4843. ** foo bar**
  4844. .
  4845. <p>** foo bar**</p>
  4846. ````````````````````````````````
  4847. This is not strong emphasis, because the opening `**` is preceded
  4848. by an alphanumeric and followed by punctuation, and hence
  4849. not part of a [left-flanking delimiter run]:
  4850. ```````````````````````````````` example
  4851. a**"foo"**
  4852. .
  4853. <p>a**&quot;foo&quot;**</p>
  4854. ````````````````````````````````
  4855. Intraword strong emphasis with `**` is permitted:
  4856. ```````````````````````````````` example
  4857. foo**bar**
  4858. .
  4859. <p>foo<strong>bar</strong></p>
  4860. ````````````````````````````````
  4861. Rule 6:
  4862. ```````````````````````````````` example
  4863. __foo bar__
  4864. .
  4865. <p><strong>foo bar</strong></p>
  4866. ````````````````````````````````
  4867. This is not strong emphasis, because the opening delimiter is
  4868. followed by whitespace:
  4869. ```````````````````````````````` example
  4870. __ foo bar__
  4871. .
  4872. <p>__ foo bar__</p>
  4873. ````````````````````````````````
  4874. A newline counts as whitespace:
  4875. ```````````````````````````````` example
  4876. __
  4877. foo bar__
  4878. .
  4879. <p>__
  4880. foo bar__</p>
  4881. ````````````````````````````````
  4882. This is not strong emphasis, because the opening `__` is preceded
  4883. by an alphanumeric and followed by punctuation:
  4884. ```````````````````````````````` example
  4885. a__"foo"__
  4886. .
  4887. <p>a__&quot;foo&quot;__</p>
  4888. ````````````````````````````````
  4889. Intraword strong emphasis is forbidden with `__`:
  4890. ```````````````````````````````` example
  4891. foo__bar__
  4892. .
  4893. <p>foo__bar__</p>
  4894. ````````````````````````````````
  4895. ```````````````````````````````` example
  4896. 5__6__78
  4897. .
  4898. <p>5__6__78</p>
  4899. ````````````````````````````````
  4900. ```````````````````````````````` example
  4901. пристаням__стремятся__
  4902. .
  4903. <p>пристаням__стремятся__</p>
  4904. ````````````````````````````````
  4905. ```````````````````````````````` example
  4906. __foo, __bar__, baz__
  4907. .
  4908. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4909. ````````````````````````````````
  4910. This is strong emphasis, even though the opening delimiter is
  4911. both left- and right-flanking, because it is preceded by
  4912. punctuation:
  4913. ```````````````````````````````` example
  4914. foo-__(bar)__
  4915. .
  4916. <p>foo-<strong>(bar)</strong></p>
  4917. ````````````````````````````````
  4918. Rule 7:
  4919. This is not strong emphasis, because the closing delimiter is preceded
  4920. by whitespace:
  4921. ```````````````````````````````` example
  4922. **foo bar **
  4923. .
  4924. <p>**foo bar **</p>
  4925. ````````````````````````````````
  4926. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4927. Rule 11.)
  4928. This is not strong emphasis, because the second `**` is
  4929. preceded by punctuation and followed by an alphanumeric:
  4930. ```````````````````````````````` example
  4931. **(**foo)
  4932. .
  4933. <p>**(**foo)</p>
  4934. ````````````````````````````````
  4935. The point of this restriction is more easily appreciated
  4936. with these examples:
  4937. ```````````````````````````````` example
  4938. *(**foo**)*
  4939. .
  4940. <p><em>(<strong>foo</strong>)</em></p>
  4941. ````````````````````````````````
  4942. ```````````````````````````````` example
  4943. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4944. *Asclepias physocarpa*)**
  4945. .
  4946. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4947. <em>Asclepias physocarpa</em>)</strong></p>
  4948. ````````````````````````````````
  4949. ```````````````````````````````` example
  4950. **foo "*bar*" foo**
  4951. .
  4952. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4953. ````````````````````````````````
  4954. Intraword emphasis:
  4955. ```````````````````````````````` example
  4956. **foo**bar
  4957. .
  4958. <p><strong>foo</strong>bar</p>
  4959. ````````````````````````````````
  4960. Rule 8:
  4961. This is not strong emphasis, because the closing delimiter is
  4962. preceded by whitespace:
  4963. ```````````````````````````````` example
  4964. __foo bar __
  4965. .
  4966. <p>__foo bar __</p>
  4967. ````````````````````````````````
  4968. This is not strong emphasis, because the second `__` is
  4969. preceded by punctuation and followed by an alphanumeric:
  4970. ```````````````````````````````` example
  4971. __(__foo)
  4972. .
  4973. <p>__(__foo)</p>
  4974. ````````````````````````````````
  4975. The point of this restriction is more easily appreciated
  4976. with this example:
  4977. ```````````````````````````````` example
  4978. _(__foo__)_
  4979. .
  4980. <p><em>(<strong>foo</strong>)</em></p>
  4981. ````````````````````````````````
  4982. Intraword strong emphasis is forbidden with `__`:
  4983. ```````````````````````````````` example
  4984. __foo__bar
  4985. .
  4986. <p>__foo__bar</p>
  4987. ````````````````````````````````
  4988. ```````````````````````````````` example
  4989. __пристаням__стремятся
  4990. .
  4991. <p>__пристаням__стремятся</p>
  4992. ````````````````````````````````
  4993. ```````````````````````````````` example
  4994. __foo__bar__baz__
  4995. .
  4996. <p><strong>foo__bar__baz</strong></p>
  4997. ````````````````````````````````
  4998. This is strong emphasis, even though the closing delimiter is
  4999. both left- and right-flanking, because it is followed by
  5000. punctuation:
  5001. ```````````````````````````````` example
  5002. __(bar)__.
  5003. .
  5004. <p><strong>(bar)</strong>.</p>
  5005. ````````````````````````````````
  5006. Rule 9:
  5007. Any nonempty sequence of inline elements can be the contents of an
  5008. emphasized span.
  5009. ```````````````````````````````` example
  5010. *foo [bar](/url)*
  5011. .
  5012. <p><em>foo <a href="/url">bar</a></em></p>
  5013. ````````````````````````````````
  5014. ```````````````````````````````` example
  5015. *foo
  5016. bar*
  5017. .
  5018. <p><em>foo
  5019. bar</em></p>
  5020. ````````````````````````````````
  5021. In particular, emphasis and strong emphasis can be nested
  5022. inside emphasis:
  5023. ```````````````````````````````` example
  5024. _foo __bar__ baz_
  5025. .
  5026. <p><em>foo <strong>bar</strong> baz</em></p>
  5027. ````````````````````````````````
  5028. ```````````````````````````````` example
  5029. _foo _bar_ baz_
  5030. .
  5031. <p><em>foo <em>bar</em> baz</em></p>
  5032. ````````````````````````````````
  5033. ```````````````````````````````` example
  5034. __foo_ bar_
  5035. .
  5036. <p><em><em>foo</em> bar</em></p>
  5037. ````````````````````````````````
  5038. ```````````````````````````````` example
  5039. *foo *bar**
  5040. .
  5041. <p><em>foo <em>bar</em></em></p>
  5042. ````````````````````````````````
  5043. ```````````````````````````````` example
  5044. *foo **bar** baz*
  5045. .
  5046. <p><em>foo <strong>bar</strong> baz</em></p>
  5047. ````````````````````````````````
  5048. ```````````````````````````````` example
  5049. *foo**bar**baz*
  5050. .
  5051. <p><em>foo<strong>bar</strong>baz</em></p>
  5052. ````````````````````````````````
  5053. Note that in the preceding case, the interpretation
  5054. ``` markdown
  5055. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5056. ```
  5057. is precluded by the condition that a delimiter that
  5058. can both open and close (like the `*` after `foo`)
  5059. cannot form emphasis if the sum of the lengths of
  5060. the delimiter runs containing the opening and
  5061. closing delimiters is a multiple of 3.
  5062. The same condition ensures that the following
  5063. cases are all strong emphasis nested inside
  5064. emphasis, even when the interior spaces are
  5065. omitted:
  5066. ```````````````````````````````` example
  5067. ***foo** bar*
  5068. .
  5069. <p><em><strong>foo</strong> bar</em></p>
  5070. ````````````````````````````````
  5071. ```````````````````````````````` example
  5072. *foo **bar***
  5073. .
  5074. <p><em>foo <strong>bar</strong></em></p>
  5075. ````````````````````````````````
  5076. ```````````````````````````````` example
  5077. *foo**bar***
  5078. .
  5079. <p><em>foo<strong>bar</strong></em></p>
  5080. ````````````````````````````````
  5081. Indefinite levels of nesting are possible:
  5082. ```````````````````````````````` example
  5083. *foo **bar *baz* bim** bop*
  5084. .
  5085. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5086. ````````````````````````````````
  5087. ```````````````````````````````` example
  5088. *foo [*bar*](/url)*
  5089. .
  5090. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5091. ````````````````````````````````
  5092. There can be no empty emphasis or strong emphasis:
  5093. ```````````````````````````````` example
  5094. ** is not an empty emphasis
  5095. .
  5096. <p>** is not an empty emphasis</p>
  5097. ````````````````````````````````
  5098. ```````````````````````````````` example
  5099. **** is not an empty strong emphasis
  5100. .
  5101. <p>**** is not an empty strong emphasis</p>
  5102. ````````````````````````````````
  5103. Rule 10:
  5104. Any nonempty sequence of inline elements can be the contents of an
  5105. strongly emphasized span.
  5106. ```````````````````````````````` example
  5107. **foo [bar](/url)**
  5108. .
  5109. <p><strong>foo <a href="/url">bar</a></strong></p>
  5110. ````````````````````````````````
  5111. ```````````````````````````````` example
  5112. **foo
  5113. bar**
  5114. .
  5115. <p><strong>foo
  5116. bar</strong></p>
  5117. ````````````````````````````````
  5118. In particular, emphasis and strong emphasis can be nested
  5119. inside strong emphasis:
  5120. ```````````````````````````````` example
  5121. __foo _bar_ baz__
  5122. .
  5123. <p><strong>foo <em>bar</em> baz</strong></p>
  5124. ````````````````````````````````
  5125. ```````````````````````````````` example
  5126. __foo __bar__ baz__
  5127. .
  5128. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5129. ````````````````````````````````
  5130. ```````````````````````````````` example
  5131. ____foo__ bar__
  5132. .
  5133. <p><strong><strong>foo</strong> bar</strong></p>
  5134. ````````````````````````````````
  5135. ```````````````````````````````` example
  5136. **foo **bar****
  5137. .
  5138. <p><strong>foo <strong>bar</strong></strong></p>
  5139. ````````````````````````````````
  5140. ```````````````````````````````` example
  5141. **foo *bar* baz**
  5142. .
  5143. <p><strong>foo <em>bar</em> baz</strong></p>
  5144. ````````````````````````````````
  5145. ```````````````````````````````` example
  5146. **foo*bar*baz**
  5147. .
  5148. <p><strong>foo<em>bar</em>baz</strong></p>
  5149. ````````````````````````````````
  5150. ```````````````````````````````` example
  5151. ***foo* bar**
  5152. .
  5153. <p><strong><em>foo</em> bar</strong></p>
  5154. ````````````````````````````````
  5155. ```````````````````````````````` example
  5156. **foo *bar***
  5157. .
  5158. <p><strong>foo <em>bar</em></strong></p>
  5159. ````````````````````````````````
  5160. Indefinite levels of nesting are possible:
  5161. ```````````````````````````````` example
  5162. **foo *bar **baz**
  5163. bim* bop**
  5164. .
  5165. <p><strong>foo <em>bar <strong>baz</strong>
  5166. bim</em> bop</strong></p>
  5167. ````````````````````````````````
  5168. ```````````````````````````````` example
  5169. **foo [*bar*](/url)**
  5170. .
  5171. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5172. ````````````````````````````````
  5173. There can be no empty emphasis or strong emphasis:
  5174. ```````````````````````````````` example
  5175. __ is not an empty emphasis
  5176. .
  5177. <p>__ is not an empty emphasis</p>
  5178. ````````````````````````````````
  5179. ```````````````````````````````` example
  5180. ____ is not an empty strong emphasis
  5181. .
  5182. <p>____ is not an empty strong emphasis</p>
  5183. ````````````````````````````````
  5184. Rule 11:
  5185. ```````````````````````````````` example
  5186. foo ***
  5187. .
  5188. <p>foo ***</p>
  5189. ````````````````````````````````
  5190. ```````````````````````````````` example
  5191. foo *\**
  5192. .
  5193. <p>foo <em>*</em></p>
  5194. ````````````````````````````````
  5195. ```````````````````````````````` example
  5196. foo *_*
  5197. .
  5198. <p>foo <em>_</em></p>
  5199. ````````````````````````````````
  5200. ```````````````````````````````` example
  5201. foo *****
  5202. .
  5203. <p>foo *****</p>
  5204. ````````````````````````````````
  5205. ```````````````````````````````` example
  5206. foo **\***
  5207. .
  5208. <p>foo <strong>*</strong></p>
  5209. ````````````````````````````````
  5210. ```````````````````````````````` example
  5211. foo **_**
  5212. .
  5213. <p>foo <strong>_</strong></p>
  5214. ````````````````````````````````
  5215. Note that when delimiters do not match evenly, Rule 11 determines
  5216. that the excess literal `*` characters will appear outside of the
  5217. emphasis, rather than inside it:
  5218. ```````````````````````````````` example
  5219. **foo*
  5220. .
  5221. <p>*<em>foo</em></p>
  5222. ````````````````````````````````
  5223. ```````````````````````````````` example
  5224. *foo**
  5225. .
  5226. <p><em>foo</em>*</p>
  5227. ````````````````````````````````
  5228. ```````````````````````````````` example
  5229. ***foo**
  5230. .
  5231. <p>*<strong>foo</strong></p>
  5232. ````````````````````````````````
  5233. ```````````````````````````````` example
  5234. ****foo*
  5235. .
  5236. <p>***<em>foo</em></p>
  5237. ````````````````````````````````
  5238. ```````````````````````````````` example
  5239. **foo***
  5240. .
  5241. <p><strong>foo</strong>*</p>
  5242. ````````````````````````````````
  5243. ```````````````````````````````` example
  5244. *foo****
  5245. .
  5246. <p><em>foo</em>***</p>
  5247. ````````````````````````````````
  5248. Rule 12:
  5249. ```````````````````````````````` example
  5250. foo ___
  5251. .
  5252. <p>foo ___</p>
  5253. ````````````````````````````````
  5254. ```````````````````````````````` example
  5255. foo _\__
  5256. .
  5257. <p>foo <em>_</em></p>
  5258. ````````````````````````````````
  5259. ```````````````````````````````` example
  5260. foo _*_
  5261. .
  5262. <p>foo <em>*</em></p>
  5263. ````````````````````````````````
  5264. ```````````````````````````````` example
  5265. foo _____
  5266. .
  5267. <p>foo _____</p>
  5268. ````````````````````````````````
  5269. ```````````````````````````````` example
  5270. foo __\___
  5271. .
  5272. <p>foo <strong>_</strong></p>
  5273. ````````````````````````````````
  5274. ```````````````````````````````` example
  5275. foo __*__
  5276. .
  5277. <p>foo <strong>*</strong></p>
  5278. ````````````````````````````````
  5279. ```````````````````````````````` example
  5280. __foo_
  5281. .
  5282. <p>_<em>foo</em></p>
  5283. ````````````````````````````````
  5284. Note that when delimiters do not match evenly, Rule 12 determines
  5285. that the excess literal `_` characters will appear outside of the
  5286. emphasis, rather than inside it:
  5287. ```````````````````````````````` example
  5288. _foo__
  5289. .
  5290. <p><em>foo</em>_</p>
  5291. ````````````````````````````````
  5292. ```````````````````````````````` example
  5293. ___foo__
  5294. .
  5295. <p>_<strong>foo</strong></p>
  5296. ````````````````````````````````
  5297. ```````````````````````````````` example
  5298. ____foo_
  5299. .
  5300. <p>___<em>foo</em></p>
  5301. ````````````````````````````````
  5302. ```````````````````````````````` example
  5303. __foo___
  5304. .
  5305. <p><strong>foo</strong>_</p>
  5306. ````````````````````````````````
  5307. ```````````````````````````````` example
  5308. _foo____
  5309. .
  5310. <p><em>foo</em>___</p>
  5311. ````````````````````````````````
  5312. Rule 13 implies that if you want emphasis nested directly inside
  5313. emphasis, you must use different delimiters:
  5314. ```````````````````````````````` example
  5315. **foo**
  5316. .
  5317. <p><strong>foo</strong></p>
  5318. ````````````````````````````````
  5319. ```````````````````````````````` example
  5320. *_foo_*
  5321. .
  5322. <p><em><em>foo</em></em></p>
  5323. ````````````````````````````````
  5324. ```````````````````````````````` example
  5325. __foo__
  5326. .
  5327. <p><strong>foo</strong></p>
  5328. ````````````````````````````````
  5329. ```````````````````````````````` example
  5330. _*foo*_
  5331. .
  5332. <p><em><em>foo</em></em></p>
  5333. ````````````````````````````````
  5334. However, strong emphasis within strong emphasis is possible without
  5335. switching delimiters:
  5336. ```````````````````````````````` example
  5337. ****foo****
  5338. .
  5339. <p><strong><strong>foo</strong></strong></p>
  5340. ````````````````````````````````
  5341. ```````````````````````````````` example
  5342. ____foo____
  5343. .
  5344. <p><strong><strong>foo</strong></strong></p>
  5345. ````````````````````````````````
  5346. Rule 13 can be applied to arbitrarily long sequences of
  5347. delimiters:
  5348. ```````````````````````````````` example
  5349. ******foo******
  5350. .
  5351. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5352. ````````````````````````````````
  5353. Rule 14:
  5354. ```````````````````````````````` example
  5355. ***foo***
  5356. .
  5357. <p><em><strong>foo</strong></em></p>
  5358. ````````````````````````````````
  5359. ```````````````````````````````` example
  5360. _____foo_____
  5361. .
  5362. <p><em><strong><strong>foo</strong></strong></em></p>
  5363. ````````````````````````````````
  5364. Rule 15:
  5365. ```````````````````````````````` example
  5366. *foo _bar* baz_
  5367. .
  5368. <p><em>foo _bar</em> baz_</p>
  5369. ````````````````````````````````
  5370. ```````````````````````````````` example
  5371. *foo __bar *baz bim__ bam*
  5372. .
  5373. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5374. ````````````````````````````````
  5375. Rule 16:
  5376. ```````````````````````````````` example
  5377. **foo **bar baz**
  5378. .
  5379. <p>**foo <strong>bar baz</strong></p>
  5380. ````````````````````````````````
  5381. ```````````````````````````````` example
  5382. *foo *bar baz*
  5383. .
  5384. <p>*foo <em>bar baz</em></p>
  5385. ````````````````````````````````
  5386. Rule 17:
  5387. ```````````````````````````````` example
  5388. *[bar*](/url)
  5389. .
  5390. <p>*<a href="/url">bar*</a></p>
  5391. ````````````````````````````````
  5392. ```````````````````````````````` example
  5393. _foo [bar_](/url)
  5394. .
  5395. <p>_foo <a href="/url">bar_</a></p>
  5396. ````````````````````````````````
  5397. ```````````````````````````````` example
  5398. *<img src="foo" title="*"/>
  5399. .
  5400. <p>*<img src="foo" title="*"/></p>
  5401. ````````````````````````````````
  5402. ```````````````````````````````` example
  5403. **<a href="**">
  5404. .
  5405. <p>**<a href="**"></p>
  5406. ````````````````````````````````
  5407. ```````````````````````````````` example
  5408. __<a href="__">
  5409. .
  5410. <p>__<a href="__"></p>
  5411. ````````````````````````````````
  5412. ```````````````````````````````` example
  5413. *a `*`*
  5414. .
  5415. <p><em>a <code>*</code></em></p>
  5416. ````````````````````````````````
  5417. ```````````````````````````````` example
  5418. _a `_`_
  5419. .
  5420. <p><em>a <code>_</code></em></p>
  5421. ````````````````````````````````
  5422. ```````````````````````````````` example
  5423. **a<http://foo.bar/?q=**>
  5424. .
  5425. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5426. ````````````````````````````````
  5427. ```````````````````````````````` example
  5428. __a<http://foo.bar/?q=__>
  5429. .
  5430. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5431. ````````````````````````````````
  5432. ## Links
  5433. A link contains [link text] (the visible text), a [link destination]
  5434. (the URI that is the link destination), and optionally a [link title].
  5435. There are two basic kinds of links in Markdown. In [inline links] the
  5436. destination and title are given immediately after the link text. In
  5437. [reference links] the destination and title are defined elsewhere in
  5438. the document.
  5439. A [link text](@) consists of a sequence of zero or more
  5440. inline elements enclosed by square brackets (`[` and `]`). The
  5441. following rules apply:
  5442. - Links may not contain other links, at any level of nesting. If
  5443. multiple otherwise valid link definitions appear nested inside each
  5444. other, the inner-most definition is used.
  5445. - Brackets are allowed in the [link text] only if (a) they
  5446. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5447. with an open bracket `[`, a sequence of zero or more inlines, and
  5448. a close bracket `]`.
  5449. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5450. than the brackets in link text. Thus, for example,
  5451. `` [foo`]` `` could not be a link text, since the second `]`
  5452. is part of a code span.
  5453. - The brackets in link text bind more tightly than markers for
  5454. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5455. A [link destination](@) consists of either
  5456. - a sequence of zero or more characters between an opening `<` and a
  5457. closing `>` that contains no spaces, line breaks, or unescaped
  5458. `<` or `>` characters, or
  5459. - a nonempty sequence of characters that does not include
  5460. ASCII space or control characters, and includes parentheses
  5461. only if (a) they are backslash-escaped or (b) they are part of
  5462. a balanced pair of unescaped parentheses. (Implementations
  5463. may impose limits on parentheses nesting to avoid performance
  5464. issues, but at least three levels of nesting should be supported.)
  5465. A [link title](@) consists of either
  5466. - a sequence of zero or more characters between straight double-quote
  5467. characters (`"`), including a `"` character only if it is
  5468. backslash-escaped, or
  5469. - a sequence of zero or more characters between straight single-quote
  5470. characters (`'`), including a `'` character only if it is
  5471. backslash-escaped, or
  5472. - a sequence of zero or more characters between matching parentheses
  5473. (`(...)`), including a `)` character only if it is backslash-escaped.
  5474. Although [link titles] may span multiple lines, they may not contain
  5475. a [blank line].
  5476. An [inline link](@) consists of a [link text] followed immediately
  5477. by a left parenthesis `(`, optional [whitespace], an optional
  5478. [link destination], an optional [link title] separated from the link
  5479. destination by [whitespace], optional [whitespace], and a right
  5480. parenthesis `)`. The link's text consists of the inlines contained
  5481. in the [link text] (excluding the enclosing square brackets).
  5482. The link's URI consists of the link destination, excluding enclosing
  5483. `<...>` if present, with backslash-escapes in effect as described
  5484. above. The link's title consists of the link title, excluding its
  5485. enclosing delimiters, with backslash-escapes in effect as described
  5486. above.
  5487. Here is a simple inline link:
  5488. ```````````````````````````````` example
  5489. [link](/uri "title")
  5490. .
  5491. <p><a href="/uri" title="title">link</a></p>
  5492. ````````````````````````````````
  5493. The title may be omitted:
  5494. ```````````````````````````````` example
  5495. [link](/uri)
  5496. .
  5497. <p><a href="/uri">link</a></p>
  5498. ````````````````````````````````
  5499. Both the title and the destination may be omitted:
  5500. ```````````````````````````````` example
  5501. [link]()
  5502. .
  5503. <p><a href="">link</a></p>
  5504. ````````````````````````````````
  5505. ```````````````````````````````` example
  5506. [link](<>)
  5507. .
  5508. <p><a href="">link</a></p>
  5509. ````````````````````````````````
  5510. The destination cannot contain spaces or line breaks,
  5511. even if enclosed in pointy brackets:
  5512. ```````````````````````````````` example
  5513. [link](/my uri)
  5514. .
  5515. <p>[link](/my uri)</p>
  5516. ````````````````````````````````
  5517. ```````````````````````````````` example
  5518. [link](</my uri>)
  5519. .
  5520. <p>[link](&lt;/my uri&gt;)</p>
  5521. ````````````````````````````````
  5522. ```````````````````````````````` example
  5523. [link](foo
  5524. bar)
  5525. .
  5526. <p>[link](foo
  5527. bar)</p>
  5528. ````````````````````````````````
  5529. ```````````````````````````````` example
  5530. [link](<foo
  5531. bar>)
  5532. .
  5533. <p>[link](<foo
  5534. bar>)</p>
  5535. ````````````````````````````````
  5536. Parentheses inside the link destination may be escaped:
  5537. ```````````````````````````````` example
  5538. [link](\(foo\))
  5539. .
  5540. <p><a href="(foo)">link</a></p>
  5541. ````````````````````````````````
  5542. Any number of parentheses are allowed without escaping, as long as they are
  5543. balanced:
  5544. ```````````````````````````````` example
  5545. [link](foo(and(bar)))
  5546. .
  5547. <p><a href="foo(and(bar))">link</a></p>
  5548. ````````````````````````````````
  5549. However, if you have unbalanced parentheses, you need to escape or use the
  5550. `<...>` form:
  5551. ```````````````````````````````` example
  5552. [link](foo\(and\(bar\))
  5553. .
  5554. <p><a href="foo(and(bar)">link</a></p>
  5555. ````````````````````````````````
  5556. ```````````````````````````````` example
  5557. [link](<foo(and(bar)>)
  5558. .
  5559. <p><a href="foo(and(bar)">link</a></p>
  5560. ````````````````````````````````
  5561. Parentheses and other symbols can also be escaped, as usual
  5562. in Markdown:
  5563. ```````````````````````````````` example
  5564. [link](foo\)\:)
  5565. .
  5566. <p><a href="foo):">link</a></p>
  5567. ````````````````````````````````
  5568. A link can contain fragment identifiers and queries:
  5569. ```````````````````````````````` example
  5570. [link](#fragment)
  5571. [link](http://example.com#fragment)
  5572. [link](http://example.com?foo=3#frag)
  5573. .
  5574. <p><a href="#fragment">link</a></p>
  5575. <p><a href="http://example.com#fragment">link</a></p>
  5576. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5577. ````````````````````````````````
  5578. Note that a backslash before a non-escapable character is
  5579. just a backslash:
  5580. ```````````````````````````````` example
  5581. [link](foo\bar)
  5582. .
  5583. <p><a href="foo%5Cbar">link</a></p>
  5584. ````````````````````````````````
  5585. URL-escaping should be left alone inside the destination, as all
  5586. URL-escaped characters are also valid URL characters. Entity and
  5587. numerical character references in the destination will be parsed
  5588. into the corresponding Unicode code points, as usual. These may
  5589. be optionally URL-escaped when written as HTML, but this spec
  5590. does not enforce any particular policy for rendering URLs in
  5591. HTML or other formats. Renderers may make different decisions
  5592. about how to escape or normalize URLs in the output.
  5593. ```````````````````````````````` example
  5594. [link](foo%20b&auml;)
  5595. .
  5596. <p><a href="foo%20b%C3%A4">link</a></p>
  5597. ````````````````````````````````
  5598. Note that, because titles can often be parsed as destinations,
  5599. if you try to omit the destination and keep the title, you'll
  5600. get unexpected results:
  5601. ```````````````````````````````` example
  5602. [link]("title")
  5603. .
  5604. <p><a href="%22title%22">link</a></p>
  5605. ````````````````````````````````
  5606. Titles may be in single quotes, double quotes, or parentheses:
  5607. ```````````````````````````````` example
  5608. [link](/url "title")
  5609. [link](/url 'title')
  5610. [link](/url (title))
  5611. .
  5612. <p><a href="/url" title="title">link</a>
  5613. <a href="/url" title="title">link</a>
  5614. <a href="/url" title="title">link</a></p>
  5615. ````````````````````````````````
  5616. Backslash escapes and entity and numeric character references
  5617. may be used in titles:
  5618. ```````````````````````````````` example
  5619. [link](/url "title \"&quot;")
  5620. .
  5621. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5622. ````````````````````````````````
  5623. Titles must be separated from the link using a [whitespace].
  5624. Other [Unicode whitespace] like non-breaking space doesn't work.
  5625. ```````````````````````````````` example
  5626. [link](/url "title")
  5627. .
  5628. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5629. ````````````````````````````````
  5630. Nested balanced quotes are not allowed without escaping:
  5631. ```````````````````````````````` example
  5632. [link](/url "title "and" title")
  5633. .
  5634. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5635. ````````````````````````````````
  5636. But it is easy to work around this by using a different quote type:
  5637. ```````````````````````````````` example
  5638. [link](/url 'title "and" title')
  5639. .
  5640. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5641. ````````````````````````````````
  5642. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5643. title, and its test suite included a test demonstrating this.
  5644. But it is hard to see a good rationale for the extra complexity this
  5645. brings, since there are already many ways---backslash escaping,
  5646. entity and numeric character references, or using a different
  5647. quote type for the enclosing title---to write titles containing
  5648. double quotes. `Markdown.pl`'s handling of titles has a number
  5649. of other strange features. For example, it allows single-quoted
  5650. titles in inline links, but not reference links. And, in
  5651. reference links but not inline links, it allows a title to begin
  5652. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5653. titles with no closing quotation mark, though 1.0.2b8 does not.
  5654. It seems preferable to adopt a simple, rational rule that works
  5655. the same way in inline links and link reference definitions.)
  5656. [Whitespace] is allowed around the destination and title:
  5657. ```````````````````````````````` example
  5658. [link]( /uri
  5659. "title" )
  5660. .
  5661. <p><a href="/uri" title="title">link</a></p>
  5662. ````````````````````````````````
  5663. But it is not allowed between the link text and the
  5664. following parenthesis:
  5665. ```````````````````````````````` example
  5666. [link] (/uri)
  5667. .
  5668. <p>[link] (/uri)</p>
  5669. ````````````````````````````````
  5670. The link text may contain balanced brackets, but not unbalanced ones,
  5671. unless they are escaped:
  5672. ```````````````````````````````` example
  5673. [link [foo [bar]]](/uri)
  5674. .
  5675. <p><a href="/uri">link [foo [bar]]</a></p>
  5676. ````````````````````````````````
  5677. ```````````````````````````````` example
  5678. [link] bar](/uri)
  5679. .
  5680. <p>[link] bar](/uri)</p>
  5681. ````````````````````````````````
  5682. ```````````````````````````````` example
  5683. [link [bar](/uri)
  5684. .
  5685. <p>[link <a href="/uri">bar</a></p>
  5686. ````````````````````````````````
  5687. ```````````````````````````````` example
  5688. [link \[bar](/uri)
  5689. .
  5690. <p><a href="/uri">link [bar</a></p>
  5691. ````````````````````````````````
  5692. The link text may contain inline content:
  5693. ```````````````````````````````` example
  5694. [link *foo **bar** `#`*](/uri)
  5695. .
  5696. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5697. ````````````````````````````````
  5698. ```````````````````````````````` example
  5699. [![moon](moon.jpg)](/uri)
  5700. .
  5701. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5702. ````````````````````````````````
  5703. However, links may not contain other links, at any level of nesting.
  5704. ```````````````````````````````` example
  5705. [foo [bar](/uri)](/uri)
  5706. .
  5707. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5708. ````````````````````````````````
  5709. ```````````````````````````````` example
  5710. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5711. .
  5712. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5713. ````````````````````````````````
  5714. ```````````````````````````````` example
  5715. ![[[foo](uri1)](uri2)](uri3)
  5716. .
  5717. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5718. ````````````````````````````````
  5719. These cases illustrate the precedence of link text grouping over
  5720. emphasis grouping:
  5721. ```````````````````````````````` example
  5722. *[foo*](/uri)
  5723. .
  5724. <p>*<a href="/uri">foo*</a></p>
  5725. ````````````````````````````````
  5726. ```````````````````````````````` example
  5727. [foo *bar](baz*)
  5728. .
  5729. <p><a href="baz*">foo *bar</a></p>
  5730. ````````````````````````````````
  5731. Note that brackets that *aren't* part of links do not take
  5732. precedence:
  5733. ```````````````````````````````` example
  5734. *foo [bar* baz]
  5735. .
  5736. <p><em>foo [bar</em> baz]</p>
  5737. ````````````````````````````````
  5738. These cases illustrate the precedence of HTML tags, code spans,
  5739. and autolinks over link grouping:
  5740. ```````````````````````````````` example
  5741. [foo <bar attr="](baz)">
  5742. .
  5743. <p>[foo <bar attr="](baz)"></p>
  5744. ````````````````````````````````
  5745. ```````````````````````````````` example
  5746. [foo`](/uri)`
  5747. .
  5748. <p>[foo<code>](/uri)</code></p>
  5749. ````````````````````````````````
  5750. ```````````````````````````````` example
  5751. [foo<http://example.com/?search=](uri)>
  5752. .
  5753. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5754. ````````````````````````````````
  5755. There are three kinds of [reference link](@)s:
  5756. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5757. and [shortcut](#shortcut-reference-link).
  5758. A [full reference link](@)
  5759. consists of a [link text] immediately followed by a [link label]
  5760. that [matches] a [link reference definition] elsewhere in the document.
  5761. A [link label](@) begins with a left bracket (`[`) and ends
  5762. with the first right bracket (`]`) that is not backslash-escaped.
  5763. Between these brackets there must be at least one [non-whitespace character].
  5764. Unescaped square bracket characters are not allowed inside the
  5765. opening and closing square brackets of [link labels]. A link
  5766. label can have at most 999 characters inside the square
  5767. brackets.
  5768. One label [matches](@)
  5769. another just in case their normalized forms are equal. To normalize a
  5770. label, strip off the opening and closing brackets,
  5771. perform the *Unicode case fold*, strip leading and trailing
  5772. [whitespace] and collapse consecutive internal
  5773. [whitespace] to a single space. If there are multiple
  5774. matching reference link definitions, the one that comes first in the
  5775. document is used. (It is desirable in such cases to emit a warning.)
  5776. The contents of the first link label are parsed as inlines, which are
  5777. used as the link's text. The link's URI and title are provided by the
  5778. matching [link reference definition].
  5779. Here is a simple example:
  5780. ```````````````````````````````` example
  5781. [foo][bar]
  5782. [bar]: /url "title"
  5783. .
  5784. <p><a href="/url" title="title">foo</a></p>
  5785. ````````````````````````````````
  5786. The rules for the [link text] are the same as with
  5787. [inline links]. Thus:
  5788. The link text may contain balanced brackets, but not unbalanced ones,
  5789. unless they are escaped:
  5790. ```````````````````````````````` example
  5791. [link [foo [bar]]][ref]
  5792. [ref]: /uri
  5793. .
  5794. <p><a href="/uri">link [foo [bar]]</a></p>
  5795. ````````````````````````````````
  5796. ```````````````````````````````` example
  5797. [link \[bar][ref]
  5798. [ref]: /uri
  5799. .
  5800. <p><a href="/uri">link [bar</a></p>
  5801. ````````````````````````````````
  5802. The link text may contain inline content:
  5803. ```````````````````````````````` example
  5804. [link *foo **bar** `#`*][ref]
  5805. [ref]: /uri
  5806. .
  5807. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5808. ````````````````````````````````
  5809. ```````````````````````````````` example
  5810. [![moon](moon.jpg)][ref]
  5811. [ref]: /uri
  5812. .
  5813. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5814. ````````````````````````````````
  5815. However, links may not contain other links, at any level of nesting.
  5816. ```````````````````````````````` example
  5817. [foo [bar](/uri)][ref]
  5818. [ref]: /uri
  5819. .
  5820. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5821. ````````````````````````````````
  5822. ```````````````````````````````` example
  5823. [foo *bar [baz][ref]*][ref]
  5824. [ref]: /uri
  5825. .
  5826. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5827. ````````````````````````````````
  5828. (In the examples above, we have two [shortcut reference links]
  5829. instead of one [full reference link].)
  5830. The following cases illustrate the precedence of link text grouping over
  5831. emphasis grouping:
  5832. ```````````````````````````````` example
  5833. *[foo*][ref]
  5834. [ref]: /uri
  5835. .
  5836. <p>*<a href="/uri">foo*</a></p>
  5837. ````````````````````````````````
  5838. ```````````````````````````````` example
  5839. [foo *bar][ref]
  5840. [ref]: /uri
  5841. .
  5842. <p><a href="/uri">foo *bar</a></p>
  5843. ````````````````````````````````
  5844. These cases illustrate the precedence of HTML tags, code spans,
  5845. and autolinks over link grouping:
  5846. ```````````````````````````````` example
  5847. [foo <bar attr="][ref]">
  5848. [ref]: /uri
  5849. .
  5850. <p>[foo <bar attr="][ref]"></p>
  5851. ````````````````````````````````
  5852. ```````````````````````````````` example
  5853. [foo`][ref]`
  5854. [ref]: /uri
  5855. .
  5856. <p>[foo<code>][ref]</code></p>
  5857. ````````````````````````````````
  5858. ```````````````````````````````` example
  5859. [foo<http://example.com/?search=][ref]>
  5860. [ref]: /uri
  5861. .
  5862. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5863. ````````````````````````````````
  5864. Matching is case-insensitive:
  5865. ```````````````````````````````` example
  5866. [foo][BaR]
  5867. [bar]: /url "title"
  5868. .
  5869. <p><a href="/url" title="title">foo</a></p>
  5870. ````````````````````````````````
  5871. Unicode case fold is used:
  5872. ```````````````````````````````` example
  5873. [Толпой][Толпой] is a Russian word.
  5874. [ТОЛПОЙ]: /url
  5875. .
  5876. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5877. ````````````````````````````````
  5878. Consecutive internal [whitespace] is treated as one space for
  5879. purposes of determining matching:
  5880. ```````````````````````````````` example
  5881. [Foo
  5882. bar]: /url
  5883. [Baz][Foo bar]
  5884. .
  5885. <p><a href="/url">Baz</a></p>
  5886. ````````````````````````````````
  5887. No [whitespace] is allowed between the [link text] and the
  5888. [link label]:
  5889. ```````````````````````````````` example
  5890. [foo] [bar]
  5891. [bar]: /url "title"
  5892. .
  5893. <p>[foo] <a href="/url" title="title">bar</a></p>
  5894. ````````````````````````````````
  5895. ```````````````````````````````` example
  5896. [foo]
  5897. [bar]
  5898. [bar]: /url "title"
  5899. .
  5900. <p>[foo]
  5901. <a href="/url" title="title">bar</a></p>
  5902. ````````````````````````````````
  5903. This is a departure from John Gruber's original Markdown syntax
  5904. description, which explicitly allows whitespace between the link
  5905. text and the link label. It brings reference links in line with
  5906. [inline links], which (according to both original Markdown and
  5907. this spec) cannot have whitespace after the link text. More
  5908. importantly, it prevents inadvertent capture of consecutive
  5909. [shortcut reference links]. If whitespace is allowed between the
  5910. link text and the link label, then in the following we will have
  5911. a single reference link, not two shortcut reference links, as
  5912. intended:
  5913. ``` markdown
  5914. [foo]
  5915. [bar]
  5916. [foo]: /url1
  5917. [bar]: /url2
  5918. ```
  5919. (Note that [shortcut reference links] were introduced by Gruber
  5920. himself in a beta version of `Markdown.pl`, but never included
  5921. in the official syntax description. Without shortcut reference
  5922. links, it is harmless to allow space between the link text and
  5923. link label; but once shortcut references are introduced, it is
  5924. too dangerous to allow this, as it frequently leads to
  5925. unintended results.)
  5926. When there are multiple matching [link reference definitions],
  5927. the first is used:
  5928. ```````````````````````````````` example
  5929. [foo]: /url1
  5930. [foo]: /url2
  5931. [bar][foo]
  5932. .
  5933. <p><a href="/url1">bar</a></p>
  5934. ````````````````````````````````
  5935. Note that matching is performed on normalized strings, not parsed
  5936. inline content. So the following does not match, even though the
  5937. labels define equivalent inline content:
  5938. ```````````````````````````````` example
  5939. [bar][foo\!]
  5940. [foo!]: /url
  5941. .
  5942. <p>[bar][foo!]</p>
  5943. ````````````````````````````````
  5944. [Link labels] cannot contain brackets, unless they are
  5945. backslash-escaped:
  5946. ```````````````````````````````` example
  5947. [foo][ref[]
  5948. [ref[]: /uri
  5949. .
  5950. <p>[foo][ref[]</p>
  5951. <p>[ref[]: /uri</p>
  5952. ````````````````````````````````
  5953. ```````````````````````````````` example
  5954. [foo][ref[bar]]
  5955. [ref[bar]]: /uri
  5956. .
  5957. <p>[foo][ref[bar]]</p>
  5958. <p>[ref[bar]]: /uri</p>
  5959. ````````````````````````````````
  5960. ```````````````````````````````` example
  5961. [[[foo]]]
  5962. [[[foo]]]: /url
  5963. .
  5964. <p>[[[foo]]]</p>
  5965. <p>[[[foo]]]: /url</p>
  5966. ````````````````````````````````
  5967. ```````````````````````````````` example
  5968. [foo][ref\[]
  5969. [ref\[]: /uri
  5970. .
  5971. <p><a href="/uri">foo</a></p>
  5972. ````````````````````````````````
  5973. Note that in this example `]` is not backslash-escaped:
  5974. ```````````````````````````````` example
  5975. [bar\\]: /uri
  5976. [bar\\]
  5977. .
  5978. <p><a href="/uri">bar\</a></p>
  5979. ````````````````````````````````
  5980. A [link label] must contain at least one [non-whitespace character]:
  5981. ```````````````````````````````` example
  5982. []
  5983. []: /uri
  5984. .
  5985. <p>[]</p>
  5986. <p>[]: /uri</p>
  5987. ````````````````````````````````
  5988. ```````````````````````````````` example
  5989. [
  5990. ]
  5991. [
  5992. ]: /uri
  5993. .
  5994. <p>[
  5995. ]</p>
  5996. <p>[
  5997. ]: /uri</p>
  5998. ````````````````````````````````
  5999. A [collapsed reference link](@)
  6000. consists of a [link label] that [matches] a
  6001. [link reference definition] elsewhere in the
  6002. document, followed by the string `[]`.
  6003. The contents of the first link label are parsed as inlines,
  6004. which are used as the link's text. The link's URI and title are
  6005. provided by the matching reference link definition. Thus,
  6006. `[foo][]` is equivalent to `[foo][foo]`.
  6007. ```````````````````````````````` example
  6008. [foo][]
  6009. [foo]: /url "title"
  6010. .
  6011. <p><a href="/url" title="title">foo</a></p>
  6012. ````````````````````````````````
  6013. ```````````````````````````````` example
  6014. [*foo* bar][]
  6015. [*foo* bar]: /url "title"
  6016. .
  6017. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6018. ````````````````````````````````
  6019. The link labels are case-insensitive:
  6020. ```````````````````````````````` example
  6021. [Foo][]
  6022. [foo]: /url "title"
  6023. .
  6024. <p><a href="/url" title="title">Foo</a></p>
  6025. ````````````````````````````````
  6026. As with full reference links, [whitespace] is not
  6027. allowed between the two sets of brackets:
  6028. ```````````````````````````````` example
  6029. [foo]
  6030. []
  6031. [foo]: /url "title"
  6032. .
  6033. <p><a href="/url" title="title">foo</a>
  6034. []</p>
  6035. ````````````````````````````````
  6036. A [shortcut reference link](@)
  6037. consists of a [link label] that [matches] a
  6038. [link reference definition] elsewhere in the
  6039. document and is not followed by `[]` or a link label.
  6040. The contents of the first link label are parsed as inlines,
  6041. which are used as the link's text. The link's URI and title
  6042. are provided by the matching link reference definition.
  6043. Thus, `[foo]` is equivalent to `[foo][]`.
  6044. ```````````````````````````````` example
  6045. [foo]
  6046. [foo]: /url "title"
  6047. .
  6048. <p><a href="/url" title="title">foo</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. [[*foo* bar]]
  6058. [*foo* bar]: /url "title"
  6059. .
  6060. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6061. ````````````````````````````````
  6062. ```````````````````````````````` example
  6063. [[bar [foo]
  6064. [foo]: /url
  6065. .
  6066. <p>[[bar <a href="/url">foo</a></p>
  6067. ````````````````````````````````
  6068. The link labels are case-insensitive:
  6069. ```````````````````````````````` example
  6070. [Foo]
  6071. [foo]: /url "title"
  6072. .
  6073. <p><a href="/url" title="title">Foo</a></p>
  6074. ````````````````````````````````
  6075. A space after the link text should be preserved:
  6076. ```````````````````````````````` example
  6077. [foo] bar
  6078. [foo]: /url
  6079. .
  6080. <p><a href="/url">foo</a> bar</p>
  6081. ````````````````````````````````
  6082. If you just want bracketed text, you can backslash-escape the
  6083. opening bracket to avoid links:
  6084. ```````````````````````````````` example
  6085. \[foo]
  6086. [foo]: /url "title"
  6087. .
  6088. <p>[foo]</p>
  6089. ````````````````````````````````
  6090. Note that this is a link, because a link label ends with the first
  6091. following closing bracket:
  6092. ```````````````````````````````` example
  6093. [foo*]: /url
  6094. *[foo*]
  6095. .
  6096. <p>*<a href="/url">foo*</a></p>
  6097. ````````````````````````````````
  6098. Full and compact references take precedence over shortcut
  6099. references:
  6100. ```````````````````````````````` example
  6101. [foo][bar]
  6102. [foo]: /url1
  6103. [bar]: /url2
  6104. .
  6105. <p><a href="/url2">foo</a></p>
  6106. ````````````````````````````````
  6107. ```````````````````````````````` example
  6108. [foo][]
  6109. [foo]: /url1
  6110. .
  6111. <p><a href="/url1">foo</a></p>
  6112. ````````````````````````````````
  6113. Inline links also take precedence:
  6114. ```````````````````````````````` example
  6115. [foo]()
  6116. [foo]: /url1
  6117. .
  6118. <p><a href="">foo</a></p>
  6119. ````````````````````````````````
  6120. ```````````````````````````````` example
  6121. [foo](not a link)
  6122. [foo]: /url1
  6123. .
  6124. <p><a href="/url1">foo</a>(not a link)</p>
  6125. ````````````````````````````````
  6126. In the following case `[bar][baz]` is parsed as a reference,
  6127. `[foo]` as normal text:
  6128. ```````````````````````````````` example
  6129. [foo][bar][baz]
  6130. [baz]: /url
  6131. .
  6132. <p>[foo]<a href="/url">bar</a></p>
  6133. ````````````````````````````````
  6134. Here, though, `[foo][bar]` is parsed as a reference, since
  6135. `[bar]` is defined:
  6136. ```````````````````````````````` example
  6137. [foo][bar][baz]
  6138. [baz]: /url1
  6139. [bar]: /url2
  6140. .
  6141. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6142. ````````````````````````````````
  6143. Here `[foo]` is not parsed as a shortcut reference, because it
  6144. is followed by a link label (even though `[bar]` is not defined):
  6145. ```````````````````````````````` example
  6146. [foo][bar][baz]
  6147. [baz]: /url1
  6148. [foo]: /url2
  6149. .
  6150. <p>[foo]<a href="/url1">bar</a></p>
  6151. ````````````````````````````````
  6152. ## Images
  6153. Syntax for images is like the syntax for links, with one
  6154. difference. Instead of [link text], we have an
  6155. [image description](@). The rules for this are the
  6156. same as for [link text], except that (a) an
  6157. image description starts with `![` rather than `[`, and
  6158. (b) an image description may contain links.
  6159. An image description has inline elements
  6160. as its contents. When an image is rendered to HTML,
  6161. this is standardly used as the image's `alt` attribute.
  6162. ```````````````````````````````` example
  6163. ![foo](/url "title")
  6164. .
  6165. <p><img src="/url" alt="foo" title="title" /></p>
  6166. ````````````````````````````````
  6167. ```````````````````````````````` example
  6168. ![foo *bar*]
  6169. [foo *bar*]: train.jpg "train & tracks"
  6170. .
  6171. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6172. ````````````````````````````````
  6173. ```````````````````````````````` example
  6174. ![foo ![bar](/url)](/url2)
  6175. .
  6176. <p><img src="/url2" alt="foo bar" /></p>
  6177. ````````````````````````````````
  6178. ```````````````````````````````` example
  6179. ![foo [bar](/url)](/url2)
  6180. .
  6181. <p><img src="/url2" alt="foo bar" /></p>
  6182. ````````````````````````````````
  6183. Though this spec is concerned with parsing, not rendering, it is
  6184. recommended that in rendering to HTML, only the plain string content
  6185. of the [image description] be used. Note that in
  6186. the above example, the alt attribute's value is `foo bar`, not `foo
  6187. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6188. content is rendered, without formatting.
  6189. ```````````````````````````````` example
  6190. ![foo *bar*][]
  6191. [foo *bar*]: train.jpg "train & tracks"
  6192. .
  6193. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6194. ````````````````````````````````
  6195. ```````````````````````````````` example
  6196. ![foo *bar*][foobar]
  6197. [FOOBAR]: train.jpg "train & tracks"
  6198. .
  6199. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6200. ````````````````````````````````
  6201. ```````````````````````````````` example
  6202. ![foo](train.jpg)
  6203. .
  6204. <p><img src="train.jpg" alt="foo" /></p>
  6205. ````````````````````````````````
  6206. ```````````````````````````````` example
  6207. My ![foo bar](/path/to/train.jpg "title" )
  6208. .
  6209. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6210. ````````````````````````````````
  6211. ```````````````````````````````` example
  6212. ![foo](<url>)
  6213. .
  6214. <p><img src="url" alt="foo" /></p>
  6215. ````````````````````````````````
  6216. ```````````````````````````````` example
  6217. ![](/url)
  6218. .
  6219. <p><img src="/url" alt="" /></p>
  6220. ````````````````````````````````
  6221. Reference-style:
  6222. ```````````````````````````````` example
  6223. ![foo][bar]
  6224. [bar]: /url
  6225. .
  6226. <p><img src="/url" alt="foo" /></p>
  6227. ````````````````````````````````
  6228. ```````````````````````````````` example
  6229. ![foo][bar]
  6230. [BAR]: /url
  6231. .
  6232. <p><img src="/url" alt="foo" /></p>
  6233. ````````````````````````````````
  6234. Collapsed:
  6235. ```````````````````````````````` example
  6236. ![foo][]
  6237. [foo]: /url "title"
  6238. .
  6239. <p><img src="/url" alt="foo" title="title" /></p>
  6240. ````````````````````````````````
  6241. ```````````````````````````````` example
  6242. ![*foo* bar][]
  6243. [*foo* bar]: /url "title"
  6244. .
  6245. <p><img src="/url" alt="foo bar" title="title" /></p>
  6246. ````````````````````````````````
  6247. The labels are case-insensitive:
  6248. ```````````````````````````````` example
  6249. ![Foo][]
  6250. [foo]: /url "title"
  6251. .
  6252. <p><img src="/url" alt="Foo" title="title" /></p>
  6253. ````````````````````````````````
  6254. As with reference links, [whitespace] is not allowed
  6255. between the two sets of brackets:
  6256. ```````````````````````````````` example
  6257. ![foo]
  6258. []
  6259. [foo]: /url "title"
  6260. .
  6261. <p><img src="/url" alt="foo" title="title" />
  6262. []</p>
  6263. ````````````````````````````````
  6264. Shortcut:
  6265. ```````````````````````````````` example
  6266. ![foo]
  6267. [foo]: /url "title"
  6268. .
  6269. <p><img src="/url" alt="foo" title="title" /></p>
  6270. ````````````````````````````````
  6271. ```````````````````````````````` example
  6272. ![*foo* bar]
  6273. [*foo* bar]: /url "title"
  6274. .
  6275. <p><img src="/url" alt="foo bar" title="title" /></p>
  6276. ````````````````````````````````
  6277. Note that link labels cannot contain unescaped brackets:
  6278. ```````````````````````````````` example
  6279. ![[foo]]
  6280. [[foo]]: /url "title"
  6281. .
  6282. <p>![[foo]]</p>
  6283. <p>[[foo]]: /url &quot;title&quot;</p>
  6284. ````````````````````````````````
  6285. The link labels are case-insensitive:
  6286. ```````````````````````````````` example
  6287. ![Foo]
  6288. [foo]: /url "title"
  6289. .
  6290. <p><img src="/url" alt="Foo" title="title" /></p>
  6291. ````````````````````````````````
  6292. If you just want a literal `!` followed by bracketed text, you can
  6293. backslash-escape the opening `[`:
  6294. ```````````````````````````````` example
  6295. !\[foo]
  6296. [foo]: /url "title"
  6297. .
  6298. <p>![foo]</p>
  6299. ````````````````````````````````
  6300. If you want a link after a literal `!`, backslash-escape the
  6301. `!`:
  6302. ```````````````````````````````` example
  6303. \![foo]
  6304. [foo]: /url "title"
  6305. .
  6306. <p>!<a href="/url" title="title">foo</a></p>
  6307. ````````````````````````````````
  6308. ## Autolinks
  6309. [Autolink](@)s are absolute URIs and email addresses inside
  6310. `<` and `>`. They are parsed as links, with the URL or email address
  6311. as the link label.
  6312. A [URI autolink](@) consists of `<`, followed by an
  6313. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6314. a link to the URI, with the URI as the link's label.
  6315. An [absolute URI](@),
  6316. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6317. followed by zero or more characters other than ASCII
  6318. [whitespace] and control characters, `<`, and `>`. If
  6319. the URI includes these characters, they must be percent-encoded
  6320. (e.g. `%20` for a space).
  6321. For purposes of this spec, a [scheme](@) is any sequence
  6322. of 2--32 characters beginning with an ASCII letter and followed
  6323. by any combination of ASCII letters, digits, or the symbols plus
  6324. ("+"), period ("."), or hyphen ("-").
  6325. Here are some valid autolinks:
  6326. ```````````````````````````````` example
  6327. <http://foo.bar.baz>
  6328. .
  6329. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6330. ````````````````````````````````
  6331. ```````````````````````````````` example
  6332. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6333. .
  6334. <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>
  6335. ````````````````````````````````
  6336. ```````````````````````````````` example
  6337. <irc://foo.bar:2233/baz>
  6338. .
  6339. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6340. ````````````````````````````````
  6341. Uppercase is also fine:
  6342. ```````````````````````````````` example
  6343. <MAILTO:FOO@BAR.BAZ>
  6344. .
  6345. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6346. ````````````````````````````````
  6347. Note that many strings that count as [absolute URIs] for
  6348. purposes of this spec are not valid URIs, because their
  6349. schemes are not registered or because of other problems
  6350. with their syntax:
  6351. ```````````````````````````````` example
  6352. <a+b+c:d>
  6353. .
  6354. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6355. ````````````````````````````````
  6356. ```````````````````````````````` example
  6357. <made-up-scheme://foo,bar>
  6358. .
  6359. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6360. ````````````````````````````````
  6361. ```````````````````````````````` example
  6362. <http://../>
  6363. .
  6364. <p><a href="http://../">http://../</a></p>
  6365. ````````````````````````````````
  6366. ```````````````````````````````` example
  6367. <localhost:5001/foo>
  6368. .
  6369. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6370. ````````````````````````````````
  6371. Spaces are not allowed in autolinks:
  6372. ```````````````````````````````` example
  6373. <http://foo.bar/baz bim>
  6374. .
  6375. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6376. ````````````````````````````````
  6377. Backslash-escapes do not work inside autolinks:
  6378. ```````````````````````````````` example
  6379. <http://example.com/\[\>
  6380. .
  6381. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6382. ````````````````````````````````
  6383. An [email autolink](@)
  6384. consists of `<`, followed by an [email address],
  6385. followed by `>`. The link's label is the email address,
  6386. and the URL is `mailto:` followed by the email address.
  6387. An [email address](@),
  6388. for these purposes, is anything that matches
  6389. the [non-normative regex from the HTML5
  6390. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6391. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6392. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6393. Examples of email autolinks:
  6394. ```````````````````````````````` example
  6395. <foo@bar.example.com>
  6396. .
  6397. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6398. ````````````````````````````````
  6399. ```````````````````````````````` example
  6400. <foo+special@Bar.baz-bar0.com>
  6401. .
  6402. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6403. ````````````````````````````````
  6404. Backslash-escapes do not work inside email autolinks:
  6405. ```````````````````````````````` example
  6406. <foo\+@bar.example.com>
  6407. .
  6408. <p>&lt;foo+@bar.example.com&gt;</p>
  6409. ````````````````````````````````
  6410. These are not autolinks:
  6411. ```````````````````````````````` example
  6412. <>
  6413. .
  6414. <p>&lt;&gt;</p>
  6415. ````````````````````````````````
  6416. ```````````````````````````````` example
  6417. < http://foo.bar >
  6418. .
  6419. <p>&lt; http://foo.bar &gt;</p>
  6420. ````````````````````````````````
  6421. ```````````````````````````````` example
  6422. <m:abc>
  6423. .
  6424. <p>&lt;m:abc&gt;</p>
  6425. ````````````````````````````````
  6426. ```````````````````````````````` example
  6427. <foo.bar.baz>
  6428. .
  6429. <p>&lt;foo.bar.baz&gt;</p>
  6430. ````````````````````````````````
  6431. ```````````````````````````````` example
  6432. http://example.com
  6433. .
  6434. <p>http://example.com</p>
  6435. ````````````````````````````````
  6436. ```````````````````````````````` example
  6437. foo@bar.example.com
  6438. .
  6439. <p>foo@bar.example.com</p>
  6440. ````````````````````````````````
  6441. ## Raw HTML
  6442. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6443. raw HTML tag and will be rendered in HTML without escaping.
  6444. Tag and attribute names are not limited to current HTML tags,
  6445. so custom tags (and even, say, DocBook tags) may be used.
  6446. Here is the grammar for tags:
  6447. A [tag name](@) consists of an ASCII letter
  6448. followed by zero or more ASCII letters, digits, or
  6449. hyphens (`-`).
  6450. An [attribute](@) consists of [whitespace],
  6451. an [attribute name], and an optional
  6452. [attribute value specification].
  6453. An [attribute name](@)
  6454. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6455. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6456. specification restricted to ASCII. HTML5 is laxer.)
  6457. An [attribute value specification](@)
  6458. consists of optional [whitespace],
  6459. a `=` character, optional [whitespace], and an [attribute
  6460. value].
  6461. An [attribute value](@)
  6462. consists of an [unquoted attribute value],
  6463. a [single-quoted attribute value], or a [double-quoted attribute value].
  6464. An [unquoted attribute value](@)
  6465. is a nonempty string of characters not
  6466. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6467. A [single-quoted attribute value](@)
  6468. consists of `'`, zero or more
  6469. characters not including `'`, and a final `'`.
  6470. A [double-quoted attribute value](@)
  6471. consists of `"`, zero or more
  6472. characters not including `"`, and a final `"`.
  6473. An [open tag](@) consists of a `<` character, a [tag name],
  6474. zero or more [attributes], optional [whitespace], an optional `/`
  6475. character, and a `>` character.
  6476. A [closing tag](@) consists of the string `</`, a
  6477. [tag name], optional [whitespace], and the character `>`.
  6478. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6479. where *text* does not start with `>` or `->`, does not end with `-`,
  6480. and does not contain `--`. (See the
  6481. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6482. A [processing instruction](@)
  6483. consists of the string `<?`, a string
  6484. of characters not including the string `?>`, and the string
  6485. `?>`.
  6486. A [declaration](@) consists of the
  6487. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6488. [whitespace], a string of characters not including the
  6489. character `>`, and the character `>`.
  6490. A [CDATA section](@) consists of
  6491. the string `<![CDATA[`, a string of characters not including the string
  6492. `]]>`, and the string `]]>`.
  6493. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6494. an [HTML comment], a [processing instruction], a [declaration],
  6495. or a [CDATA section].
  6496. Here are some simple open tags:
  6497. ```````````````````````````````` example
  6498. <a><bab><c2c>
  6499. .
  6500. <p><a><bab><c2c></p>
  6501. ````````````````````````````````
  6502. Empty elements:
  6503. ```````````````````````````````` example
  6504. <a/><b2/>
  6505. .
  6506. <p><a/><b2/></p>
  6507. ````````````````````````````````
  6508. [Whitespace] is allowed:
  6509. ```````````````````````````````` example
  6510. <a /><b2
  6511. data="foo" >
  6512. .
  6513. <p><a /><b2
  6514. data="foo" ></p>
  6515. ````````````````````````````````
  6516. With attributes:
  6517. ```````````````````````````````` example
  6518. <a foo="bar" bam = 'baz <em>"</em>'
  6519. _boolean zoop:33=zoop:33 />
  6520. .
  6521. <p><a foo="bar" bam = 'baz <em>"</em>'
  6522. _boolean zoop:33=zoop:33 /></p>
  6523. ````````````````````````````````
  6524. Custom tag names can be used:
  6525. ```````````````````````````````` example
  6526. Foo <responsive-image src="foo.jpg" />
  6527. .
  6528. <p>Foo <responsive-image src="foo.jpg" /></p>
  6529. ````````````````````````````````
  6530. Illegal tag names, not parsed as HTML:
  6531. ```````````````````````````````` example
  6532. <33> <__>
  6533. .
  6534. <p>&lt;33&gt; &lt;__&gt;</p>
  6535. ````````````````````````````````
  6536. Illegal attribute names:
  6537. ```````````````````````````````` example
  6538. <a h*#ref="hi">
  6539. .
  6540. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6541. ````````````````````````````````
  6542. Illegal attribute values:
  6543. ```````````````````````````````` example
  6544. <a href="hi'> <a href=hi'>
  6545. .
  6546. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6547. ````````````````````````````````
  6548. Illegal [whitespace]:
  6549. ```````````````````````````````` example
  6550. < a><
  6551. foo><bar/ >
  6552. .
  6553. <p>&lt; a&gt;&lt;
  6554. foo&gt;&lt;bar/ &gt;</p>
  6555. ````````````````````````````````
  6556. Missing [whitespace]:
  6557. ```````````````````````````````` example
  6558. <a href='bar'title=title>
  6559. .
  6560. <p>&lt;a href='bar'title=title&gt;</p>
  6561. ````````````````````````````````
  6562. Closing tags:
  6563. ```````````````````````````````` example
  6564. </a></foo >
  6565. .
  6566. <p></a></foo ></p>
  6567. ````````````````````````````````
  6568. Illegal attributes in closing tag:
  6569. ```````````````````````````````` example
  6570. </a href="foo">
  6571. .
  6572. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6573. ````````````````````````````````
  6574. Comments:
  6575. ```````````````````````````````` example
  6576. foo <!-- this is a
  6577. comment - with hyphen -->
  6578. .
  6579. <p>foo <!-- this is a
  6580. comment - with hyphen --></p>
  6581. ````````````````````````````````
  6582. ```````````````````````````````` example
  6583. foo <!-- not a comment -- two hyphens -->
  6584. .
  6585. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6586. ````````````````````````````````
  6587. Not comments:
  6588. ```````````````````````````````` example
  6589. foo <!--> foo -->
  6590. foo <!-- foo--->
  6591. .
  6592. <p>foo &lt;!--&gt; foo --&gt;</p>
  6593. <p>foo &lt;!-- foo---&gt;</p>
  6594. ````````````````````````````````
  6595. Processing instructions:
  6596. ```````````````````````````````` example
  6597. foo <?php echo $a; ?>
  6598. .
  6599. <p>foo <?php echo $a; ?></p>
  6600. ````````````````````````````````
  6601. Declarations:
  6602. ```````````````````````````````` example
  6603. foo <!ELEMENT br EMPTY>
  6604. .
  6605. <p>foo <!ELEMENT br EMPTY></p>
  6606. ````````````````````````````````
  6607. CDATA sections:
  6608. ```````````````````````````````` example
  6609. foo <![CDATA[>&<]]>
  6610. .
  6611. <p>foo <![CDATA[>&<]]></p>
  6612. ````````````````````````````````
  6613. Entity and numeric character references are preserved in HTML
  6614. attributes:
  6615. ```````````````````````````````` example
  6616. foo <a href="&ouml;">
  6617. .
  6618. <p>foo <a href="&ouml;"></p>
  6619. ````````````````````````````````
  6620. Backslash escapes do not work in HTML attributes:
  6621. ```````````````````````````````` example
  6622. foo <a href="\*">
  6623. .
  6624. <p>foo <a href="\*"></p>
  6625. ````````````````````````````````
  6626. ```````````````````````````````` example
  6627. <a href="\"">
  6628. .
  6629. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6630. ````````````````````````````````
  6631. ## Hard line breaks
  6632. A line break (not in a code span or HTML tag) that is preceded
  6633. by two or more spaces and does not occur at the end of a block
  6634. is parsed as a [hard line break](@) (rendered
  6635. in HTML as a `<br />` tag):
  6636. ```````````````````````````````` example
  6637. foo
  6638. baz
  6639. .
  6640. <p>foo<br />
  6641. baz</p>
  6642. ````````````````````````````````
  6643. For a more visible alternative, a backslash before the
  6644. [line ending] may be used instead of two spaces:
  6645. ```````````````````````````````` example
  6646. foo\
  6647. baz
  6648. .
  6649. <p>foo<br />
  6650. baz</p>
  6651. ````````````````````````````````
  6652. More than two spaces can be used:
  6653. ```````````````````````````````` example
  6654. foo
  6655. baz
  6656. .
  6657. <p>foo<br />
  6658. baz</p>
  6659. ````````````````````````````````
  6660. Leading spaces at the beginning of the next line are ignored:
  6661. ```````````````````````````````` example
  6662. foo
  6663. bar
  6664. .
  6665. <p>foo<br />
  6666. bar</p>
  6667. ````````````````````````````````
  6668. ```````````````````````````````` example
  6669. foo\
  6670. bar
  6671. .
  6672. <p>foo<br />
  6673. bar</p>
  6674. ````````````````````````````````
  6675. Line breaks can occur inside emphasis, links, and other constructs
  6676. that allow inline content:
  6677. ```````````````````````````````` example
  6678. *foo
  6679. bar*
  6680. .
  6681. <p><em>foo<br />
  6682. bar</em></p>
  6683. ````````````````````````````````
  6684. ```````````````````````````````` example
  6685. *foo\
  6686. bar*
  6687. .
  6688. <p><em>foo<br />
  6689. bar</em></p>
  6690. ````````````````````````````````
  6691. Line breaks do not occur inside code spans
  6692. ```````````````````````````````` example
  6693. `code
  6694. span`
  6695. .
  6696. <p><code>code span</code></p>
  6697. ````````````````````````````````
  6698. ```````````````````````````````` example
  6699. `code\
  6700. span`
  6701. .
  6702. <p><code>code\ span</code></p>
  6703. ````````````````````````````````
  6704. or HTML tags:
  6705. ```````````````````````````````` example
  6706. <a href="foo
  6707. bar">
  6708. .
  6709. <p><a href="foo
  6710. bar"></p>
  6711. ````````````````````````````````
  6712. ```````````````````````````````` example
  6713. <a href="foo\
  6714. bar">
  6715. .
  6716. <p><a href="foo\
  6717. bar"></p>
  6718. ````````````````````````````````
  6719. Hard line breaks are for separating inline content within a block.
  6720. Neither syntax for hard line breaks works at the end of a paragraph or
  6721. other block element:
  6722. ```````````````````````````````` example
  6723. foo\
  6724. .
  6725. <p>foo\</p>
  6726. ````````````````````````````````
  6727. ```````````````````````````````` example
  6728. foo
  6729. .
  6730. <p>foo</p>
  6731. ````````````````````````````````
  6732. ```````````````````````````````` example
  6733. ### foo\
  6734. .
  6735. <h3>foo\</h3>
  6736. ````````````````````````````````
  6737. ```````````````````````````````` example
  6738. ### foo
  6739. .
  6740. <h3>foo</h3>
  6741. ````````````````````````````````
  6742. ## Soft line breaks
  6743. A regular line break (not in a code span or HTML tag) that is not
  6744. preceded by two or more spaces or a backslash is parsed as a
  6745. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6746. [line ending] or as a space. The result will be the same in
  6747. browsers. In the examples here, a [line ending] will be used.)
  6748. ```````````````````````````````` example
  6749. foo
  6750. baz
  6751. .
  6752. <p>foo
  6753. baz</p>
  6754. ````````````````````````````````
  6755. Spaces at the end of the line and beginning of the next line are
  6756. removed:
  6757. ```````````````````````````````` example
  6758. foo
  6759. baz
  6760. .
  6761. <p>foo
  6762. baz</p>
  6763. ````````````````````````````````
  6764. A conforming parser may render a soft line break in HTML either as a
  6765. line break or as a space.
  6766. A renderer may also provide an option to render soft line breaks
  6767. as hard line breaks.
  6768. ## Textual content
  6769. Any characters not given an interpretation by the above rules will
  6770. be parsed as plain textual content.
  6771. ```````````````````````````````` example
  6772. hello $.;'there
  6773. .
  6774. <p>hello $.;'there</p>
  6775. ````````````````````````````````
  6776. ```````````````````````````````` example
  6777. Foo χρῆν
  6778. .
  6779. <p>Foo χρῆν</p>
  6780. ````````````````````````````````
  6781. Internal spaces are preserved verbatim:
  6782. ```````````````````````````````` example
  6783. Multiple spaces
  6784. .
  6785. <p>Multiple spaces</p>
  6786. ````````````````````````````````
  6787. <!-- END TESTS -->
  6788. # Appendix: A parsing strategy
  6789. In this appendix we describe some features of the parsing strategy
  6790. used in the CommonMark reference implementations.
  6791. ## Overview
  6792. Parsing has two phases:
  6793. 1. In the first phase, lines of input are consumed and the block
  6794. structure of the document---its division into paragraphs, block quotes,
  6795. list items, and so on---is constructed. Text is assigned to these
  6796. blocks but not parsed. Link reference definitions are parsed and a
  6797. map of links is constructed.
  6798. 2. In the second phase, the raw text contents of paragraphs and headings
  6799. are parsed into sequences of Markdown inline elements (strings,
  6800. code spans, links, emphasis, and so on), using the map of link
  6801. references constructed in phase 1.
  6802. At each point in processing, the document is represented as a tree of
  6803. **blocks**. The root of the tree is a `document` block. The `document`
  6804. may have any number of other blocks as **children**. These children
  6805. may, in turn, have other blocks as children. The last child of a block
  6806. is normally considered **open**, meaning that subsequent lines of input
  6807. can alter its contents. (Blocks that are not open are **closed**.)
  6808. Here, for example, is a possible document tree, with the open blocks
  6809. marked by arrows:
  6810. ``` tree
  6811. -> document
  6812. -> block_quote
  6813. paragraph
  6814. "Lorem ipsum dolor\nsit amet."
  6815. -> list (type=bullet tight=true bullet_char=-)
  6816. list_item
  6817. paragraph
  6818. "Qui *quodsi iracundia*"
  6819. -> list_item
  6820. -> paragraph
  6821. "aliquando id"
  6822. ```
  6823. ## Phase 1: block structure
  6824. Each line that is processed has an effect on this tree. The line is
  6825. analyzed and, depending on its contents, the document may be altered
  6826. in one or more of the following ways:
  6827. 1. One or more open blocks may be closed.
  6828. 2. One or more new blocks may be created as children of the
  6829. last open block.
  6830. 3. Text may be added to the last (deepest) open block remaining
  6831. on the tree.
  6832. Once a line has been incorporated into the tree in this way,
  6833. it can be discarded, so input can be read in a stream.
  6834. For each line, we follow this procedure:
  6835. 1. First we iterate through the open blocks, starting with the
  6836. root document, and descending through last children down to the last
  6837. open block. Each block imposes a condition that the line must satisfy
  6838. if the block is to remain open. For example, a block quote requires a
  6839. `>` character. A paragraph requires a non-blank line.
  6840. In this phase we may match all or just some of the open
  6841. blocks. But we cannot close unmatched blocks yet, because we may have a
  6842. [lazy continuation line].
  6843. 2. Next, after consuming the continuation markers for existing
  6844. blocks, we look for new block starts (e.g. `>` for a block quote).
  6845. If we encounter a new block start, we close any blocks unmatched
  6846. in step 1 before creating the new block as a child of the last
  6847. matched block.
  6848. 3. Finally, we look at the remainder of the line (after block
  6849. markers like `>`, list markers, and indentation have been consumed).
  6850. This is text that can be incorporated into the last open
  6851. block (a paragraph, code block, heading, or raw HTML).
  6852. Setext headings are formed when we see a line of a paragraph
  6853. that is a [setext heading underline].
  6854. Reference link definitions are detected when a paragraph is closed;
  6855. the accumulated text lines are parsed to see if they begin with
  6856. one or more reference link definitions. Any remainder becomes a
  6857. normal paragraph.
  6858. We can see how this works by considering how the tree above is
  6859. generated by four lines of Markdown:
  6860. ``` markdown
  6861. > Lorem ipsum dolor
  6862. sit amet.
  6863. > - Qui *quodsi iracundia*
  6864. > - aliquando id
  6865. ```
  6866. At the outset, our document model is just
  6867. ``` tree
  6868. -> document
  6869. ```
  6870. The first line of our text,
  6871. ``` markdown
  6872. > Lorem ipsum dolor
  6873. ```
  6874. causes a `block_quote` block to be created as a child of our
  6875. open `document` block, and a `paragraph` block as a child of
  6876. the `block_quote`. Then the text is added to the last open
  6877. block, the `paragraph`:
  6878. ``` tree
  6879. -> document
  6880. -> block_quote
  6881. -> paragraph
  6882. "Lorem ipsum dolor"
  6883. ```
  6884. The next line,
  6885. ``` markdown
  6886. sit amet.
  6887. ```
  6888. is a "lazy continuation" of the open `paragraph`, so it gets added
  6889. to the paragraph's text:
  6890. ``` tree
  6891. -> document
  6892. -> block_quote
  6893. -> paragraph
  6894. "Lorem ipsum dolor\nsit amet."
  6895. ```
  6896. The third line,
  6897. ``` markdown
  6898. > - Qui *quodsi iracundia*
  6899. ```
  6900. causes the `paragraph` block to be closed, and a new `list` block
  6901. opened as a child of the `block_quote`. A `list_item` is also
  6902. added as a child of the `list`, and a `paragraph` as a child of
  6903. the `list_item`. The text is then added to the new `paragraph`:
  6904. ``` tree
  6905. -> document
  6906. -> block_quote
  6907. paragraph
  6908. "Lorem ipsum dolor\nsit amet."
  6909. -> list (type=bullet tight=true bullet_char=-)
  6910. -> list_item
  6911. -> paragraph
  6912. "Qui *quodsi iracundia*"
  6913. ```
  6914. The fourth line,
  6915. ``` markdown
  6916. > - aliquando id
  6917. ```
  6918. causes the `list_item` (and its child the `paragraph`) to be closed,
  6919. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6920. is added as a child of the new `list_item`, to contain the text.
  6921. We thus obtain the final tree:
  6922. ``` tree
  6923. -> document
  6924. -> block_quote
  6925. paragraph
  6926. "Lorem ipsum dolor\nsit amet."
  6927. -> list (type=bullet tight=true bullet_char=-)
  6928. list_item
  6929. paragraph
  6930. "Qui *quodsi iracundia*"
  6931. -> list_item
  6932. -> paragraph
  6933. "aliquando id"
  6934. ```
  6935. ## Phase 2: inline structure
  6936. Once all of the input has been parsed, all open blocks are closed.
  6937. We then "walk the tree," visiting every node, and parse raw
  6938. string contents of paragraphs and headings as inlines. At this
  6939. point we have seen all the link reference definitions, so we can
  6940. resolve reference links as we go.
  6941. ``` tree
  6942. document
  6943. block_quote
  6944. paragraph
  6945. str "Lorem ipsum dolor"
  6946. softbreak
  6947. str "sit amet."
  6948. list (type=bullet tight=true bullet_char=-)
  6949. list_item
  6950. paragraph
  6951. str "Qui "
  6952. emph
  6953. str "quodsi iracundia"
  6954. list_item
  6955. paragraph
  6956. str "aliquando id"
  6957. ```
  6958. Notice how the [line ending] in the first paragraph has
  6959. been parsed as a `softbreak`, and the asterisks in the first list item
  6960. have become an `emph`.
  6961. ### An algorithm for parsing nested emphasis and links
  6962. By far the trickiest part of inline parsing is handling emphasis,
  6963. strong emphasis, links, and images. This is done using the following
  6964. algorithm.
  6965. When we're parsing inlines and we hit either
  6966. - a run of `*` or `_` characters, or
  6967. - a `[` or `![`
  6968. we insert a text node with these symbols as its literal content, and we
  6969. add a pointer to this text node to the [delimiter stack](@).
  6970. The [delimiter stack] is a doubly linked list. Each
  6971. element contains a pointer to a text node, plus information about
  6972. - the type of delimiter (`[`, `![`, `*`, `_`)
  6973. - the number of delimiters,
  6974. - whether the delimiter is "active" (all are active to start), and
  6975. - whether the delimiter is a potential opener, a potential closer,
  6976. or both (which depends on what sort of characters precede
  6977. and follow the delimiters).
  6978. When we hit a `]` character, we call the *look for link or image*
  6979. procedure (see below).
  6980. When we hit the end of the input, we call the *process emphasis*
  6981. procedure (see below), with `stack_bottom` = NULL.
  6982. #### *look for link or image*
  6983. Starting at the top of the delimiter stack, we look backwards
  6984. through the stack for an opening `[` or `![` delimiter.
  6985. - If we don't find one, we return a literal text node `]`.
  6986. - If we do find one, but it's not *active*, we remove the inactive
  6987. delimiter from the stack, and return a literal text node `]`.
  6988. - If we find one and it's active, then we parse ahead to see if
  6989. we have an inline link/image, reference link/image, compact reference
  6990. link/image, or shortcut reference link/image.
  6991. + If we don't, then we remove the opening delimiter from the
  6992. delimiter stack and return a literal text node `]`.
  6993. + If we do, then
  6994. * We return a link or image node whose children are the inlines
  6995. after the text node pointed to by the opening delimiter.
  6996. * We run *process emphasis* on these inlines, with the `[` opener
  6997. as `stack_bottom`.
  6998. * We remove the opening delimiter.
  6999. * If we have a link (and not an image), we also set all
  7000. `[` delimiters before the opening delimiter to *inactive*. (This
  7001. will prevent us from getting links within links.)
  7002. #### *process emphasis*
  7003. Parameter `stack_bottom` sets a lower bound to how far we
  7004. descend in the [delimiter stack]. If it is NULL, we can
  7005. go all the way to the bottom. Otherwise, we stop before
  7006. visiting `stack_bottom`.
  7007. Let `current_position` point to the element on the [delimiter stack]
  7008. just above `stack_bottom` (or the first element if `stack_bottom`
  7009. is NULL).
  7010. We keep track of the `openers_bottom` for each delimiter
  7011. type (`*`, `_`). Initialize this to `stack_bottom`.
  7012. Then we repeat the following until we run out of potential
  7013. closers:
  7014. - Move `current_position` forward in the delimiter stack (if needed)
  7015. until we find the first potential closer with delimiter `*` or `_`.
  7016. (This will be the potential closer closest
  7017. to the beginning of the input -- the first one in parse order.)
  7018. - Now, look back in the stack (staying above `stack_bottom` and
  7019. the `openers_bottom` for this delimiter type) for the
  7020. first matching potential opener ("matching" means same delimiter).
  7021. - If one is found:
  7022. + Figure out whether we have emphasis or strong emphasis:
  7023. if both closer and opener spans have length >= 2, we have
  7024. strong, otherwise regular.
  7025. + Insert an emph or strong emph node accordingly, after
  7026. the text node corresponding to the opener.
  7027. + Remove any delimiters between the opener and closer from
  7028. the delimiter stack.
  7029. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7030. from the opening and closing text nodes. If they become empty
  7031. as a result, remove them and remove the corresponding element
  7032. of the delimiter stack. If the closing node is removed, reset
  7033. `current_position` to the next element in the stack.
  7034. - If none in found:
  7035. + Set `openers_bottom` to the element before `current_position`.
  7036. (We know that there are no openers for this kind of closer up to and
  7037. including this point, so this puts a lower bound on future searches.)
  7038. + If the closer at `current_position` is not a potential opener,
  7039. remove it from the delimiter stack (since we know it can't
  7040. be a closer either).
  7041. + Advance `current_position` to the next element in the stack.
  7042. After we're done, we remove all delimiters above `stack_bottom` from the
  7043. delimiter stack.