Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CommonMark Spec</title>
<style type="text/css">
body { font-family: Helvetica, arial, freesans, clean, sans-serif;
line-height: 1.4;
max-width: 48em;
margin: auto;
color: #333333;
background-color: #fff;
font-size: 13pt;
}
div#TOC ul { list-style: none; }
h1 { font-size: 140%; font-weight: bold; border-top: 1px solid gray; padding-top: 0.5em; }
h2 { font-size: 120%; font-weight: bold; }
h3 { font-size: 110%; font-weight: bold; }
h4 { font-size: 100%; font-weight: bold; }
span.space { position: relative; }
span.space:after {
content: "";
position: absolute;
/* create a mark that indicates a space (trick from D. Greenspan) */
top: 3px; bottom: 3px; left: 1px; right: 1px;
border: 1px solid #999;
}
div.example { overflow: hidden; }
p { text-align: justify; }
pre { padding: 0.5em; margin-left: 0; margin-right: 0; margin-top: 0.2em;
margin-bottom: 0.5em; font-size: 88%; }
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
code { font-family: monospace; }
div.example > pre { float:left; width: 48%; }
div.example > pre.markdown { clear:left; }
pre.tree { font-weight: bold; color: #777; }
pre.markdown { background-color: #D3E1E4; }
pre.html { background-color: #C9CaCE; }
pre.html span.space:after {
border: 1px solid #666;
}
div.examplenum { font-size: 82%; text-align: left; }
a.footnoteRef > sup:before {
content: "[";
}
a.footnoteRef > sup:after {
content: "]";
}
a.footnoteRef > sup {
vertical-align: baseline;
font-size: 100%;
}
</style>
</head>
<body>
<h1 class="title">CommonMark Spec</h1>
<div class="version">Version 1 (2014-09-06)</div>
<div class="authors">
<span class="author">John MacFarlane</span>
</div>
<div id="TOC">
<ul>
<li><a href="#introduction"><span class="toc-section-number">1</span> Introduction</a><ul>
<li><a href="#what-is-markdown"><span class="toc-section-number">1.1</span> What is Markdown?</a></li>
<li><a href="#why-is-a-spec-needed"><span class="toc-section-number">1.2</span> Why is a spec needed?</a></li>
<li><a href="#about-this-document"><span class="toc-section-number">1.3</span> About this document</a></li>
</ul></li>
<li><a href="#preprocessing"><span class="toc-section-number">2</span> Preprocessing</a></li>
<li><a href="#blocks-and-inlines"><span class="toc-section-number">3</span> Blocks and inlines</a><ul>
<li><a href="#precedence"><span class="toc-section-number">3.1</span> Precedence</a></li>
<li><a href="#container-blocks-and-leaf-blocks"><span class="toc-section-number">3.2</span> Container blocks and leaf blocks</a></li>
</ul></li>
<li><a href="#leaf-blocks"><span class="toc-section-number">4</span> Leaf blocks</a><ul>
<li><a href="#horizontal-rules"><span class="toc-section-number">4.1</span> Horizontal rules</a></li>
<li><a href="#atx-headers"><span class="toc-section-number">4.2</span> ATX headers</a></li>
<li><a href="#setext-headers"><span class="toc-section-number">4.3</span> Setext headers</a></li>
<li><a href="#indented-code-blocks"><span class="toc-section-number">4.4</span> Indented code blocks</a></li>
<li><a href="#fenced-code-blocks"><span class="toc-section-number">4.5</span> Fenced code blocks</a></li>
<li><a href="#html-blocks"><span class="toc-section-number">4.6</span> HTML blocks</a></li>
<li><a href="#link-reference-definitions"><span class="toc-section-number">4.7</span> Link reference definitions</a></li>
<li><a href="#paragraphs"><span class="toc-section-number">4.8</span> Paragraphs</a></li>
<li><a href="#blank-lines"><span class="toc-section-number">4.9</span> Blank lines</a></li>
</ul></li>
<li><a href="#container-blocks"><span class="toc-section-number">5</span> Container blocks</a><ul>
<li><a href="#block-quotes"><span class="toc-section-number">5.1</span> Block quotes</a></li>
<li><a href="#list-items"><span class="toc-section-number">5.2</span> List items</a><ul>
<li><a href="#motivation"><span class="toc-section-number">5.2.1</span> Motivation</a></li>
</ul></li>
<li><a href="#lists"><span class="toc-section-number">5.3</span> Lists</a></li>
</ul></li>
<li><a href="#inlines"><span class="toc-section-number">6</span> Inlines</a><ul>
<li><a href="#backslash-escapes"><span class="toc-section-number">6.1</span> Backslash escapes</a></li>
<li><a href="#entities"><span class="toc-section-number">6.2</span> Entities</a></li>
<li><a href="#code-span"><span class="toc-section-number">6.3</span> Code span</a></li>
<li><a href="#emphasis-and-strong-emphasis"><span class="toc-section-number">6.4</span> Emphasis and strong emphasis</a></li>
<li><a href="#links"><span class="toc-section-number">6.5</span> Links</a></li>
<li><a href="#images"><span class="toc-section-number">6.6</span> Images</a></li>
<li><a href="#autolinks"><span class="toc-section-number">6.7</span> Autolinks</a></li>
<li><a href="#raw-html"><span class="toc-section-number">6.8</span> Raw HTML</a></li>
<li><a href="#hard-line-breaks"><span class="toc-section-number">6.9</span> Hard line breaks</a></li>
<li><a href="#soft-line-breaks"><span class="toc-section-number">6.10</span> Soft line breaks</a></li>
<li><a href="#strings"><span class="toc-section-number">6.11</span> Strings</a></li>
</ul></li>
<li><a href="#extensions"><span class="toc-section-number">7</span> Extensions</a><ul>
<li><a href="#attributes"><span class="toc-section-number">7.1</span> Attributes</a><ul>
<li><a href="#horizontal-rules-1"><span class="toc-section-number">7.1.1</span> Horizontal rules</a></li>
<li><a href="#atx-headers-1"><span class="toc-section-number">7.1.2</span> ATX headers</a></li>
<li><a href="#setext-headers-1"><span class="toc-section-number">7.1.3</span> Setext headers</a></li>
<li><a href="#fenced-code-blocks-1"><span class="toc-section-number">7.1.4</span> Fenced code blocks</a></li>
<li><a href="#reference-links"><span class="toc-section-number">7.1.5</span> Reference Links</a></li>
<li><a href="#paragraphs-1"><span class="toc-section-number">7.1.6</span> Paragraphs</a></li>
<li><a href="#block-quotes-1"><span class="toc-section-number">7.1.7</span> Block quotes</a></li>
<li><a href="#lists-1"><span class="toc-section-number">7.1.8</span> Lists</a></li>
<li><a href="#code-span-1"><span class="toc-section-number">7.1.9</span> Code span</a></li>
<li><a href="#emphasis-and-strong-emphasis-1"><span class="toc-section-number">7.1.10</span> Emphasis and strong emphasis</a></li>
<li><a href="#inline-links"><span class="toc-section-number">7.1.11</span> Inline Links</a></li>
<li><a href="#images-1"><span class="toc-section-number">7.1.12</span> Images</a></li>
<li><a href="#spans"><span class="toc-section-number">7.1.13</span> Spans</a></li>
</ul></li>
</ul></li>
<li><a href="#appendix-a-a-parsing-strategy">Appendix A: A parsing strategy</a><ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#the-document-tree">The document tree</a></li>
<li><a href="#how-source-lines-alter-the-document-tree">How source lines alter the document tree</a></li>
<li><a href="#from-block-structure-to-the-final-document">From block structure to the final document</a></li>
</ul></li>
</ul>
</div>
<h1 id="introduction"><span class="header-section-number">1</span> Introduction</h1>
<h2 id="what-is-markdown"><span class="header-section-number">1.1</span> What is Markdown?</h2>
<p>Markdown is a plain text format for writing structured documents, based on conventions used for indicating formatting in email and usenet posts. It was developed in 2004 by John Gruber, who wrote the first Markdown-to-HTML converter in perl, and it soon became widely used in websites. By 2014 there were dozens of implementations in many languages. Some of them extended basic Markdown syntax with conventions for footnotes, definition lists, tables, and other constructs, and some allowed output not just in HTML but in LaTeX and many other formats.</p>
<h2 id="why-is-a-spec-needed"><span class="header-section-number">1.2</span> Why is a spec needed?</h2>
<p>John Gruber’s <a href="http://daringfireball.net/projects/markdown/syntax">canonical description of Markdown’s syntax</a> does not specify the syntax unambiguously. Here are some examples of questions it does not answer:</p>
<ol style="list-style-type: decimal">
<li><p>How much indentation is needed for a sublist? The spec says that continuation paragraphs need to be indented four spaces, but is not fully explicit about sublists. It is natural to think that they, too, must be indented four spaces, but <code>Markdown.pl</code> does not require that. This is hardly a “corner case,” and divergences between implementations on this issue often lead to surprises for users in real documents. (See <a href="http://article.gmane.org/gmane.text.markdown.general/1997">this comment by John Gruber</a>.)</p></li>
<li><p>Is a blank line needed before a block quote or header? Most implementations do not require the blank line. However, this can lead to unexpected results in hard-wrapped text, and also to ambiguities in parsing (note that some implementations put the header inside the blockquote, while others do not). (John Gruber has also spoken <a href="http://article.gmane.org/gmane.text.markdown.general/2146">in favor of requiring the blank lines</a>.)</p></li>
<li><p>Is a blank line needed before an indented code block? (<code>Markdown.pl</code> requires it, but this is not mentioned in the documentation, and some implementations do not require it.)</p>
<pre class="markdown"><code>paragraph
code?</code></pre></li>
<li><p>What is the exact rule for determining when list items get wrapped in <code>&lt;p&gt;</code> tags? Can a list be partially “loose” and partially “tight”? What should we do with a list like this?</p>
<pre class="markdown"><code>1. one
2. two
3. three</code></pre>
<p>Or this?</p>
<pre class="markdown"><code>1. one
- a
- b
2. two</code></pre>
<p>(There are some relevant comments by John Gruber <a href="http://article.gmane.org/gmane.text.markdown.general/2554">here</a>.)</p></li>
<li><p>Can list markers be indented? Can ordered list markers be right-aligned?</p>
<pre class="markdown"><code> 8. item 1
9. item 2
10. item 2a</code></pre></li>
<li><p>Is this one list with a horizontal rule in its second item, or two lists separated by a horizontal rule?</p>
<pre class="markdown"><code>* a
* * * * *
* b</code></pre></li>
<li><p>When list markers change from numbers to bullets, do we have two lists or one? (The Markdown syntax description suggests two, but the perl scripts and many other implementations produce one.)</p>
<pre class="markdown"><code>1. fee
2. fie
- foe
- fum</code></pre></li>
<li><p>What are the precedence rules for the markers of inline structure? For example, is the following a valid link, or does the code span take precedence ?</p>
<pre class="markdown"><code>[a backtick (`)](/url) and [another backtick (`)](/url).</code></pre></li>
<li><p>What are the precedence rules for markers of emphasis and strong emphasis? For example, how should the following be parsed?</p>
<pre class="markdown"><code>*foo *bar* baz*</code></pre></li>
<li><p>What are the precedence rules between block-level and inline-level structure? For example, how should the following be parsed?</p>
<pre class="markdown"><code>- `a long code span can contain a hyphen like this
- and it can screw things up`</code></pre></li>
<li><p>Can list items include headers? (<code>Markdown.pl</code> does not allow this, but headers can occur in blockquotes.)</p>
<pre class="markdown"><code>- # Heading</code></pre></li>
<li><p>Can link references be defined inside block quotes or list items?</p>
<pre class="markdown"><code>&gt; Blockquote [foo].
&gt;
&gt; [foo]: /url</code></pre></li>
<li><p>If there are multiple definitions for the same reference, which takes precedence?</p>
<pre class="markdown"><code>[foo]: /url1
[foo]: /url2
[foo][]</code></pre></li>
</ol>
<p>In the absence of a spec, early implementers consulted <code>Markdown.pl</code> to resolve these ambiguities. But <code>Markdown.pl</code> was quite buggy, and gave manifestly bad results in many cases, so it was not a satisfactory replacement for a spec.</p>
<p>Because there is no unambiguous spec, implementations have diverged considerably. As a result, users are often surprised to find that a document that renders one way on one system (say, a github wiki) renders differently on another (say, converting to docbook using pandoc). To make matters worse, because nothing in Markdown counts as a “syntax error,” the divergence often isn’t discovered right away.</p>
<h2 id="about-this-document"><span class="header-section-number">1.3</span> About this document</h2>
<p>This document attempts to specify Markdown syntax unambiguously. It contains many examples with side-by-side Markdown and HTML. These are intended to double as conformance tests. An accompanying script <code>runtests.pl</code> can be used to run the tests against any Markdown program:</p>
<pre><code>perl runtests.pl spec.txt PROGRAM</code></pre>
<p>Since this document describes how Markdown is to be parsed into an abstract syntax tree, it would have made sense to use an abstract representation of the syntax tree instead of HTML. But HTML is capable of representing the structural distinctions we need to make, and the choice of HTML for the tests makes it possible to run the tests against an implementation without writing an abstract syntax tree renderer.</p>
<p>This document is generated from a text file, <code>spec.txt</code>, written in Markdown with a small extension for the side-by-side tests. The script <code>spec2md.pl</code> can be used to turn <code>spec.txt</code> into pandoc Markdown, which can then be converted into other formats.</p>
<p>In the examples, the <code></code> character is used to represent tabs.</p>
<h1 id="preprocessing"><span class="header-section-number">2</span> Preprocessing</h1>
<p>A <a href="#line">line</a> <a id="line"></a> is a sequence of zero or more characters followed by a line ending (CR, LF, or CRLF) or by the end of file.</p>
<p>This spec does not specify an encoding; it thinks of lines as composed of characters rather than bytes. A conforming parser may be limited to a certain encoding.</p>
<p>Tabs in lines are expanded to spaces, with a tab stop of 4 characters:</p>
<div id="example-1" class="example" data-section="Preprocessing">
<div class="examplenum">
Example 1
</div>
<pre class="markdown"><code>→foo→baz→→bim</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;foo baz bim
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-2" class="example" data-section="Preprocessing">
<div class="examplenum">
Example 2
</div>
<pre class="markdown"><code> a→a
ὐ→a</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;a a
ὐ a
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Line endings are replaced by newline characters (LF).</p>
<p>A line containing no characters, or a line containing only spaces (after tab expansion), is called a <a href="#blank-line">blank line</a>. <a id="blank-line"></a></p>
<h1 id="blocks-and-inlines"><span class="header-section-number">3</span> Blocks and inlines</h1>
<p>We can think of a document as a sequence of <a href="#block">blocks</a><a
id="block"></a>—structural elements like paragraphs, block quotations, lists, headers, rules, and code blocks. Blocks can contain other blocks, or they can contain <a href="#inline">inline</a><a id="inline"></a> content: words, spaces, links, emphasized text, images, and inline code.</p>
<h2 id="precedence"><span class="header-section-number">3.1</span> Precedence</h2>
<p>Indicators of block structure always take precedence over indicators of inline structure. So, for example, the following is a list with two items, not a list with one item containing a code span:</p>
<div id="example-3" class="example" data-section="Precedence">
<div class="examplenum">
Example 3
</div>
<pre class="markdown"><code>- `one
- two`</code></pre>
<pre class="html"><code>&lt;ul&gt;
&lt;li&gt;`one&lt;/li&gt;
&lt;li&gt;two`&lt;/li&gt;
&lt;/ul&gt;</code></pre>
</div>
<p>This means that parsing can proceed in two steps: first, the block structure of the document can be discerned; second, text lines inside paragraphs, headers, and other block constructs can be parsed for inline structure. The second step requires information about link reference definitions that will be available only at the end of the first step. Note that the first step requires processing lines in sequence, but the second can be parallelized, since the inline parsing of one block element does not affect the inline parsing of any other.</p>
<h2 id="container-blocks-and-leaf-blocks"><span class="header-section-number">3.2</span> Container blocks and leaf blocks</h2>
<p>We can divide blocks into two types: <a href="#container-block">container blocks</a>, <a id="container-block"></a> which can contain other blocks, and <a href="#leaf-block">leaf blocks</a>, <a id="leaf-block"></a> which cannot.</p>
<h1 id="leaf-blocks"><span class="header-section-number">4</span> Leaf blocks</h1>
<p>This section describes the different kinds of leaf block that make up a Markdown document.</p>
<h2 id="horizontal-rules"><span class="header-section-number">4.1</span> Horizontal rules</h2>
<p>A line consisting of 0-3 spaces of indentation, followed by a sequence of three or more matching <code>-</code>, <code>_</code>, or <code>*</code> characters, each followed optionally by any number of spaces, forms a <a href="#horizontal-rule">horizontal rule</a>. <a id="horizontal-rule"></a></p>
<div id="example-4" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 4
</div>
<pre class="markdown"><code>***
---
___</code></pre>
<pre class="html"><code>&lt;hr /&gt;
&lt;hr /&gt;
&lt;hr /&gt;</code></pre>
</div>
<p>Wrong characters:</p>
<div id="example-5" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 5
</div>
<pre class="markdown"><code>+++</code></pre>
<pre class="html"><code>&lt;p&gt;+++&lt;/p&gt;</code></pre>
</div>
<div id="example-6" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 6
</div>
<pre class="markdown"><code>===</code></pre>
<pre class="html"><code>&lt;p&gt;===&lt;/p&gt;</code></pre>
</div>
<p>Not enough characters:</p>
<div id="example-7" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 7
</div>
<pre class="markdown"><code>--
**
__</code></pre>
<pre class="html"><code>&lt;p&gt;--
**
__&lt;/p&gt;</code></pre>
</div>
<p>One to three spaces indent are allowed:</p>
<div id="example-8" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 8
</div>
<pre class="markdown"><code> ***
***
***</code></pre>
<pre class="html"><code>&lt;hr /&gt;
&lt;hr /&gt;
&lt;hr /&gt;</code></pre>
</div>
<p>Four spaces is too many:</p>
<div id="example-9" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 9
</div>
<pre class="markdown"><code> ***</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;***
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-10" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 10
</div>
<pre class="markdown"><code>Foo
***</code></pre>
<pre class="html"><code>&lt;p&gt;Foo
***&lt;/p&gt;</code></pre>
</div>
<p>More than three characters may be used:</p>
<div id="example-11" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 11
</div>
<pre class="markdown"><code>_____________________________________</code></pre>
<pre class="html"><code>&lt;hr /&gt;</code></pre>
</div>
<p>Spaces are allowed between the characters:</p>
<div id="example-12" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 12
</div>
<pre class="markdown"><code> - - -</code></pre>
<pre class="html"><code>&lt;hr /&gt;</code></pre>
</div>
<div id="example-13" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 13
</div>
<pre class="markdown"><code> ** * ** * ** * **</code></pre>
<pre class="html"><code>&lt;hr /&gt;</code></pre>
</div>
<div id="example-14" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 14
</div>
<pre class="markdown"><code>- - - -</code></pre>
<pre class="html"><code>&lt;hr /&gt;</code></pre>
</div>
<p>Spaces are allowed at the end:</p>
<div id="example-15" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 15
</div>
<pre class="markdown"><code>- - - - </code></pre>
<pre class="html"><code>&lt;hr /&gt;</code></pre>
</div>
<p>However, no other characters may occur at the end or the beginning:</p>
<div id="example-16" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 16
</div>
<pre class="markdown"><code>_ _ _ _ a
a------</code></pre>
<pre class="html"><code>&lt;p&gt;_ _ _ _ a&lt;/p&gt;
&lt;p&gt;a------&lt;/p&gt;</code></pre>
</div>
<p>It is required that all of the non-space characters be the same. So, this is not a horizontal rule:</p>
<div id="example-17" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 17
</div>
<pre class="markdown"><code> *-*</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;em&gt;-&lt;/em&gt;&lt;/p&gt;</code></pre>
</div>
<p>Horizontal rules do not need blank lines before or after:</p>
<div id="example-18" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 18
</div>
<pre class="markdown"><code>- foo
***
- bar</code></pre>
<pre class="html"><code>&lt;ul&gt;
&lt;li&gt;foo&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;ul&gt;
&lt;li&gt;bar&lt;/li&gt;
&lt;/ul&gt;</code></pre>
</div>
<p>Horizontal rules can interrupt a paragraph:</p>
<div id="example-19" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 19
</div>
<pre class="markdown"><code>Foo
***
bar</code></pre>
<pre class="html"><code>&lt;p&gt;Foo&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;bar&lt;/p&gt;</code></pre>
</div>
<p>Note, however, that this is a setext header, not a paragraph followed by a horizontal rule:</p>
<div id="example-20" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 20
</div>
<pre class="markdown"><code>Foo
---
bar</code></pre>
<pre class="html"><code>&lt;h2&gt;Foo&lt;/h2&gt;
&lt;p&gt;bar&lt;/p&gt;</code></pre>
</div>
<p>When both a horizontal rule and a list item are possible interpretations of a line, the horizontal rule is preferred:</p>
<div id="example-21" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 21
</div>
<pre class="markdown"><code>* Foo
* * *
* Bar</code></pre>
<pre class="html"><code>&lt;ul&gt;
&lt;li&gt;Foo&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;ul&gt;
&lt;li&gt;Bar&lt;/li&gt;
&lt;/ul&gt;</code></pre>
</div>
<p>If you want a horizontal rule in a list item, use a different bullet:</p>
<div id="example-22" class="example" data-section="Horizontal rules">
<div class="examplenum">
Example 22
</div>
<pre class="markdown"><code>- Foo
- * * *</code></pre>
<pre class="html"><code>&lt;ul&gt;
&lt;li&gt;Foo&lt;/li&gt;
&lt;li&gt;&lt;hr /&gt;&lt;/li&gt;
&lt;/ul&gt;</code></pre>
</div>
<h2 id="atx-headers"><span class="header-section-number">4.2</span> ATX headers</h2>
<p>An <a href="#atx-header">ATX header</a> <a id="atx-header"></a> consists of a string of characters, parsed as inline content, between an opening sequence of 1–6 unescaped <code>#</code> characters and an optional closing sequence of any number of <code>#</code> characters. The opening sequence of <code>#</code> characters cannot be followed directly by a non-space character. The closing <code>#</code> characters may be followed by spaces only. The opening <code>#</code> character may be indented 0-3 spaces. The raw contents of the header are stripped of leading and trailing spaces before being parsed as inline content. The header level is equal to the number of <code>#</code> characters in the opening sequence.</p>
<p>Simple headers:</p>
<div id="example-23" class="example" data-section="ATX headers">
<div class="examplenum">
Example 23
</div>
<pre class="markdown"><code># foo
## foo
### foo
#### foo
##### foo
###### foo</code></pre>
<pre class="html"><code>&lt;h1&gt;foo&lt;/h1&gt;
&lt;h2&gt;foo&lt;/h2&gt;
&lt;h3&gt;foo&lt;/h3&gt;
&lt;h4&gt;foo&lt;/h4&gt;
&lt;h5&gt;foo&lt;/h5&gt;
&lt;h6&gt;foo&lt;/h6&gt;</code></pre>
</div>
<p>More than six <code>#</code> characters is not a header:</p>
<div id="example-24" class="example" data-section="ATX headers">
<div class="examplenum">
Example 24
</div>
<pre class="markdown"><code>####### foo</code></pre>
<pre class="html"><code>&lt;p&gt;####### foo&lt;/p&gt;</code></pre>
</div>
<p>A space is required between the <code>#</code> characters and the header’s contents. Note that many implementations currently do not require the space. However, the space was required by the <a href="http://www.aaronsw.com/2002/atx/atx.py">original ATX implementation</a>, and it helps prevent things like the following from being parsed as headers:</p>
<div id="example-25" class="example" data-section="ATX headers">
<div class="examplenum">
Example 25
</div>
<pre class="markdown"><code>#5 bolt</code></pre>
<pre class="html"><code>&lt;p&gt;#5 bolt&lt;/p&gt;</code></pre>
</div>
<p>This is not a header, because the first <code>#</code> is escaped:</p>
<div id="example-26" class="example" data-section="ATX headers">
<div class="examplenum">
Example 26
</div>
<pre class="markdown"><code>\## foo</code></pre>
<pre class="html"><code>&lt;p&gt;## foo&lt;/p&gt;</code></pre>
</div>
<p>Contents are parsed as inlines:</p>
<div id="example-27" class="example" data-section="ATX headers">
<div class="examplenum">
Example 27
</div>
<pre class="markdown"><code># foo *bar* \*baz\*</code></pre>
<pre class="html"><code>&lt;h1&gt;foo &lt;em&gt;bar&lt;/em&gt; *baz*&lt;/h1&gt;</code></pre>
</div>
<p>Leading and trailing blanks are ignored in parsing inline content:</p>
<div id="example-28" class="example" data-section="ATX headers">
<div class="examplenum">
Example 28
</div>
<pre class="markdown"><code># foo </code></pre>
<pre class="html"><code>&lt;h1&gt;foo&lt;/h1&gt;</code></pre>
</div>
<p>One to three spaces indentation are allowed:</p>
<div id="example-29" class="example" data-section="ATX headers">
<div class="examplenum">
Example 29
</div>
<pre class="markdown"><code> ### foo
## foo
# foo</code></pre>
<pre class="html"><code>&lt;h3&gt;foo&lt;/h3&gt;
&lt;h2&gt;foo&lt;/h2&gt;
&lt;h1&gt;foo&lt;/h1&gt;</code></pre>
</div>
<p>Four spaces are too much:</p>
<div id="example-30" class="example" data-section="ATX headers">
<div class="examplenum">
Example 30
</div>
<pre class="markdown"><code> # foo</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;# foo
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-31" class="example" data-section="ATX headers">
<div class="examplenum">
Example 31
</div>
<pre class="markdown"><code>foo
# bar</code></pre>
<pre class="html"><code>&lt;p&gt;foo
# bar&lt;/p&gt;</code></pre>
</div>
<p>A closing sequence of <code>#</code> characters is optional:</p>
<div id="example-32" class="example" data-section="ATX headers">
<div class="examplenum">
Example 32
</div>
<pre class="markdown"><code>## foo ##
### bar ###</code></pre>
<pre class="html"><code>&lt;h2&gt;foo&lt;/h2&gt;
&lt;h3&gt;bar&lt;/h3&gt;</code></pre>
</div>
<p>It need not be the same length as the opening sequence:</p>
<div id="example-33" class="example" data-section="ATX headers">
<div class="examplenum">
Example 33
</div>
<pre class="markdown"><code># foo ##################################
##### foo ##</code></pre>
<pre class="html"><code>&lt;h1&gt;foo&lt;/h1&gt;
&lt;h5&gt;foo&lt;/h5&gt;</code></pre>
</div>
<p>Spaces are allowed after the closing sequence:</p>
<div id="example-34" class="example" data-section="ATX headers">
<div class="examplenum">
Example 34
</div>
<pre class="markdown"><code>### foo ### </code></pre>
<pre class="html"><code>&lt;h3&gt;foo&lt;/h3&gt;</code></pre>
</div>
<p>A sequence of <code>#</code> characters with a non-space character following it is not a closing sequence, but counts as part of the contents of the header:</p>
<div id="example-35" class="example" data-section="ATX headers">
<div class="examplenum">
Example 35
</div>
<pre class="markdown"><code>### foo ### b</code></pre>
<pre class="html"><code>&lt;h3&gt;foo ### b&lt;/h3&gt;</code></pre>
</div>
<p>Backslash-escaped <code>#</code> characters do not count as part of the closing sequence:</p>
<div id="example-36" class="example" data-section="ATX headers">
<div class="examplenum">
Example 36
</div>
<pre class="markdown"><code>### foo \###
## foo \#\##
# foo \#</code></pre>
<pre class="html"><code>&lt;h3&gt;foo #&lt;/h3&gt;
&lt;h2&gt;foo ##&lt;/h2&gt;
&lt;h1&gt;foo #&lt;/h1&gt;</code></pre>
</div>
<p>ATX headers need not be separated from surrounding content by blank lines, and they can interrupt paragraphs:</p>
<div id="example-37" class="example" data-section="ATX headers">
<div class="examplenum">
Example 37
</div>
<pre class="markdown"><code>****
## foo
****</code></pre>
<pre class="html"><code>&lt;hr /&gt;
&lt;h2&gt;foo&lt;/h2&gt;
&lt;hr /&gt;</code></pre>
</div>
<div id="example-38" class="example" data-section="ATX headers">
<div class="examplenum">
Example 38
</div>
<pre class="markdown"><code>Foo bar
# baz
Bar foo</code></pre>
<pre class="html"><code>&lt;p&gt;Foo bar&lt;/p&gt;
&lt;h1&gt;baz&lt;/h1&gt;
&lt;p&gt;Bar foo&lt;/p&gt;</code></pre>
</div>
<p>ATX headers can be empty:</p>
<div id="example-39" class="example" data-section="ATX headers">
<div class="examplenum">
Example 39
</div>
<pre class="markdown"><code>##
#
### ###</code></pre>
<pre class="html"><code>&lt;h2&gt;&lt;/h2&gt;
&lt;h1&gt;&lt;/h1&gt;
&lt;h3&gt;&lt;/h3&gt;</code></pre>
</div>
<h2 id="setext-headers"><span class="header-section-number">4.3</span> Setext headers</h2>
<p>A <a href="#setext-header">setext header</a> <a id="setext-header"></a> consists of a line of text, containing at least one non-space character, with no more than 3 spaces indentation, followed by a <a href="#setext-header-underline">setext header underline</a>. A <a href="#setext-header-underline">setext header underline</a> <a id="setext-header-underline"></a> is a sequence of <code>=</code> characters or a sequence of <code>-</code> characters, with no more than 3 spaces indentation and any number of trailing spaces. The header is a level 1 header if <code>=</code> characters are used, and a level 2 header if <code>-</code> characters are used. The contents of the header are the result of parsing the first line as Markdown inline content.</p>
<p>In general, a setext header need not be preceded or followed by a blank line. However, it cannot interrupt a paragraph, so when a setext header comes after a paragraph, a blank line is needed between them.</p>
<p>Simple examples:</p>
<div id="example-40" class="example" data-section="Setext headers">
<div class="examplenum">
Example 40
</div>
<pre class="markdown"><code>Foo *bar*
=========
Foo *bar*
---------</code></pre>
<pre class="html"><code>&lt;h1&gt;Foo &lt;em&gt;bar&lt;/em&gt;&lt;/h1&gt;
&lt;h2&gt;Foo &lt;em&gt;bar&lt;/em&gt;&lt;/h2&gt;</code></pre>
</div>
<p>The underlining can be any length:</p>
<div id="example-41" class="example" data-section="Setext headers">
<div class="examplenum">
Example 41
</div>
<pre class="markdown"><code>Foo
-------------------------
Foo
=</code></pre>
<pre class="html"><code>&lt;h2&gt;Foo&lt;/h2&gt;
&lt;h1&gt;Foo&lt;/h1&gt;</code></pre>
</div>
<p>The header content can be indented up to three spaces, and need not line up with the underlining:</p>
<div id="example-42" class="example" data-section="Setext headers">
<div class="examplenum">
Example 42
</div>
<pre class="markdown"><code> Foo
---
Foo
-----
Foo
===</code></pre>
<pre class="html"><code>&lt;h2&gt;Foo&lt;/h2&gt;
&lt;h2&gt;Foo&lt;/h2&gt;
&lt;h1&gt;Foo&lt;/h1&gt;</code></pre>
</div>
<p>Four spaces indent is too much:</p>
<div id="example-43" class="example" data-section="Setext headers">
<div class="examplenum">
Example 43
</div>
<pre class="markdown"><code> Foo
---
Foo
---</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;Foo
---
Foo
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;</code></pre>
</div>
<p>The setext header underline can be indented up to three spaces, and may have trailing spaces:</p>
<div id="example-44" class="example" data-section="Setext headers">
<div class="examplenum">
Example 44
</div>
<pre class="markdown"><code>Foo
---- </code></pre>
<pre class="html"><code>&lt;h2&gt;Foo&lt;/h2&gt;</code></pre>
</div>
<p>Four spaces is too much:</p>
<div id="example-45" class="example" data-section="Setext headers">
<div class="examplenum">
Example 45
</div>
<pre class="markdown"><code>Foo
---</code></pre>
<pre class="html"><code>&lt;p&gt;Foo
---&lt;/p&gt;</code></pre>
</div>
<p>The setext header underline cannot contain internal spaces:</p>
<div id="example-46" class="example" data-section="Setext headers">
<div class="examplenum">
Example 46
</div>
<pre class="markdown"><code>Foo
= =
Foo
--- -</code></pre>
<pre class="html"><code>&lt;p&gt;Foo
= =&lt;/p&gt;
&lt;p&gt;Foo&lt;/p&gt;
&lt;hr /&gt;</code></pre>
</div>
<p>Trailing spaces in the content line do not cause a line break:</p>
<div id="example-47" class="example" data-section="Setext headers">
<div class="examplenum">
Example 47
</div>
<pre class="markdown"><code>Foo
-----</code></pre>
<pre class="html"><code>&lt;h2&gt;Foo&lt;/h2&gt;</code></pre>
</div>
<p>Nor does a backslash at the end:</p>
<div id="example-48" class="example" data-section="Setext headers">
<div class="examplenum">
Example 48
</div>
<pre class="markdown"><code>Foo\
----</code></pre>
<pre class="html"><code>&lt;h2&gt;Foo\&lt;/h2&gt;</code></pre>
</div>
<p>Since indicators of block structure take precedence over indicators of inline structure, the following are setext headers:</p>
<div id="example-49" class="example" data-section="Setext headers">
<div class="examplenum">
Example 49
</div>
<pre class="markdown"><code>`Foo
----
`
&lt;a title=&quot;a lot
---
of dashes&quot;/&gt;</code></pre>
<pre class="html"><code>&lt;h2&gt;`Foo&lt;/h2&gt;
&lt;p&gt;`&lt;/p&gt;
&lt;h2&gt;&amp;lt;a title=&amp;quot;a lot&lt;/h2&gt;
&lt;p&gt;of dashes&amp;quot;/&amp;gt;&lt;/p&gt;</code></pre>
</div>
<p>The setext header underline cannot be a lazy line:</p>
<div id="example-50" class="example" data-section="Setext headers">
<div class="examplenum">
Example 50
</div>
<pre class="markdown"><code>&gt; Foo
---</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;Foo&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr /&gt;</code></pre>
</div>
<p>A setext header cannot interrupt a paragraph:</p>
<div id="example-51" class="example" data-section="Setext headers">
<div class="examplenum">
Example 51
</div>
<pre class="markdown"><code>Foo
Bar
---
Foo
Bar
===</code></pre>
<pre class="html"><code>&lt;p&gt;Foo
Bar&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Foo
Bar
===&lt;/p&gt;</code></pre>
</div>
<p>But in general a blank line is not required before or after:</p>
<div id="example-52" class="example" data-section="Setext headers">
<div class="examplenum">
Example 52
</div>
<pre class="markdown"><code>---
Foo
---
Bar
---
Baz</code></pre>
<pre class="html"><code>&lt;hr /&gt;
&lt;h2&gt;Foo&lt;/h2&gt;
&lt;h2&gt;Bar&lt;/h2&gt;
&lt;p&gt;Baz&lt;/p&gt;</code></pre>
</div>
<p>Setext headers cannot be empty:</p>
<div id="example-53" class="example" data-section="Setext headers">
<div class="examplenum">
Example 53
</div>
<pre class="markdown"><code>
====</code></pre>
<pre class="html"><code>&lt;p&gt;====&lt;/p&gt;</code></pre>
</div>
<h2 id="indented-code-blocks"><span class="header-section-number">4.4</span> Indented code blocks</h2>
<p>An <a href="#indented-code-block">indented code block</a> <a id="indented-code-block"></a> is composed of one or more <a href="#indented-chunk">indented chunks</a> separated by blank lines. An <a href="#indented-chunk">indented chunk</a> <a id="indented-chunk"></a> is a sequence of non-blank lines, each indented four or more spaces. An indented code block cannot interrupt a paragraph, so if it occurs before or after a paragraph, there must be an intervening blank line. The contents of the code block are the literal contents of the lines, including trailing newlines, minus four spaces of indentation. An indented code block has no attributes.</p>
<div id="example-54" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 54
</div>
<pre class="markdown"><code> a simple
indented code block</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;a simple
indented code block
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>The contents are literal text, and do not get parsed as Markdown:</p>
<div id="example-55" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 55
</div>
<pre class="markdown"><code> &lt;a/&gt;
*hi*
- one</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;&amp;lt;a/&amp;gt;
*hi*
- one
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Here we have three chunks separated by blank lines:</p>
<div id="example-56" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 56
</div>
<pre class="markdown"><code> chunk1
chunk2
chunk3</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;chunk1
chunk2
chunk3
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Any initial spaces beyond four will be included in the content, even in interior blank lines:</p>
<div id="example-57" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 57
</div>
<pre class="markdown"><code> chunk1
chunk2</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;chunk1
chunk2
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>An indented code block cannot interrupt a paragraph. (This allows hanging indents and the like.)</p>
<div id="example-58" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 58
</div>
<pre class="markdown"><code>Foo
bar
</code></pre>
<pre class="html"><code>&lt;p&gt;Foo
bar&lt;/p&gt;</code></pre>
</div>
<p>However, any non-blank line with fewer than four leading spaces ends the code block immediately. So a paragraph may occur immediately after indented code:</p>
<div id="example-59" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 59
</div>
<pre class="markdown"><code> foo
bar</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;foo
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;bar&lt;/p&gt;</code></pre>
</div>
<p>And indented code can occur immediately before and after other kinds of blocks:</p>
<div id="example-60" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 60
</div>
<pre class="markdown"><code># Header
foo
Header
------
foo
----</code></pre>
<pre class="html"><code>&lt;h1&gt;Header&lt;/h1&gt;
&lt;pre&gt;&lt;code&gt;foo
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Header&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;foo
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;</code></pre>
</div>
<p>The first line can be indented more than four spaces:</p>
<div id="example-61" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 61
</div>
<pre class="markdown"><code> foo
bar</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt; foo
bar
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Blank lines preceding or following an indented code block are not included in it:</p>
<div id="example-62" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 62
</div>
<pre class="markdown"><code>
foo
</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;foo
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Trailing spaces are included in the code block’s content:</p>
<div id="example-63" class="example" data-section="Indented code blocks">
<div class="examplenum">
Example 63
</div>
<pre class="markdown"><code> foo </code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;foo
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<h2 id="fenced-code-blocks"><span class="header-section-number">4.5</span> Fenced code blocks</h2>
<p>A <a href="#code-fence">code fence</a> <a id="code-fence"></a> is a sequence of at least three consecutive backtick characters (<code>`</code>) or tildes (<code>~</code>). (Tildes and backticks cannot be mixed.) A <a href="#fenced-code-block">fenced code block</a> <a id="fenced-code-block"></a> begins with a code fence, indented no more than three spaces.</p>
<p>The line with the opening code fence may optionally contain some text following the code fence; this is trimmed of leading and trailing spaces and called the <a href="#info-string">info string</a>. <a id="info-string"></a> The info string may not contain any backtick characters. (The reason for this restriction is that otherwise some inline code would be incorrectly interpreted as the beginning of a fenced code block.)</p>
<p>The content of the code block consists of all subsequent lines, until a closing <a href="#code-fence">code fence</a> of the same type as the code block began with (backticks or tildes), and with at least as many backticks or tildes as the opening code fence. If the leading code fence is indented N spaces, then up to N spaces of indentation are removed from each line of the content (if present). (If a content line is not indented, it is preserved unchanged. If it is indented less than N spaces, all of the indentation is removed.)</p>
<p>The closing code fence may be indented up to three spaces, and may be followed only by spaces, which are ignored. If the end of the containing block (or document) is reached and no closing code fence has been found, the code block contains all of the lines after the opening code fence until the end of the containing block (or document). (An alternative spec would require backtracking in the event that a closing code fence is not found. But this makes parsing much less efficient, and there seems to be no real down side to the behavior described here.)</p>
<p>A fenced code block may interrupt a paragraph, and does not require a blank line either before or after.</p>
<p>The content of a code fence is treated as literal text, not parsed as inlines. The first word of the info string is typically used to specify the language of the code sample, and rendered in the <code>class</code> attribute of the <code>code</code> tag. However, this spec does not mandate any particular treatment of the info string.</p>
<p>Here is a simple example with backticks:</p>
<div id="example-64" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 64
</div>
<pre class="markdown"><code>```
&lt;
&gt;
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;&amp;lt;
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>With tildes:</p>
<div id="example-65" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 65
</div>
<pre class="markdown"><code>~~~
&lt;
&gt;
~~~</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;&amp;lt;
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>The closing code fence must use the same character as the opening fence:</p>
<div id="example-66" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 66
</div>
<pre class="markdown"><code>```
aaa
~~~
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
~~~
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-67" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 67
</div>
<pre class="markdown"><code>~~~
aaa
```
~~~</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
```
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>The closing code fence must be at least as long as the opening fence:</p>
<div id="example-68" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 68
</div>
<pre class="markdown"><code>````
aaa
```
``````</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
```
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-69" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 69
</div>
<pre class="markdown"><code>~~~~
aaa
~~~
~~~~</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
~~~
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Unclosed code blocks are closed by the end of the document:</p>
<div id="example-70" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 70
</div>
<pre class="markdown"><code>```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-71" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 71
</div>
<pre class="markdown"><code>`````
```
aaa</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;
```
aaa
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>A code block can have all empty lines as its content:</p>
<div id="example-72" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 72
</div>
<pre class="markdown"><code>```
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>A code block can be empty:</p>
<div id="example-73" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 73
</div>
<pre class="markdown"><code>```
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Fences can be indented. If the opening fence is indented, content lines will have equivalent opening indentation removed, if present:</p>
<div id="example-74" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 74
</div>
<pre class="markdown"><code> ```
aaa
aaa
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
aaa
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-75" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 75
</div>
<pre class="markdown"><code> ```
aaa
aaa
aaa
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
aaa
aaa
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-76" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 76
</div>
<pre class="markdown"><code> ```
aaa
aaa
aaa
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
aaa
aaa
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Four spaces indentation produces an indented code block:</p>
<div id="example-77" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 77
</div>
<pre class="markdown"><code> ```
aaa
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;```
aaa
```
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Code fences (opening and closing) cannot contain internal spaces:</p>
<div id="example-78" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 78
</div>
<pre class="markdown"><code>``` ```
aaa</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;code&gt;&lt;/code&gt;
aaa&lt;/p&gt;</code></pre>
</div>
<div id="example-79" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 79
</div>
<pre class="markdown"><code>~~~~~~
aaa
~~~ ~~</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
~~~ ~~
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Fenced code blocks can interrupt paragraphs, and can be followed directly by paragraphs, without a blank line between:</p>
<div id="example-80" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 80
</div>
<pre class="markdown"><code>foo
```
bar
```
baz</code></pre>
<pre class="html"><code>&lt;p&gt;foo&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;bar
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;baz&lt;/p&gt;</code></pre>
</div>
<p>Other blocks can also occur before and after fenced code blocks without an intervening blank line:</p>
<div id="example-81" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 81
</div>
<pre class="markdown"><code>foo
---
~~~
bar
~~~
# baz</code></pre>
<pre class="html"><code>&lt;h2&gt;foo&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;bar
&lt;/code&gt;&lt;/pre&gt;
&lt;h1&gt;baz&lt;/h1&gt;</code></pre>
</div>
<p>An <a href="#info-string">info string</a> can be provided after the opening code fence. Opening and closing spaces will be stripped, and the first word, prefixed with <code>language-</code>, is used as the value for the <code>class</code> attribute of the <code>code</code> element within the enclosing <code>pre</code> element.</p>
<div id="example-82" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 82
</div>
<pre class="markdown"><code>```ruby
def foo(x)
return 3
end
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;def foo(x)
return 3
end
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-83" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 83
</div>
<pre class="markdown"><code>~~~~ ruby startline=3 $%@#$
def foo(x)
return 3
end
~~~~~~~</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;def foo(x)
return 3
end
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-84" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 84
</div>
<pre class="markdown"><code>````;
````</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code class=&quot;language-;&quot;&gt;&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>Info strings for backtick code blocks cannot contain backticks:</p>
<div id="example-85" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 85
</div>
<pre class="markdown"><code>``` aa ```
foo</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;code&gt;aa&lt;/code&gt;
foo&lt;/p&gt;</code></pre>
</div>
<p>Closing code fences cannot have info strings:</p>
<div id="example-86" class="example" data-section="Fenced code blocks">
<div class="examplenum">
Example 86
</div>
<pre class="markdown"><code>```
``` aaa
```</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;``` aaa
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<h2 id="html-blocks"><span class="header-section-number">4.6</span> HTML blocks</h2>
<p>An <a href="#html-block-tag">HTML block tag</a> <a id="html-block-tag"></a> is an <a href="#open-tag">open tag</a> or <a href="#closing-tag">closing tag</a> whose tag name is one of the following (case-insensitive): <code>article</code>, <code>header</code>, <code>aside</code>, <code>hgroup</code>, <code>blockquote</code>, <code>hr</code>, <code>iframe</code>, <code>body</code>, <code>li</code>, <code>map</code>, <code>button</code>, <code>object</code>, <code>canvas</code>, <code>ol</code>, <code>caption</code>, <code>output</code>, <code>col</code>, <code>p</code>, <code>colgroup</code>, <code>pre</code>, <code>dd</code>, <code>progress</code>, <code>div</code>, <code>section</code>, <code>dl</code>, <code>table</code>, <code>td</code>, <code>dt</code>, <code>tbody</code>, <code>embed</code>, <code>textarea</code>, <code>fieldset</code>, <code>tfoot</code>, <code>figcaption</code>, <code>th</code>, <code>figure</code>, <code>thead</code>, <code>footer</code>, <code>footer</code>, <code>tr</code>, <code>form</code>, <code>ul</code>, <code>h1</code>, <code>h2</code>, <code>h3</code>, <code>h4</code>, <code>h5</code>, <code>h6</code>, <code>video</code>, <code>script</code>, <code>style</code>.</p>
<p>An <a href="#html-block">HTML block</a> <a id="html-block"></a> begins with an <a href="#html-block-tag">HTML block tag</a>, <a href="#html-comment">HTML comment</a>, <a href="#processing-instruction">processing instruction</a>, <a href="#declaration">declaration</a>, or <a href="#cdata-section">CDATA section</a>. It ends when a <a href="#blank-line">blank line</a> or the end of the input is encountered. The initial line may be indented up to three spaces, and subsequent lines may have any indentation. The contents of the HTML block are interpreted as raw HTML, and will not be escaped in HTML output.</p>
<p>Some simple examples:</p>
<div id="example-87" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 87
</div>
<pre class="markdown"><code>&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
hi
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
okay.</code></pre>
<pre class="html"><code>&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
hi
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;okay.&lt;/p&gt;</code></pre>
</div>
<div id="example-88" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 88
</div>
<pre class="markdown"><code> &lt;div&gt;
*hello*
&lt;foo&gt;&lt;a&gt;</code></pre>
<pre class="html"><code> &lt;div&gt;
*hello*
&lt;foo&gt;&lt;a&gt;</code></pre>
</div>
<p>Here we have two code blocks with a Markdown paragraph between them:</p>
<div id="example-89" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 89
</div>
<pre class="markdown"><code>&lt;DIV CLASS=&quot;foo&quot;&gt;
*Markdown*
&lt;/DIV&gt;</code></pre>
<pre class="html"><code>&lt;DIV CLASS=&quot;foo&quot;&gt;
&lt;p&gt;&lt;em&gt;Markdown&lt;/em&gt;&lt;/p&gt;
&lt;/DIV&gt;</code></pre>
</div>
<p>In the following example, what looks like a Markdown code block is actually part of the HTML block, which continues until a blank line or the end of the document is reached:</p>
<div id="example-90" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 90
</div>
<pre class="markdown"><code>&lt;div&gt;&lt;/div&gt;
``` c
int x = 33;
```</code></pre>
<pre class="html"><code>&lt;div&gt;&lt;/div&gt;
``` c
int x = 33;
```</code></pre>
</div>
<p>A comment:</p>
<div id="example-91" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 91
</div>
<pre class="markdown"><code>&lt;!-- Foo
bar
baz --&gt;</code></pre>
<pre class="html"><code>&lt;!-- Foo
bar
baz --&gt;</code></pre>
</div>
<p>A processing instruction:</p>
<div id="example-92" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 92
</div>
<pre class="markdown"><code>&lt;?php
echo &#39;foo&#39;
?&gt;</code></pre>
<pre class="html"><code>&lt;?php
echo &#39;foo&#39;
?&gt;</code></pre>
</div>
<p>CDATA:</p>
<div id="example-93" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 93
</div>
<pre class="markdown"><code>&lt;![CDATA[
function matchwo(a,b)
{
if (a &lt; b &amp;&amp; a &lt; 0) then
{
return 1;
}
else
{
return 0;
}
}
]]&gt;</code></pre>
<pre class="html"><code>&lt;![CDATA[
function matchwo(a,b)
{
if (a &lt; b &amp;&amp; a &lt; 0) then
{
return 1;
}
else
{
return 0;
}
}
]]&gt;</code></pre>
</div>
<p>The opening tag can be indented 1-3 spaces, but not 4:</p>
<div id="example-94" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 94
</div>
<pre class="markdown"><code> &lt;!-- foo --&gt;
&lt;!-- foo --&gt;</code></pre>
<pre class="html"><code> &lt;!-- foo --&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;!-- foo --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>An HTML block can interrupt a paragraph, and need not be preceded by a blank line.</p>
<div id="example-95" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 95
</div>
<pre class="markdown"><code>Foo
&lt;div&gt;
bar
&lt;/div&gt;</code></pre>
<pre class="html"><code>&lt;p&gt;Foo&lt;/p&gt;
&lt;div&gt;
bar
&lt;/div&gt;</code></pre>
</div>
<p>However, a following blank line is always needed, except at the end of a document:</p>
<div id="example-96" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 96
</div>
<pre class="markdown"><code>&lt;div&gt;
bar
&lt;/div&gt;
*foo*</code></pre>
<pre class="html"><code>&lt;div&gt;
bar
&lt;/div&gt;
*foo*</code></pre>
</div>
<p>An incomplete HTML block tag may also start an HTML block:</p>
<div id="example-97" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 97
</div>
<pre class="markdown"><code>&lt;div class
foo</code></pre>
<pre class="html"><code>&lt;div class
foo</code></pre>
</div>
<p>This rule differs from John Gruber’s original Markdown syntax specification, which says:</p>
<blockquote>
<p>The only restrictions are that block-level HTML elements — e.g. <code>&lt;div&gt;</code>, <code>&lt;table&gt;</code>, <code>&lt;pre&gt;</code>, <code>&lt;p&gt;</code>, etc. — must be separated from surrounding content by blank lines, and the start and end tags of the block should not be indented with tabs or spaces.</p>
</blockquote>
<p>In some ways Gruber’s rule is more restrictive than the one given here:</p>
<ul>
<li>It requires that an HTML block be preceded by a blank line.</li>
<li>It does not allow the start tag to be indented.</li>
<li>It requires a matching end tag, which it also does not allow to be indented.</li>
</ul>
<p>Indeed, most Markdown implementations, including some of Gruber’s own perl implementations, do not impose these restrictions.</p>
<p>There is one respect, however, in which Gruber’s rule is more liberal than the one given here, since it allows blank lines to occur inside an HTML block. There are two reasons for disallowing them here. First, it removes the need to parse balanced tags, which is expensive and can require backtracking from the end of the document if no matching end tag is found. Second, it provides a very simple and flexible way of including Markdown content inside HTML tags: simply separate the Markdown from the HTML using blank lines:</p>
<div id="example-98" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 98
</div>
<pre class="markdown"><code>&lt;div&gt;
*Emphasized* text.
&lt;/div&gt;</code></pre>
<pre class="html"><code>&lt;div&gt;
&lt;p&gt;&lt;em&gt;Emphasized&lt;/em&gt; text.&lt;/p&gt;
&lt;/div&gt;</code></pre>
</div>
<p>Compare:</p>
<div id="example-99" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 99
</div>
<pre class="markdown"><code>&lt;div&gt;
*Emphasized* text.
&lt;/div&gt;</code></pre>
<pre class="html"><code>&lt;div&gt;
*Emphasized* text.
&lt;/div&gt;</code></pre>
</div>
<p>Some Markdown implementations have adopted a convention of interpreting content inside tags as text if the open tag has the attribute <code>markdown=1</code>. The rule given above seems a simpler and more elegant way of achieving the same expressive power, which is also much simpler to parse.</p>
<p>The main potential drawback is that one can no longer paste HTML blocks into Markdown documents with 100% reliability. However, <em>in most cases</em> this will work fine, because the blank lines in HTML are usually followed by HTML block tags. For example:</p>
<div id="example-100" class="example" data-section="HTML blocks">
<div class="examplenum">
Example 100
</div>
<pre class="markdown"><code>&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
Hi
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;</code></pre>
<pre class="html"><code>&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
Hi
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;</code></pre>
</div>
<p>Moreover, blank lines are usually not necessary and can be deleted. The exception is inside <code>&lt;pre&gt;</code> tags; here, one can replace the blank lines with <code>&amp;#10;</code> entities.</p>
<p>So there is no important loss of expressive power with the new rule.</p>
<h2 id="link-reference-definitions"><span class="header-section-number">4.7</span> Link reference definitions</h2>
<p>A <a href="#link-reference-definition">link reference definition</a> <a id="link-reference-definition"></a> consists of a <a href="#link-label">link label</a>, indented up to three spaces, followed by a colon (<code>:</code>), optional blank space (including up to one newline), a <a href="#link-destination">link destination</a>, optional blank space (including up to one newline), and an optional <a href="#link-title">link title</a>, which if it is present must be separated from the <a href="#link-destination">link destination</a> by whitespace. No further non-space characters may occur on the line.</p>
<p>A <a href="#link-reference-definition">link reference-definition</a> does not correspond to a structural element of a document. Instead, it defines a label which can be used in <a href="#reference-link">reference links</a> and reference-style <a href="#image">images</a> elsewhere in the document. <a href="#link-reference-definitions">Link reference definitions</a> can come either before or after the links that use them.</p>
<div id="example-101" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 101
</div>
<pre class="markdown"><code>[foo]: /url &quot;title&quot;
[foo]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/url&quot; title=&quot;title&quot;&gt;foo&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<div id="example-102" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 102
</div>
<pre class="markdown"><code> [foo]:
/url
&#39;the title&#39;
[foo]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/url&quot; title=&quot;the title&quot;&gt;foo&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<div id="example-103" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 103
</div>
<pre class="markdown"><code>[Foo*bar\]]:my_(url) &#39;title (with parens)&#39;
[Foo*bar\]]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;my_(url)&quot; title=&quot;title (with parens)&quot;&gt;Foo*bar]&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<div id="example-104" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 104
</div>
<pre class="markdown"><code>[Foo bar]:
&lt;my url&gt;
&#39;title&#39;
[Foo bar]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;my url&quot; title=&quot;title&quot;&gt;Foo bar&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<p>The title may be omitted:</p>
<div id="example-105" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 105
</div>
<pre class="markdown"><code>[foo]:
/url
[foo]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/url&quot;&gt;foo&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<p>The link destination may not be omitted:</p>
<div id="example-106" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 106
</div>
<pre class="markdown"><code>[foo]:
[foo]</code></pre>
<pre class="html"><code>&lt;p&gt;[foo]:&lt;/p&gt;
&lt;p&gt;[foo]&lt;/p&gt;</code></pre>
</div>
<p>A link can come before its corresponding definition:</p>
<div id="example-107" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 107
</div>
<pre class="markdown"><code>[foo]
[foo]: url</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;url&quot;&gt;foo&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<p>If there are several matching definitions, the first one takes precedence:</p>
<div id="example-108" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 108
</div>
<pre class="markdown"><code>[foo]
[foo]: first
[foo]: second</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;first&quot;&gt;foo&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<p>As noted in the section on <a href="#links">Links</a>, matching of labels is case-insensitive (see <a href="#matches">matches</a>).</p>
<div id="example-109" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 109
</div>
<pre class="markdown"><code>[FOO]: /url
[Foo]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/url&quot;&gt;Foo&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<div id="example-110" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 110
</div>
<pre class="markdown"><code>[ΑΓΩ]: /φου
[αγω]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/φου&quot;&gt;αγω&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<p>Here is a link reference definition with no corresponding link. It contributes nothing to the document.</p>
<div id="example-111" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 111
</div>
<pre class="markdown"><code>[foo]: /url</code></pre>
<pre class="html"><code></code></pre>
</div>
<p>This is not a link reference definition, because there are non-space characters after the title:</p>
<div id="example-112" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 112
</div>
<pre class="markdown"><code>[foo]: /url &quot;title&quot; ok</code></pre>
<pre class="html"><code>&lt;p&gt;[foo]: /url &amp;quot;title&amp;quot; ok&lt;/p&gt;</code></pre>
</div>
<p>This is not a link reference definition, because it is indented four spaces:</p>
<div id="example-113" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 113
</div>
<pre class="markdown"><code> [foo]: /url &quot;title&quot;
[foo]</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;[foo]: /url &amp;quot;title&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;[foo]&lt;/p&gt;</code></pre>
</div>
<p>This is not a link reference definition, because it occurs inside a code block:</p>
<div id="example-114" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 114
</div>
<pre class="markdown"><code>```
[foo]: /url
```
[foo]</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;[foo]: /url
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;[foo]&lt;/p&gt;</code></pre>
</div>
<p>A <a href="#link-reference-definition">link reference definition</a> cannot interrupt a paragraph.</p>
<div id="example-115" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 115
</div>
<pre class="markdown"><code>Foo
[bar]: /baz
[bar]</code></pre>
<pre class="html"><code>&lt;p&gt;Foo
[bar]: /baz&lt;/p&gt;
&lt;p&gt;[bar]&lt;/p&gt;</code></pre>
</div>
<p>However, it can directly follow other block elements, such as headers and horizontal rules, and it need not be followed by a blank line.</p>
<div id="example-116" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 116
</div>
<pre class="markdown"><code># [Foo]
[foo]: /url
&gt; bar</code></pre>
<pre class="html"><code>&lt;h1&gt;&lt;a href=&quot;/url&quot;&gt;Foo&lt;/a&gt;&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;bar&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>Several <a href="#link-reference">link references</a> can occur one after another, without intervening blank lines.</p>
<div id="example-117" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 117
</div>
<pre class="markdown"><code>[foo]: /foo-url &quot;foo&quot;
[bar]: /bar-url
&quot;bar&quot;
[baz]: /baz-url
[foo],
[bar],
[baz]</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/foo-url&quot; title=&quot;foo&quot;&gt;foo&lt;/a&gt;,
&lt;a href=&quot;/bar-url&quot; title=&quot;bar&quot;&gt;bar&lt;/a&gt;,
&lt;a href=&quot;/baz-url&quot;&gt;baz&lt;/a&gt;&lt;/p&gt;</code></pre>
</div>
<p><a href="#link-reference-definition">Link reference definitions</a> can occur inside block containers, like lists and block quotations. They affect the entire document, not just the container in which they are defined:</p>
<div id="example-118" class="example" data-section="Link reference definitions">
<div class="examplenum">
Example 118
</div>
<pre class="markdown"><code>[foo]
&gt; [foo]: /url</code></pre>
<pre class="html"><code>&lt;p&gt;&lt;a href=&quot;/url&quot;&gt;foo&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<h2 id="paragraphs"><span class="header-section-number">4.8</span> Paragraphs</h2>
<p>A sequence of non-blank lines that cannot be interpreted as other kinds of blocks forms a <a href="#paragraph">paragraph</a>.<a id="paragraph"></a> The contents of the paragraph are the result of parsing the paragraph’s raw content as inlines. The paragraph’s raw content is formed by concatenating the lines and removing initial and final spaces.</p>
<p>A simple example with two paragraphs:</p>
<div id="example-119" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 119
</div>
<pre class="markdown"><code>aaa
bbb</code></pre>
<pre class="html"><code>&lt;p&gt;aaa&lt;/p&gt;
&lt;p&gt;bbb&lt;/p&gt;</code></pre>
</div>
<p>Paragraphs can contain multiple lines, but no blank lines:</p>
<div id="example-120" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 120
</div>
<pre class="markdown"><code>aaa
bbb
ccc
ddd</code></pre>
<pre class="html"><code>&lt;p&gt;aaa
bbb&lt;/p&gt;
&lt;p&gt;ccc
ddd&lt;/p&gt;</code></pre>
</div>
<p>Multiple blank lines between paragraph have no effect:</p>
<div id="example-121" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 121
</div>
<pre class="markdown"><code>aaa
bbb</code></pre>
<pre class="html"><code>&lt;p&gt;aaa&lt;/p&gt;
&lt;p&gt;bbb&lt;/p&gt;</code></pre>
</div>
<p>Leading spaces are skipped:</p>
<div id="example-122" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 122
</div>
<pre class="markdown"><code> aaa
bbb</code></pre>
<pre class="html"><code>&lt;p&gt;aaa
bbb&lt;/p&gt;</code></pre>
</div>
<p>Lines after the first may be indented any amount, since indented code blocks cannot interrupt paragraphs.</p>
<div id="example-123" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 123
</div>
<pre class="markdown"><code>aaa
bbb
ccc</code></pre>
<pre class="html"><code>&lt;p&gt;aaa
bbb
ccc&lt;/p&gt;</code></pre>
</div>
<p>However, the first line may be indented at most three spaces, or an indented code block will be triggered:</p>
<div id="example-124" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 124
</div>
<pre class="markdown"><code> aaa
bbb</code></pre>
<pre class="html"><code>&lt;p&gt;aaa
bbb&lt;/p&gt;</code></pre>
</div>
<div id="example-125" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 125
</div>
<pre class="markdown"><code> aaa
bbb</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;aaa
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;bbb&lt;/p&gt;</code></pre>
</div>
<p>Final spaces are stripped before inline parsing, so a paragraph that ends with two or more spaces will not end with a hard line break:</p>
<div id="example-126" class="example" data-section="Paragraphs">
<div class="examplenum">
Example 126
</div>
<pre class="markdown"><code>aaa
bbb </code></pre>
<pre class="html"><code>&lt;p&gt;aaa&lt;br /&gt;
bbb&lt;/p&gt;</code></pre>
</div>
<h2 id="blank-lines"><span class="header-section-number">4.9</span> Blank lines</h2>
<p><a href="#blank-line">Blank lines</a> between block-level elements are ignored, except for the role they play in determining whether a <a href="#list">list</a> is <a href="#tight">tight</a> or <a href="#loose">loose</a>.</p>
<p>Blank lines at the beginning and end of the document are also ignored.</p>
<div id="example-127" class="example" data-section="Blank lines">
<div class="examplenum">
Example 127
</div>
<pre class="markdown"><code>
aaa
# aaa
</code></pre>
<pre class="html"><code>&lt;p&gt;aaa&lt;/p&gt;
&lt;h1&gt;aaa&lt;/h1&gt;</code></pre>
</div>
<h1 id="container-blocks"><span class="header-section-number">5</span> Container blocks</h1>
<p>A <a href="#container-block">container block</a> is a block that has other blocks as its contents. There are two basic kinds of container blocks: <a href="#block-quote">block quotes</a> and <a href="#list-item">list items</a>. <a href="#list">Lists</a> are meta-containers for <a href="#list-item">list items</a>.</p>
<p>We define the syntax for container blocks recursively. The general form of the definition is:</p>
<blockquote>
<p>If X is a sequence of blocks, then the result of transforming X in such-and-such a way is a container of type Y with these blocks as its content.</p>
</blockquote>
<p>So, we explain what counts as a block quote or list item by explaining how these can be <em>generated</em> from their contents. This should suffice to define the syntax, although it does not give a recipe for <em>parsing</em> these constructions. (A recipe is provided below in the section entitled [A parsing strategy].)</p>
<h2 id="block-quotes"><span class="header-section-number">5.1</span> Block quotes</h2>
<p>A <a href="#block-quote-marker">block quote marker</a> <a id="block-quote-marker"></a> consists of 0-3 spaces of initial indent, plus (a) the character <code>&gt;</code> together with a following space, or (b) a single character <code>&gt;</code> not followed by a space.</p>
<p>The following rules define <a href="#block-quote">block quotes</a>: <a id="block-quote"></a></p>
<ol style="list-style-type: decimal">
<li><p><strong>Basic case.</strong> If a string of lines <em>Ls</em> constitute a sequence of blocks <em>Bs</em>, then the result of appending a [block quote marker] to the beginning of each line in <em>Ls</em> is a <a href="#block-quote">block quote</a> containing <em>Bs</em>.</p></li>
<li><p><strong>Laziness.</strong> If a string of lines <em>Ls</em> constitute a <a href="#block-quote">block quote</a> with contents <em>Bs</em>, then the result of deleting the initial <a href="#block-quote-marker">block quote marker</a> from one or more lines in which the next non-space character after the <a href="#block-quote-marker">block quote marker</a> is <a href="#paragraph-continuation-text">paragraph continuation text</a> is a block quote with <em>Bs</em> as its content. <a id="paragraph-continuation-text"></a> <a href="#paragraph-continuation-text">Paragraph continuation text</a> is text that will be parsed as part of the content of a paragraph, but does not occur at the beginning of the paragraph.</p></li>
<li><p><strong>Consecutiveness.</strong> A document cannot contain two <a href="#block-quote">block quotes</a> in a row unless there is a <a href="#blank-line">blank line</a> between them.</p></li>
</ol>
<p>Nothing else counts as a <a href="#block-quote">block quote</a>.</p>
<p>Here is a simple example:</p>
<div id="example-128" class="example" data-section="Block quotes">
<div class="examplenum">
Example 128
</div>
<pre class="markdown"><code>&gt; # Foo
&gt; bar
&gt; baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;h1&gt;Foo&lt;/h1&gt;
&lt;p&gt;bar
baz&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>The spaces after the <code>&gt;</code> characters can be omitted:</p>
<div id="example-129" class="example" data-section="Block quotes">
<div class="examplenum">
Example 129
</div>
<pre class="markdown"><code>&gt;# Foo
&gt;bar
&gt; baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;h1&gt;Foo&lt;/h1&gt;
&lt;p&gt;bar
baz&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>The <code>&gt;</code> characters can be indented 1-3 spaces:</p>
<div id="example-130" class="example" data-section="Block quotes">
<div class="examplenum">
Example 130
</div>
<pre class="markdown"><code> &gt; # Foo
&gt; bar
&gt; baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;h1&gt;Foo&lt;/h1&gt;
&lt;p&gt;bar
baz&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>Four spaces gives us a code block:</p>
<div id="example-131" class="example" data-section="Block quotes">
<div class="examplenum">
Example 131
</div>
<pre class="markdown"><code> &gt; # Foo
&gt; bar
&gt; baz</code></pre>
<pre class="html"><code>&lt;pre&gt;&lt;code&gt;&amp;gt; # Foo
&amp;gt; bar
&amp;gt; baz
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>The Laziness clause allows us to omit the <code>&gt;</code> before a paragraph continuation line:</p>
<div id="example-132" class="example" data-section="Block quotes">
<div class="examplenum">
Example 132
</div>
<pre class="markdown"><code>&gt; # Foo
&gt; bar
baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;h1&gt;Foo&lt;/h1&gt;
&lt;p&gt;bar
baz&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>A block quote can contain some lazy and some non-lazy continuation lines:</p>
<div id="example-133" class="example" data-section="Block quotes">
<div class="examplenum">
Example 133
</div>
<pre class="markdown"><code>&gt; bar
baz
&gt; foo</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;bar
baz
foo&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>Laziness only applies to lines that are continuations of paragraphs. Lines containing characters or indentation that indicate block structure cannot be lazy.</p>
<div id="example-134" class="example" data-section="Block quotes">
<div class="examplenum">
Example 134
</div>
<pre class="markdown"><code>&gt; foo
---</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr /&gt;</code></pre>
</div>
<div id="example-135" class="example" data-section="Block quotes">
<div class="examplenum">
Example 135
</div>
<pre class="markdown"><code>&gt; - foo
- bar</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;foo&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;bar&lt;/li&gt;
&lt;/ul&gt;</code></pre>
</div>
<div id="example-136" class="example" data-section="Block quotes">
<div class="examplenum">
Example 136
</div>
<pre class="markdown"><code>&gt; foo
bar</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;pre&gt;&lt;code&gt;foo
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;pre&gt;&lt;code&gt;bar
&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<div id="example-137" class="example" data-section="Block quotes">
<div class="examplenum">
Example 137
</div>
<pre class="markdown"><code>&gt; ```
foo
```</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;</code></pre>
</div>
<p>A block quote can be empty:</p>
<div id="example-138" class="example" data-section="Block quotes">
<div class="examplenum">
Example 138
</div>
<pre class="markdown"><code>&gt;</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<div id="example-139" class="example" data-section="Block quotes">
<div class="examplenum">
Example 139
</div>
<pre class="markdown"><code>&gt;
&gt;
&gt; </code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>A block quote can have initial or final blank lines:</p>
<div id="example-140" class="example" data-section="Block quotes">
<div class="examplenum">
Example 140
</div>
<pre class="markdown"><code>&gt;
&gt; foo
&gt; </code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>A blank line always separates block quotes:</p>
<div id="example-141" class="example" data-section="Block quotes">
<div class="examplenum">
Example 141
</div>
<pre class="markdown"><code>&gt; foo
&gt; bar</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;bar&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>(Most current Markdown implementations, including John Gruber’s original <code>Markdown.pl</code>, will parse this example as a single block quote with two paragraphs. But it seems better to allow the author to decide whether two block quotes or one are wanted.)</p>
<p>Consecutiveness means that if we put these block quotes together, we get a single block quote:</p>
<div id="example-142" class="example" data-section="Block quotes">
<div class="examplenum">
Example 142
</div>
<pre class="markdown"><code>&gt; foo
&gt; bar</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;foo
bar&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>To get a block quote with two paragraphs, use:</p>
<div id="example-143" class="example" data-section="Block quotes">
<div class="examplenum">
Example 143
</div>
<pre class="markdown"><code>&gt; foo
&gt;
&gt; bar</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;foo&lt;/p&gt;
&lt;p&gt;bar&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>Block quotes can interrupt paragraphs:</p>
<div id="example-144" class="example" data-section="Block quotes">
<div class="examplenum">
Example 144
</div>
<pre class="markdown"><code>foo
&gt; bar</code></pre>
<pre class="html"><code>&lt;p&gt;foo&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;bar&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>In general, blank lines are not needed before or after block quotes:</p>
<div id="example-145" class="example" data-section="Block quotes">
<div class="examplenum">
Example 145
</div>
<pre class="markdown"><code>&gt; aaa
***
&gt; bbb</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;aaa&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr /&gt;
&lt;blockquote&gt;
&lt;p&gt;bbb&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>However, because of laziness, a blank line is needed between a block quote and a following paragraph:</p>
<div id="example-146" class="example" data-section="Block quotes">
<div class="examplenum">
Example 146
</div>
<pre class="markdown"><code>&gt; bar
baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;bar
baz&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<div id="example-147" class="example" data-section="Block quotes">
<div class="examplenum">
Example 147
</div>
<pre class="markdown"><code>&gt; bar
baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;bar&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;baz&lt;/p&gt;</code></pre>
</div>
<div id="example-148" class="example" data-section="Block quotes">
<div class="examplenum">
Example 148
</div>
<pre class="markdown"><code>&gt; bar
&gt;
baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;p&gt;bar&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;baz&lt;/p&gt;</code></pre>
</div>
<p>It is a consequence of the Laziness rule that any number of initial <code>&gt;</code>s may be omitted on a continuation line of a nested block quote:</p>
<div id="example-149" class="example" data-section="Block quotes">
<div class="examplenum">
Example 149
</div>
<pre class="markdown"><code>&gt; &gt; &gt; foo
bar</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;foo
bar&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<div id="example-150" class="example" data-section="Block quotes">
<div class="examplenum">
Example 150
</div>
<pre class="markdown"><code>&gt;&gt;&gt; foo
&gt; bar
&gt;&gt;baz</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;foo
bar
baz&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<p>When including an indented code block in a block quote, remember that the <a href="#block-quote-marker">block quote marker</a> includes both the <code>&gt;</code> and a following space. So <em>five spaces</em> are needed after the <code>&gt;</code>:</p>
<div id="example-151" class="example" data-section="Block quotes">
<div class="examplenum">
Example 151
</div>
<pre class="markdown"><code>&gt; code
&gt; not code</code></pre>
<pre class="html"><code>&lt;blockquote&gt;
&lt;pre&gt;&lt;code&gt;code
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;not code&lt;/p&gt;
&lt;/blockquote&gt;</code></pre>
</div>
<h2 id="list-items"><span class="header-section-number">5.2</span> List items</h2>
<p>A <a href="#list-marker">list marker</a> <a id="list-marker"></a> is a <