kramdown

fast, pure-Ruby Markdown-superset converter

Quick Reference

Below are examples of all available structural elements that can be used in a kramdown text. Since the kramdown syntax is a superset of the Markdown syntax, only a small part of the available syntax is not available in standard Markdown syntax. Note, that only the most basic syntax information is given. However, a link to the detailed syntax for each element is provided (which also details the differences to the standard Markdown syntax). The quick reference is for version 2.4.0 of the syntax documentation.

kramdown has two main classes of elements: block and span-level elements. Block-level elements are used to create paragraphs, headers, lists and so on whereas span-level elements are used to markup text phrases as emphasized, as a link and so on.

All examples below feature the kramdown source, the converted HTML source (shown when hovering over the kramdown source) and the output as it appears in the browser. This looks like this:

kramdown example code
Example code converted to HTML
Live browser view of example code

Block-level Elements - Main Structural Elements

Paragraphs

Consecutive lines of text are considered to be one paragraph. As with other block level elements you have to add a blank line to separate it from the following block-level element:

The first paragraph.

Another paragraph
<p>The first paragraph.</p>

<p>Another paragraph</p>

The first paragraph.

Another paragraph

Explicit line breaks in a paragraph can be made by using two spaces or two backslashes at the end of a line:

This is a paragraph  
which contains a hard line break.
<p>This is a paragraph<br />
which contains a hard line break.</p>

This is a paragraph
which contains a hard line break.

Headers

kramdown supports Setext style headers and atx style headers. A header must always be preceded by a blank line except at the beginning of the document:

First level header
==================

Second level header
-------------------
<h1>First level header</h1>

<h2>Second level header</h2>

First level header

Second level header

# H1 header

## H2 header

### H3 header

#### H4 header

##### H5 header

###### H6 header
<h1>H1 header</h1>

<h2>H2 header</h2>

<h3>H3 header</h3>

<h4>H4 header</h4>

<h5>H5 header</h5>

<h6>H6 header</h6>

H1 header

H2 header

H3 header

H4 header

H5 header
H6 header

If you set the option auto_ids to false (for example, by using the options extension, see Extensions), then the automatic header ID generation is turned off:

{::options auto_ids="false" /}

# A header without an ID

<h1>A header without an ID</h1>

A header without an ID

Blockquotes

A blockquote is started using the > marker followed by an optional space; all following lines that are also started with the blockquote marker belong to the blockquote. You can use any block-level elements inside a blockquote:

> A sample blockquote.
>
> >Nested blockquotes are
> >also possible.
>
> ## Headers work too
> This is the outer quote again.
<blockquote>
  <p>A sample blockquote.</p>

  <blockquote>
    <p>Nested blockquotes are
also possible.</p>
  </blockquote>

  <h2>Headers work too</h2>
  <p>This is the outer quote again.</p>
</blockquote>

A sample blockquote.

Nested blockquotes are also possible.

Headers work too

This is the outer quote again.

You may also be lazy with the > markers as long as there is no blank line:

> This is a blockquote
continued on this
and this line.

But this is a separate paragraph.
<blockquote>
  <p>This is a blockquote
continued on this
and this line.</p>
</blockquote>

<p>But this is a separate paragraph.</p>

This is a blockquote continued on this and this line.

But this is a separate paragraph.

Code Blocks

kramdown supports two different code block styles. One uses lines indented with either four spaces or one tab whereas the other uses lines with tilde characters as delimiters – therefore the content does not need to be indented:

This is a sample code block.

    Continued here.
<p>This is a sample code block.</p>

<pre><code>Continued here.
</code></pre>

This is a sample code block.

Continued here.
~~~~~~
This is also a code block.
~~~
Ending lines must have at least as
many tildes as the starting line.
~~~~~~~~~~~~
<pre><code>This is also a code block.
~~~
Ending lines must have at least as
many tildes as the starting line.
</code></pre>

This is also a code block.
~~~
Ending lines must have at least as
many tildes as the starting line.

The following is a code block with a language specified:

~~~ ruby
def what?
  42
end
~~~
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">what?</span>
  <span class="mi">42</span>
<span class="k">end</span>
</code></pre></div></div>

def what?
  42
end

Horizontal Rules

It is easy to insert a horizontal rule in kramdown: just use three or more asterisks, dashes or underscores, optionally separated by spaces or tabs, on an otherwise blank line:

* * *

---

  _  _  _  _

---------------
<hr />

<hr />

<hr />

<hr />





Lists

kramdown supports ordered and unordered lists. Ordered lists are started by using a number followed by a period, a space and then the list item text. The content of a list item consists of block-level elements. All lines which have the same indent as the text of the line with the list marker belong to the list item:

1. This is a list item
2. And another item
2. And the third one
   with additional text
<ol>
  <li>This is a list item</li>
  <li>And another item</li>
  <li>And the third one
with additional text</li>
</ol>

  1. This is a list item
  2. And another item
  3. And the third one with additional text

As with block quotes, you may be lazy when using the list item marker:

* A list item
with additional text
<ul>
  <li>A list item
with additional text</li>
</ul>

  • A list item with additional text

As the content consists of block-level elements you can do things like the following:

1.  This is a list item

    > with a blockquote

    # And a header

2.  Followed by another item
<ol>
  <li>
    <p>This is a list item</p>

    <blockquote>
      <p>with a blockquote</p>
    </blockquote>

    <h1>And a header</h1>
  </li>
  <li>
    <p>Followed by another item</p>
  </li>
</ol>

  1. This is a list item

    with a blockquote

    And a header

  2. Followed by another item

Nested lists are also easy to create:

1. Item one
   1. sub item one
   2. sub item two
   3. sub item three
2. Item two
<ol>
  <li>Item one
    <ol>
      <li>sub item one</li>
      <li>sub item two</li>
      <li>sub item three</li>
    </ol>
  </li>
  <li>Item two</li>
</ol>

  1. Item one
    1. sub item one
    2. sub item two
    3. sub item three
  2. Item two

Lists can occur directly after other block-level elements, however, there has to be at least one blank line if you want to follow a paragraph with a list:

This is a paragraph.
1. This is NOT a list.

1. This is a list!
<p>This is a paragraph.
1. This is NOT a list.</p>

<ol>
  <li>This is a list!</li>
</ol>

This is a paragraph. 1. This is NOT a list.

  1. This is a list!

Unordered lists are started by using an asterisk, a dash or a plus sign (they can be mixed) and a space. Apart from that unordered lists follow the same rules as ordered lists:

* Item one
+ Item two
- Item three
<ul>
  <li>Item one</li>
  <li>Item two</li>
  <li>Item three</li>
</ul>

  • Item one
  • Item two
  • Item three

Definition Lists

A definition list works similar to a normal list and is used to associate definitions with terms. Definition lists are started when a normal paragraph is followed by a line starting with a colon and then the definition text. One term can have many definitions and multiple terms can have the same definition. Each line of the preceding paragraph is assumed to contain one term, for example:

term
: definition
: another definition

another term
and another term
: and a definition for the term
<dl>
  <dt>term</dt>
  <dd>definition</dd>
  <dd>another definition</dd>
  <dt>another term</dt>
  <dt>and another term</dt>
  <dd>and a definition for the term</dd>
</dl>

term
definition
another definition
another term
and another term
and a definition for the term

If you insert a blank line before a definition (note: there must only be one blank line between the terms and the first definition), the definition will be wrapped in a paragraph:

term

: definition
: definition
<dl>
  <dt>term</dt>
  <dd>
    <p>definition</p>
  </dd>
  <dd>definition</dd>
</dl>

term

definition

definition

Each term can be styled using span-level elements and each definition is parsed as block-level elements, i.e. you can use any block-level in a definition. Just use the same indent for the lines following the definition line:

This *is* a term

: This will be a para

  > a blockquote

  # A header
<dl>
  <dt>This <em>is</em> a term</dt>
  <dd>
    <p>This will be a para</p>

    <blockquote>
      <p>a blockquote</p>
    </blockquote>

    <h1>A header</h1>
  </dd>
</dl>

This is a term

This will be a para

a blockquote

A header

Tables

kramdown supports a syntax for creating simple tables. A line starting with a pipe character (|) starts a table row. However, if the pipe characters is immediately followed by a dash (-), a separator line is created. Separator lines are used to split the table header from the table body (and optionally align the table columns) and to split the table body into multiple parts. If the pipe character is followed by an equal sign (=), the tables rows below it are part of the table footer.

| A simple | table |
| with multiple | lines|
<table>
  <tbody>
    <tr>
      <td>A simple</td>
      <td>table</td>
    </tr>
    <tr>
      <td>with multiple</td>
      <td>lines</td>
    </tr>
  </tbody>
</table>

A simple table
with multiple lines
| Header1 | Header2 | Header3 |
|:--------|:-------:|--------:|
| cell1   | cell2   | cell3   |
| cell4   | cell5   | cell6   |
|----
| cell1   | cell2   | cell3   |
| cell4   | cell5   | cell6   |
|=====
| Foot1   | Foot2   | Foot3
{: rules="groups"}
<table rules="groups">
  <thead>
    <tr>
      <th style="text-align: left">Header1</th>
      <th style="text-align: center">Header2</th>
      <th style="text-align: right">Header3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="text-align: left">cell1</td>
      <td style="text-align: center">cell2</td>
      <td style="text-align: right">cell3</td>
    </tr>
    <tr>
      <td style="text-align: left">cell4</td>
      <td style="text-align: center">cell5</td>
      <td style="text-align: right">cell6</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td style="text-align: left">cell1</td>
      <td style="text-align: center">cell2</td>
      <td style="text-align: right">cell3</td>
    </tr>
    <tr>
      <td style="text-align: left">cell4</td>
      <td style="text-align: center">cell5</td>
      <td style="text-align: right">cell6</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td style="text-align: left">Foot1</td>
      <td style="text-align: center">Foot2</td>
      <td style="text-align: right">Foot3</td>
    </tr>
  </tfoot>
</table>

Header1 Header2 Header3
cell1 cell2 cell3
cell4 cell5 cell6
cell1 cell2 cell3
cell4 cell5 cell6
Foot1 Foot2 Foot3

HTML elements

kramdown allows you to use block-level HTML tags (div, p, pre, …) to markup whole blocks of text – just start a line with a block-level HTML tag. kramdown syntax is normally not processed inside an HTML tag but this can be changed with the parse_block_html option. If this options is set to true, then the content of a block-level HTML tag is parsed by kramdown either as block level or span-level text, depending on the tag:

<div style="float: right">
Something that stays right and is not wrapped in a para.
</div>

{::options parse_block_html="true" /}

<div>
This is wrapped in a para.
</div>
<p>
This can contain only *span* level elements.
</p>
<div style="float: right">
Something that stays right and is not wrapped in a para.
</div>

<div>
  <p>This is wrapped in a para.</p>
</div>
<p>
This can contain only <em>span</em> level elements.
</p>

Something that stays right and is not wrapped in a para.

This is wrapped in a para.

This can contain only span level elements.

Block Attributes

You can assign any attribute to a block-level element. Just directly follow the block with a block inline attribute list (or short: block IAL). A block IAL consists of a left curly brace, followed by a colon, the attribute definitions and a right curly brace. Here is a simple example which sets the title attribute of a block quote:

> A nice blockquote
{: title="Blockquote title"}
<blockquote title="Blockquote title">
  <p>A nice blockquote</p>
</blockquote>

A nice blockquote

As one often wants to set one or more CSS classes on an element, there is an easy shortcut:

> A nice blockquote
{: .class1 .class2}
<blockquote class="class1 class2">
  <p>A nice blockquote</p>
</blockquote>

A nice blockquote

A shortcut for setting the ID is also provided. Just prefix the ID with a hash symbol:

> A nice blockquote
{: #with-an-id}
<blockquote id="with-an-id">
  <p>A nice blockquote</p>
</blockquote>

A nice blockquote

Sometimes one wants to use the same attributes for many elements. kramdown allows you to define the attributes in one place with an attribute list definition (or short: ALD) and just reference this definition in a block IAL. An ALD has the same structure as a block IAL but the colon has to be replace with a colon, the reference name and another colon. By just using the reference name as-is in a block IAL, one can include the attributes of the referenced ALD:

{:refdef: .c1 #id .c2 title="title"}
paragraph
{: refdef}
<p class="c1 c2" id="id" title="title">paragraph</p>

paragraph

The order in a block IAL or ALD is important because later defined attributes overwrite (with the exception of the shortcut for CSS classes) prior defined attributes:

{:refdef: .c1 #id .c2 title="title"}
paragraph
{: refdef .c3 title="t" #para}
<p class="c1 c2 c3" id="para" title="t">paragraph</p>

paragraph

Extensions

kramdown provides some less used functionality through a common syntax. This will allow the easy addition of other extensions if need arises. Currently, there are extensions for ignoring text (i.e. treating text as comment), for inserting arbitrary text as-is into the output and for setting kramdown options.

Here is an example that shows how to insert comments into text:

This is a paragraph
{::comment}
This is a comment which is
completely ignored.
{:/comment}
... paragraph continues here.

Extensions can also be used
inline {::nomarkdown}**see**{:/}!
<p>This is a paragraph
<!-- 
This is a comment which is
completely ignored.
 -->
… paragraph continues here.</p>

<p>Extensions can also be used
inline **see**!</p>

This is a paragraph … paragraph continues here.

Extensions can also be used inline **see**!

As one can see from the above example, the syntax for extensions is nearly identical to that of ALDs. However, there is no trailing colon after the extension name and the extension end tag needs a slash between the colon and the extension name. One can also use the short form of the end tag, i.e. {:/}. Attribute definitions can be specified on the start tag by separating them with a space from the extension name. Also, if the extension does not have a body, there needs to be a slash right before the closing brace:

{::options auto_ids="false" /}

# Header without id

<h1>Header without id</h1>

Header without id

Span-Level Elements - Text Modifiers

Emphasis

Emphasis can be added to text by surrounding the text with either asterisks or underscores:

This is *emphasized*,
_this_ too!
<p>This is <em>emphasized</em>,
<em>this</em> too!</p>

This is emphasized, this too!

Strong emphasis can be done by doubling the delimiters:

This is **strong**,
__this__ too!
<p>This is <strong>strong</strong>,
<strong>this</strong> too!</p>

This is strong, this too!

The form with the asterisks can also be used to markup parts of words:

This w**ork**s as expected!
<p>This w<strong>ork</strong>s as expected!</p>

This works as expected!

A simple link can be created by surrounding the text with square brackets and the link URL with parentheses:

A [link](http://kramdown.gettalong.org)
to the kramdown homepage.
<p>A <a href="http://kramdown.gettalong.org">link</a>
to the kramdown homepage.</p>

A link to the kramdown homepage.

You can also add title information to the link:

A [link](http://kramdown.gettalong.org "hp")
to the homepage.
<p>A <a href="http://kramdown.gettalong.org" title="hp">link</a>
to the homepage.</p>

A link to the homepage.

There is another way to create links which does not interrupt the text flow. The URL and title are defined using a reference name and this reference name is then used in square brackets instead of the link URL:

A [link][kramdown hp]
to the homepage.

[kramdown hp]: http://kramdown.gettalong.org "hp"
<p>A <a href="http://kramdown.gettalong.org" title="hp">link</a>
to the homepage.</p>


A link to the homepage.

If the link text itself is the reference name, the second set of square brackets can be omitted:

A link to the [kramdown hp].

[kramdown hp]: http://kramdown.gettalong.org "hp"
<p>A link to the <a href="http://kramdown.gettalong.org" title="hp">kramdown hp</a>.</p>


A link to the kramdown hp.

Images can be created in a similar way: just use an exclamation mark before the square brackets. The link text will become the alternative text of the image and the link URL specifies the image source:

An image: ![gras](img/image.jpg)
<p>An image: <img src="img/image.jpg" alt="gras" /></p>

An image: gras

Inline Code

Text phrases can be easily marked up as code by surrounding them with backticks:

Use `Kramdown::Document.new(text).to_html`
to convert the `text` in kramdown
syntax to HTML.
<p>Use <code>Kramdown::Document.new(text).to_html</code>
to convert the <code>text</code> in kramdown
syntax to HTML.</p>

Use Kramdown::Document.new(text).to_html to convert the text in kramdown syntax to HTML.

If you want to use literal backticks in your code, just use two or more backticks as delimiters. The space right after the beginning delimiter and the one right before the closing delimiter are ignored:

Use backticks to markup code,
e.g. `` `code` ``.
<p>Use backticks to markup code,
e.g. <code>`code`</code>.</p>

Use backticks to markup code, e.g. `code`.

Footnotes

Footnotes can easily be used in kramdown. Just set a footnote marker (consists of square brackets with a caret and the footnote name inside) in the text and somewhere else the footnote definition (which basically looks like a reference link definition):

This is a text with a
footnote[^1].

[^1]: And here is the definition.
<p>This is a text with a
footnote<sup id="fnref:1" role="doc-noteref"><a href="#fn:1" class="footnote" rel="footnote">1</a></sup>.</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1" role="doc-endnote">
      <p>And here is the definition. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>

This is a text with a footnote1.

  1. And here is the definition. 

The footnote definition can contain any block-level element, all lines following a footnote definition indented with four spaces or one tab belong to the definition:

This is a text with a
footnote[^2].

[^2]:
    And here is the definition.

    > With a quote!
<p>This is a text with a
footnote<sup id="fnref:2" role="doc-noteref"><a href="#fn:2" class="footnote" rel="footnote">1</a></sup>.</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:2" role="doc-endnote">

      <p>And here is the definition.</p>

      <blockquote>
        <p>With a quote!</p>
      </blockquote>
      <p><a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;</a></p>
    </li>
  </ol>
</div>

This is a text with a footnote1.

  1. And here is the definition.

    With a quote!

As can be seen above the footnote name is only used for the anchors and the numbering is done automatically in document order. Repeated footnote markers will link to the same footnote definition.

Abbreviations

Abbreviations will work out of the box once you add an abbreviation definition. So you can just write the text and add the definitions later on.

This is an HTML
example.

*[HTML]: Hyper Text Markup Language
<p>This is an <abbr title="Hyper Text Markup Language">HTML</abbr>
example.</p>


This is an HTML example.

HTML Elements

HTML is not only supported on the block-level but also on the span-level:

This is <span style="color: red">written in
red</span>.
<p>This is <span style="color: red">written in
red</span>.</p>

This is written in red.

Inline Attributes

As with a block-level element you can assign any attribute to a span-level elements using a span inline attribute list (or short: span IAL). A span IAL has the same syntax as a block IAL and must immediately follow the span-level element:

This is *red*{: style="color: red"}.
<p>This is <em style="color: red">red</em>.</p>

This is red.