ion' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: bec510d2f5b2c10bd4e1d5552970da993f8abfc0 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author:
  4. - John MacFarlane
  5. version: 0.13
  6. date: 2014-12-10
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions used for indicating formatting in email and
  12. usenet posts. It was developed in 2004 by John Gruber, who wrote
  13. the first Markdown-to-HTML converter in perl, and it soon became
  14. widely used in websites. By 2014 there were dozens of
  15. implementations in many languages. Some of them extended basic
  16. Markdown syntax with conventions for footnotes, definition lists,
  17. tables, and other constructs, and some allowed output not just in
  18. HTML but in LaTeX and many other formats.
  19. ## Why is a spec needed?
  20. John Gruber's [canonical description of Markdown's
  21. syntax](http://daringfireball.net/projects/markdown/syntax)
  22. does not specify the syntax unambiguously. Here are some examples of
  23. questions it does not answer:
  24. 1. How much indentation is needed for a sublist? The spec says that
  25. continuation paragraphs need to be indented four spaces, but is
  26. not fully explicit about sublists. It is natural to think that
  27. they, too, must be indented four spaces, but `Markdown.pl` does
  28. not require that. This is hardly a "corner case," and divergences
  29. between implementations on this issue often lead to surprises for
  30. users in real documents. (See [this comment by John
  31. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  32. 2. Is a blank line needed before a block quote or header?
  33. Most implementations do not require the blank line. However,
  34. this can lead to unexpected results in hard-wrapped text, and
  35. also to ambiguities in parsing (note that some implementations
  36. put the header inside the blockquote, while others do not).
  37. (John Gruber has also spoken [in favor of requiring the blank
  38. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  39. 3. Is a blank line needed before an indented code block?
  40. (`Markdown.pl` requires it, but this is not mentioned in the
  41. documentation, and some implementations do not require it.)
  42. ``` markdown
  43. paragraph
  44. code?
  45. ```
  46. 4. What is the exact rule for determining when list items get
  47. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  48. "tight"? What should we do with a list like this?
  49. ``` markdown
  50. 1. one
  51. 2. two
  52. 3. three
  53. ```
  54. Or this?
  55. ``` markdown
  56. 1. one
  57. - a
  58. - b
  59. 2. two
  60. ```
  61. (There are some relevant comments by John Gruber
  62. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  63. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  64. ``` markdown
  65. 8. item 1
  66. 9. item 2
  67. 10. item 2a
  68. ```
  69. 6. Is this one list with a horizontal rule in its second item,
  70. or two lists separated by a horizontal rule?
  71. ``` markdown
  72. * a
  73. * * * * *
  74. * b
  75. ```
  76. 7. When list markers change from numbers to bullets, do we have
  77. two lists or one? (The Markdown syntax description suggests two,
  78. but the perl scripts and many other implementations produce one.)
  79. ``` markdown
  80. 1. fee
  81. 2. fie
  82. - foe
  83. - fum
  84. ```
  85. 8. What are the precedence rules for the markers of inline structure?
  86. For example, is the following a valid link, or does the code span
  87. take precedence ?
  88. ``` markdown
  89. [a backtick (`)](/url) and [another backtick (`)](/url).
  90. ```
  91. 9. What are the precedence rules for markers of emphasis and strong
  92. emphasis? For example, how should the following be parsed?
  93. ``` markdown
  94. *foo *bar* baz*
  95. ```
  96. 10. What are the precedence rules between block-level and inline-level
  97. structure? For example, how should the following be parsed?
  98. ``` markdown
  99. - `a long code span can contain a hyphen like this
  100. - and it can screw things up`
  101. ```
  102. 11. Can list items include section headers? (`Markdown.pl` does not
  103. allow this, but does allow blockquotes to include headers.)
  104. ``` markdown
  105. - # Heading
  106. ```
  107. 12. Can list items be empty?
  108. ``` markdown
  109. * a
  110. *
  111. * b
  112. ```
  113. 13. Can link references be defined inside block quotes or list items?
  114. ``` markdown
  115. > Blockquote [foo].
  116. >
  117. > [foo]: /url
  118. ```
  119. 14. If there are multiple definitions for the same reference, which takes
  120. precedence?
  121. ``` markdown
  122. [foo]: /url1
  123. [foo]: /url2
  124. [foo][]
  125. ```
  126. In the absence of a spec, early implementers consulted `Markdown.pl`
  127. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  128. gave manifestly bad results in many cases, so it was not a
  129. satisfactory replacement for a spec.
  130. Because there is no unambiguous spec, implementations have diverged
  131. considerably. As a result, users are often surprised to find that
  132. a document that renders one way on one system (say, a github wiki)
  133. renders differently on another (say, converting to docbook using
  134. pandoc). To make matters worse, because nothing in Markdown counts
  135. as a "syntax error," the divergence often isn't discovered right away.
  136. ## About this document
  137. This document attempts to specify Markdown syntax unambiguously.
  138. It contains many examples with side-by-side Markdown and
  139. HTML. These are intended to double as conformance tests. An
  140. accompanying script `spec_tests.py` can be used to run the tests
  141. against any Markdown program:
  142. python test/spec_tests.py --spec spec.txt --program PROGRAM
  143. Since this document describes how Markdown is to be parsed into
  144. an abstract syntax tree, it would have made sense to use an abstract
  145. representation of the syntax tree instead of HTML. But HTML is capable
  146. of representing the structural distinctions we need to make, and the
  147. choice of HTML for the tests makes it possible to run the tests against
  148. an implementation without writing an abstract syntax tree renderer.
  149. This document is generated from a text file, `spec.txt`, written
  150. in Markdown with a small extension for the side-by-side tests.
  151. The script `spec2md.pl` can be used to turn `spec.txt` into pandoc
  152. Markdown, which can then be converted into other formats.
  153. In the examples, the `→` character is used to represent tabs.
  154. # Preliminaries
  155. ## Characters and lines
  156. The input is a sequence of zero or more [lines](#line).
  157. A [line](@line)
  158. is a sequence of zero or more [characters](#character) followed by a
  159. [line ending](#line-ending) or by the end of file.
  160. A [character](@character) is a unicode code point.
  161. This spec does not specify an encoding; it thinks of lines as composed
  162. of characters rather than bytes. A conforming parser may be limited
  163. to a certain encoding.
  164. A [line ending](@line-ending) is, depending on the platform, a
  165. newline (`U+000A`), carriage return (`U+000D`), or
  166. carriage return + newline.
  167. For security reasons, a conforming parser must strip or replace the
  168. Unicode character `U+0000`.
  169. A line containing no characters, or a line containing only spaces
  170. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@blank-line).
  171. The following definitions of character classes will be used in this spec:
  172. A [whitespace character](@whitespace-character) is a space
  173. (`U+0020`), tab (`U+0009`), carriage return (`U+000D`), or
  174. newline (`U+000A`).
  175. [Whitespace](@whitespace) is a sequence of one or more [whitespace
  176. characters](#whitespace-character).
  177. A [unicode whitespace character](@unicode-whitespace-character) is
  178. any code point in the unicode `Zs` class, or a tab (`U+0009`),
  179. carriage return (`U+000D`), newline (`U+000A`), or form feed
  180. (`U+000C`).
  181. [Unicode whitespace](@unicode-whitespace) is a sequence of one
  182. or more [unicode whitespace characters](#unicode-whitespace-character).
  183. A [non-space character](@non-space-character) is anything but `U+0020`.
  184. An [ASCII punctuation character](@ascii-punctuation-character)
  185. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  186. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  187. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  188. A [punctuation character](@punctuation-character) is an [ASCII
  189. punctuation character](#ascii-punctuation-character) or anything in
  190. the unicode classes `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  191. ## Tab expansion
  192. Tabs in lines are expanded to spaces, with a tab stop of 4 characters:
  193. .
  194. →foo→baz→→bim
  195. .
  196. <pre><code>foo baz bim
  197. </code></pre>
  198. .
  199. .
  200. a→a
  201. ὐ→a
  202. .
  203. <pre><code>a a
  204. ὐ a
  205. </code></pre>
  206. .
  207. # Blocks and inlines
  208. We can think of a document as a sequence of
  209. [blocks](@block)---structural
  210. elements like paragraphs, block quotations,
  211. lists, headers, rules, and code blocks. Blocks can contain other
  212. blocks, or they can contain [inline](@inline) content:
  213. words, spaces, links, emphasized text, images, and inline code.
  214. ## Precedence
  215. Indicators of block structure always take precedence over indicators
  216. of inline structure. So, for example, the following is a list with
  217. two items, not a list with one item containing a code span:
  218. .
  219. - `one
  220. - two`
  221. .
  222. <ul>
  223. <li>`one</li>
  224. <li>two`</li>
  225. </ul>
  226. .
  227. This means that parsing can proceed in two steps: first, the block
  228. structure of the document can be discerned; second, text lines inside
  229. paragraphs, headers, and other block constructs can be parsed for inline
  230. structure. The second step requires information about link reference
  231. definitions that will be available only at the end of the first
  232. step. Note that the first step requires processing lines in sequence,
  233. but the second can be parallelized, since the inline parsing of
  234. one block element does not affect the inline parsing of any other.
  235. ## Container blocks and leaf blocks
  236. We can divide blocks into two types:
  237. [container blocks](@container-block),
  238. which can contain other blocks, and [leaf blocks](@leaf-block),
  239. which cannot.
  240. # Leaf blocks
  241. This section describes the different kinds of leaf block that make up a
  242. Markdown document.
  243. ## Horizontal rules
  244. A line consisting of 0-3 spaces of indentation, followed by a sequence
  245. of three or more matching `-`, `_`, or `*` characters, each followed
  246. optionally by any number of spaces, forms a [horizontal
  247. rule](@horizontal-rule).
  248. .
  249. ***
  250. ---
  251. ___
  252. .
  253. <hr />
  254. <hr />
  255. <hr />
  256. .
  257. Wrong characters:
  258. .
  259. +++
  260. .
  261. <p>+++</p>
  262. .
  263. .
  264. ===
  265. .
  266. <p>===</p>
  267. .
  268. Not enough characters:
  269. .
  270. --
  271. **
  272. __
  273. .
  274. <p>--
  275. **
  276. __</p>
  277. .
  278. One to three spaces indent are allowed:
  279. .
  280. ***
  281. ***
  282. ***
  283. .
  284. <hr />
  285. <hr />
  286. <hr />
  287. .
  288. Four spaces is too many:
  289. .
  290. ***
  291. .
  292. <pre><code>***
  293. </code></pre>
  294. .
  295. .
  296. Foo
  297. ***
  298. .
  299. <p>Foo
  300. ***</p>
  301. .
  302. More than three characters may be used:
  303. .
  304. _____________________________________
  305. .
  306. <hr />
  307. .
  308. Spaces are allowed between the characters:
  309. .
  310. - - -
  311. .
  312. <hr />
  313. .
  314. .
  315. ** * ** * ** * **
  316. .
  317. <hr />
  318. .
  319. .
  320. - - - -
  321. .
  322. <hr />
  323. .
  324. Spaces are allowed at the end:
  325. .
  326. - - - -
  327. .
  328. <hr />
  329. .
  330. However, no other characters may occur in the line:
  331. .
  332. _ _ _ _ a
  333. a------
  334. ---a---
  335. .
  336. <p>_ _ _ _ a</p>
  337. <p>a------</p>
  338. <p>---a---</p>
  339. .
  340. It is required that all of the
  341. [non-space characters](#non-space-character) be the same.
  342. So, this is not a horizontal rule:
  343. .
  344. *-*
  345. .
  346. <p><em>-</em></p>
  347. .
  348. Horizontal rules do not need blank lines before or after:
  349. .
  350. - foo
  351. ***
  352. - bar
  353. .
  354. <ul>
  355. <li>foo</li>
  356. </ul>
  357. <hr />
  358. <ul>
  359. <li>bar</li>
  360. </ul>
  361. .
  362. Horizontal rules can interrupt a paragraph:
  363. .
  364. Foo
  365. ***
  366. bar
  367. .
  368. <p>Foo</p>
  369. <hr />
  370. <p>bar</p>
  371. .
  372. If a line of dashes that meets the above conditions for being a
  373. horizontal rule could also be interpreted as the underline of a [setext
  374. header](#setext-header), the interpretation as a
  375. [setext-header](#setext-header) takes precedence. Thus, for example,
  376. this is a setext header, not a paragraph followed by a horizontal rule:
  377. .
  378. Foo
  379. ---
  380. bar
  381. .
  382. <h2>Foo</h2>
  383. <p>bar</p>
  384. .
  385. When both a horizontal rule and a list item are possible
  386. interpretations of a line, the horizontal rule is preferred:
  387. .
  388. * Foo
  389. * * *
  390. * Bar
  391. .
  392. <ul>
  393. <li>Foo</li>
  394. </ul>
  395. <hr />
  396. <ul>
  397. <li>Bar</li>
  398. </ul>
  399. .
  400. If you want a horizontal rule in a list item, use a different bullet:
  401. .
  402. - Foo
  403. - * * *
  404. .
  405. <ul>
  406. <li>Foo</li>
  407. <li>
  408. <hr />
  409. </li>
  410. </ul>
  411. .
  412. ## ATX headers
  413. An [ATX header](@atx-header)
  414. consists of a string of characters, parsed as inline content, between an
  415. opening sequence of 1--6 unescaped `#` characters and an optional
  416. closing sequence of any number of `#` characters. The opening sequence
  417. of `#` characters cannot be followed directly by a nonspace character.
  418. The optional closing sequence of `#`s must be preceded by a space and may be
  419. followed by spaces only. The opening `#` character may be indented 0-3
  420. spaces. The raw contents of the header are stripped of leading and
  421. trailing spaces before being parsed as inline content. The header level
  422. is equal to the number of `#` characters in the opening sequence.
  423. Simple headers:
  424. .
  425. # foo
  426. ## foo
  427. ### foo
  428. #### foo
  429. ##### foo
  430. ###### foo
  431. .
  432. <h1>foo</h1>
  433. <h2>foo</h2>
  434. <h3>foo</h3>
  435. <h4>foo</h4>
  436. <h5>foo</h5>
  437. <h6>foo</h6>
  438. .
  439. More than six `#` characters is not a header:
  440. .
  441. ####### foo
  442. .
  443. <p>####### foo</p>
  444. .
  445. A space is required between the `#` characters and the header's
  446. contents. Note that many implementations currently do not require
  447. the space. However, the space was required by the [original ATX
  448. implementation](http://www.aaronsw.com/2002/atx/atx.py), and it helps
  449. prevent things like the following from being parsed as headers:
  450. .
  451. #5 bolt
  452. .
  453. <p>#5 bolt</p>
  454. .
  455. This is not a header, because the first `#` is escaped:
  456. .
  457. \## foo
  458. .
  459. <p>## foo</p>
  460. .
  461. Contents are parsed as inlines:
  462. .
  463. # foo *bar* \*baz\*
  464. .
  465. <h1>foo <em>bar</em> *baz*</h1>
  466. .
  467. Leading and trailing blanks are ignored in parsing inline content:
  468. .
  469. # foo
  470. .
  471. <h1>foo</h1>
  472. .
  473. One to three spaces indentation are allowed:
  474. .
  475. ### foo
  476. ## foo
  477. # foo
  478. .
  479. <h3>foo</h3>
  480. <h2>foo</h2>
  481. <h1>foo</h1>
  482. .
  483. Four spaces are too much:
  484. .
  485. # foo
  486. .
  487. <pre><code># foo
  488. </code></pre>
  489. .
  490. .
  491. foo
  492. # bar
  493. .
  494. <p>foo
  495. # bar</p>
  496. .
  497. A closing sequence of `#` characters is optional:
  498. .
  499. ## foo ##
  500. ### bar ###
  501. .
  502. <h2>foo</h2>
  503. <h3>bar</h3>
  504. .
  505. It need not be the same length as the opening sequence:
  506. .
  507. # foo ##################################
  508. ##### foo ##
  509. .
  510. <h1>foo</h1>
  511. <h5>foo</h5>
  512. .
  513. Spaces are allowed after the closing sequence:
  514. .
  515. ### foo ###
  516. .
  517. <h3>foo</h3>
  518. .
  519. A sequence of `#` characters with a nonspace character following it
  520. is not a closing sequence, but counts as part of the contents of the
  521. header:
  522. .
  523. ### foo ### b
  524. .
  525. <h3>foo ### b</h3>
  526. .
  527. The closing sequence must be preceded by a space:
  528. .
  529. # foo#
  530. .
  531. <h1>foo#</h1>
  532. .
  533. Backslash-escaped `#` characters do not count as part
  534. of the closing sequence:
  535. .
  536. ### foo \###
  537. ## foo #\##
  538. # foo \#
  539. .
  540. <h3>foo ###</h3>
  541. <h2>foo ###</h2>
  542. <h1>foo #</h1>
  543. .
  544. ATX headers need not be separated from surrounding content by blank
  545. lines, and they can interrupt paragraphs:
  546. .
  547. ****
  548. ## foo
  549. ****
  550. .
  551. <hr />
  552. <h2>foo</h2>
  553. <hr />
  554. .
  555. .
  556. Foo bar
  557. # baz
  558. Bar foo
  559. .
  560. <p>Foo bar</p>
  561. <h1>baz</h1>
  562. <p>Bar foo</p>
  563. .
  564. ATX headers can be empty:
  565. .
  566. ##
  567. #
  568. ### ###
  569. .
  570. <h2></h2>
  571. <h1></h1>
  572. <h3></h3>
  573. .
  574. ## Setext headers
  575. A [setext header](@setext-header)
  576. consists of a line of text, containing at least one nonspace character,
  577. with no more than 3 spaces indentation, followed by a [setext header
  578. underline](#setext-header-underline). The line of text must be
  579. one that, were it not followed by the setext header underline,
  580. would be interpreted as part of a paragraph: it cannot be a code
  581. block, header, blockquote, horizontal rule, or list.
  582. A [setext header underline](@setext-header-underline) is a sequence of
  583. `=` characters or a sequence of `-` characters, with no more than 3
  584. spaces indentation and any number of trailing spaces. If a line
  585. containing a single `-` can be interpreted as an
  586. empty [list item](#list-items), it should be interpreted this way
  587. and not as a [setext header underline](#setext-header-underline).
  588. The header is a level 1 header if `=` characters are used in the
  589. [setext header underline](#setext-header-underline), and a level 2
  590. header if `-` characters are used. The contents of the header are the
  591. result of parsing the first line as Markdown inline content.
  592. In general, a setext header need not be preceded or followed by a
  593. blank line. However, it cannot interrupt a paragraph, so when a
  594. setext header comes after a paragraph, a blank line is needed between
  595. them.
  596. Simple examples:
  597. .
  598. Foo *bar*
  599. =========
  600. Foo *bar*
  601. ---------
  602. .
  603. <h1>Foo <em>bar</em></h1>
  604. <h2>Foo <em>bar</em></h2>
  605. .
  606. The underlining can be any length:
  607. .
  608. Foo
  609. -------------------------
  610. Foo
  611. =
  612. .
  613. <h2>Foo</h2>
  614. <h1>Foo</h1>
  615. .
  616. The header content can be indented up to three spaces, and need
  617. not line up with the underlining:
  618. .
  619. Foo
  620. ---
  621. Foo
  622. -----
  623. Foo
  624. ===
  625. .
  626. <h2>Foo</h2>
  627. <h2>Foo</h2>
  628. <h1>Foo</h1>
  629. .
  630. Four spaces indent is too much:
  631. .
  632. Foo
  633. ---
  634. Foo
  635. ---
  636. .
  637. <pre><code>Foo
  638. ---
  639. Foo
  640. </code></pre>
  641. <hr />
  642. .
  643. The setext header underline can be indented up to three spaces, and
  644. may have trailing spaces:
  645. .
  646. Foo
  647. ----
  648. .
  649. <h2>Foo</h2>
  650. .
  651. Four spaces is too much:
  652. .
  653. Foo
  654. ---
  655. .
  656. <p>Foo
  657. ---</p>
  658. .
  659. The setext header underline cannot contain internal spaces:
  660. .
  661. Foo
  662. = =
  663. Foo
  664. --- -
  665. .
  666. <p>Foo
  667. = =</p>
  668. <p>Foo</p>
  669. <hr />
  670. .
  671. Trailing spaces in the content line do not cause a line break:
  672. .
  673. Foo
  674. -----
  675. .
  676. <h2>Foo</h2>
  677. .
  678. Nor does a backslash at the end:
  679. .
  680. Foo\
  681. ----
  682. .
  683. <h2>Foo\</h2>
  684. .
  685. Since indicators of block structure take precedence over
  686. indicators of inline structure, the following are setext headers:
  687. .
  688. `Foo
  689. ----
  690. `
  691. <a title="a lot
  692. ---
  693. of dashes"/>
  694. .
  695. <h2>`Foo</h2>
  696. <p>`</p>
  697. <h2>&lt;a title=&quot;a lot</h2>
  698. <p>of dashes&quot;/&gt;</p>
  699. .
  700. The setext header underline cannot be a [lazy continuation
  701. line](#lazy-continuation-line) in a list item or block quote:
  702. .
  703. > Foo
  704. ---
  705. .
  706. <blockquote>
  707. <p>Foo</p>
  708. </blockquote>
  709. <hr />
  710. .
  711. .
  712. - Foo
  713. ---
  714. .
  715. <ul>
  716. <li>Foo</li>
  717. </ul>
  718. <hr />
  719. .
  720. A setext header cannot interrupt a paragraph:
  721. .
  722. Foo
  723. Bar
  724. ---
  725. Foo
  726. Bar
  727. ===
  728. .
  729. <p>Foo
  730. Bar</p>
  731. <hr />
  732. <p>Foo
  733. Bar
  734. ===</p>
  735. .
  736. But in general a blank line is not required before or after:
  737. .
  738. ---
  739. Foo
  740. ---
  741. Bar
  742. ---
  743. Baz
  744. .
  745. <hr />
  746. <h2>Foo</h2>
  747. <h2>Bar</h2>
  748. <p>Baz</p>
  749. .
  750. Setext headers cannot be empty:
  751. .
  752. ====
  753. .
  754. <p>====</p>
  755. .
  756. Setext header text lines must not be interpretable as block
  757. constructs other than paragraphs. So, the line of dashes
  758. in these examples gets interpreted as a horizontal rule:
  759. .
  760. ---
  761. ---
  762. .
  763. <hr />
  764. <hr />
  765. .
  766. .
  767. - foo
  768. -----
  769. .
  770. <ul>
  771. <li>foo</li>
  772. </ul>
  773. <hr />
  774. .
  775. .
  776. foo
  777. ---
  778. .
  779. <pre><code>foo
  780. </code></pre>
  781. <hr />
  782. .
  783. .
  784. > foo
  785. -----
  786. .
  787. <blockquote>
  788. <p>foo</p>
  789. </blockquote>
  790. <hr />
  791. .
  792. If you want a header with `> foo` as its literal text, you can
  793. use backslash escapes:
  794. .
  795. \> foo
  796. ------
  797. .
  798. <h2>&gt; foo</h2>
  799. .
  800. ## Indented code blocks
  801. An [indented code block](@indented-code-block) is composed of one or more
  802. [indented chunks](#indented-chunk) separated by blank lines.
  803. An [indented chunk](@indented-chunk) is a sequence of non-blank lines,
  804. each indented four or more spaces. The contents of the code block are
  805. the literal contents of the lines, including trailing
  806. [line endings](#line-ending), minus four spaces of indentation.
  807. An indented code block has no attributes.
  808. An indented code block cannot interrupt a paragraph, so there must be
  809. a blank line between a paragraph and a following indented code block.
  810. (A blank line is not needed, however, between a code block and a following
  811. paragraph.)
  812. .
  813. a simple
  814. indented code block
  815. .
  816. <pre><code>a simple
  817. indented code block
  818. </code></pre>
  819. .
  820. The contents are literal text, and do not get parsed as Markdown:
  821. .
  822. <a/>
  823. *hi*
  824. - one
  825. .
  826. <pre><code>&lt;a/&gt;
  827. *hi*
  828. - one
  829. </code></pre>
  830. .
  831. Here we have three chunks separated by blank lines:
  832. .
  833. chunk1
  834. chunk2
  835. chunk3
  836. .
  837. <pre><code>chunk1
  838. chunk2
  839. chunk3
  840. </code></pre>
  841. .
  842. Any initial spaces beyond four will be included in the content, even
  843. in interior blank lines:
  844. .
  845. chunk1
  846. chunk2
  847. .
  848. <pre><code>chunk1
  849. chunk2
  850. </code></pre>
  851. .
  852. An indented code block cannot interrupt a paragraph. (This
  853. allows hanging indents and the like.)
  854. .
  855. Foo
  856. bar
  857. .
  858. <p>Foo
  859. bar</p>
  860. .
  861. However, any non-blank line with fewer than four leading spaces ends
  862. the code block immediately. So a paragraph may occur immediately
  863. after indented code:
  864. .
  865. foo
  866. bar
  867. .
  868. <pre><code>foo
  869. </code></pre>
  870. <p>bar</p>
  871. .
  872. And indented code can occur immediately before and after other kinds of
  873. blocks:
  874. .
  875. # Header
  876. foo
  877. Header
  878. ------
  879. foo
  880. ----
  881. .
  882. <h1>Header</h1>
  883. <pre><code>foo
  884. </code></pre>
  885. <h2>Header</h2>
  886. <pre><code>foo
  887. </code></pre>
  888. <hr />
  889. .
  890. The first line can be indented more than four spaces:
  891. .
  892. foo
  893. bar
  894. .
  895. <pre><code> foo
  896. bar
  897. </code></pre>
  898. .
  899. Blank lines preceding or following an indented code block
  900. are not included in it:
  901. .
  902. foo
  903. .
  904. <pre><code>foo
  905. </code></pre>
  906. .
  907. Trailing spaces are included in the code block's content:
  908. .
  909. foo
  910. .
  911. <pre><code>foo
  912. </code></pre>
  913. .
  914. ## Fenced code blocks
  915. A [code fence](@code-fence) is a sequence
  916. of at least three consecutive backtick characters (`` ` ``) or
  917. tildes (`~`). (Tildes and backticks cannot be mixed.)
  918. A [fenced code block](@fenced-code-block)
  919. begins with a code fence, indented no more than three spaces.
  920. The line with the opening code fence may optionally contain some text
  921. following the code fence; this is trimmed of leading and trailing
  922. spaces and called the [info string](@info-string).
  923. The info string may not contain any backtick
  924. characters. (The reason for this restriction is that otherwise
  925. some inline code would be incorrectly interpreted as the
  926. beginning of a fenced code block.)
  927. The content of the code block consists of all subsequent lines, until
  928. a closing [code fence](#code-fence) of the same type as the code block
  929. began with (backticks or tildes), and with at least as many backticks
  930. or tildes as the opening code fence. If the leading code fence is
  931. indented N spaces, then up to N spaces of indentation are removed from
  932. each line of the content (if present). (If a content line is not
  933. indented, it is preserved unchanged. If it is indented less than N
  934. spaces, all of the indentation is removed.)
  935. The closing code fence may be indented up to three spaces, and may be
  936. followed only by spaces, which are ignored. If the end of the
  937. containing block (or document) is reached and no closing code fence
  938. has been found, the code block contains all of the lines after the
  939. opening code fence until the end of the containing block (or
  940. document). (An alternative spec would require backtracking in the
  941. event that a closing code fence is not found. But this makes parsing
  942. much less efficient, and there seems to be no real down side to the
  943. behavior described here.)
  944. A fenced code block may interrupt a paragraph, and does not require
  945. a blank line either before or after.
  946. The content of a code fence is treated as literal text, not parsed
  947. as inlines. The first word of the info string is typically used to
  948. specify the language of the code sample, and rendered in the `class`
  949. attribute of the `code` tag. However, this spec does not mandate any
  950. particular treatment of the info string.
  951. Here is a simple example with backticks:
  952. .
  953. ```
  954. <
  955. >
  956. ```
  957. .
  958. <pre><code>&lt;
  959. &gt;
  960. </code></pre>
  961. .
  962. With tildes:
  963. .
  964. ~~~
  965. <
  966. >
  967. ~~~
  968. .
  969. <pre><code>&lt;
  970. &gt;
  971. </code></pre>
  972. .
  973. The closing code fence must use the same character as the opening
  974. fence:
  975. .
  976. ```
  977. aaa
  978. ~~~
  979. ```
  980. .
  981. <pre><code>aaa
  982. ~~~
  983. </code></pre>
  984. .
  985. .
  986. ~~~
  987. aaa
  988. ```
  989. ~~~
  990. .
  991. <pre><code>aaa
  992. ```
  993. </code></pre>
  994. .
  995. The closing code fence must be at least as long as the opening fence:
  996. .
  997. ````
  998. aaa
  999. ```
  1000. ``````
  1001. .
  1002. <pre><code>aaa
  1003. ```
  1004. </code></pre>
  1005. .
  1006. .