References/

Markdown Syntax

Overview

Philosophy

Markdown is intended to be as easy-to-read and easy-to-write as is feasible.

Readability, however, is emphasized above all else. A Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions. While Markdown’s syntax has been influenced by several existing text-to-HTML filters – including [Setext] 1, [atx] 2, [Textile] 3, [reStructuredText] 4, [Grutatext] 5, and [EtText] 6 – the single biggest source of inspiration for Markdown’s syntax is the format of plain text email.

To this end, Markdown’s syntax is comprised entirely of punctuation characters, which punctuation characters have been carefully chosen so as to look like what they mean. E.g., asterisks around a word actually look like *emphasis*. Markdown lists look like, well, lists. Even blockquotes look like quoted passages of text, assuming you’ve ever used email.

Inline HTML

Markdown’s syntax is intended for one purpose: to be used as a format for writing for the web.

Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags. The idea is not to create a syntax that makes it easier to insert HTML tags. In my opinion, HTML tags are already easy to insert. The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself. There’s no need to preface it or delimit it to indicate that you’re switching from Markdown to HTML; you just use the tags.

The only restrictions are that block-level HTML elements – e.g. <div>, <table>, <pre>, <p>, 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. Markdown is smart enough not to add extra (unwanted) <p> tags around HTML block-level tags.

For example, to add an HTML table to a Markdown article:

This is a regular paragraph.

<table>
    <tr>
        <td>Foo</td>
    </tr>
</table>

This is another regular paragraph.

Note that Markdown formatting syntax is not processed within block-level HTML tags. E.g., you can’t use Markdown-style *emphasis* inside an HTML block.

Span-level HTML tags – e.g. <span>, <cite>, or <del> – can be used anywhere in a Markdown paragraph, list item, or header. If you want, you can even use HTML tags instead of Markdown formatting; e.g. if you’d prefer to use HTML <a> or <img> tags instead of Markdown’s link or image syntax, go right ahead.

Unlike block-level HTML tags, Markdown syntax is processed within span-level tags.

Automatic Escaping for Special Characters

In HTML, there are two characters that demand special treatment: < and &. Left angle brackets are used to start tags; ampersands are used to denote HTML entities. If you want to use them as literal characters, you must escape them as entities, e.g. &lt;, and &amp;.

Ampersands in particular are bedeviling for web writers. If you want to write about ‘AT&T’, you need to write ‘AT&amp;T’. You even need to escape ampersands within URLs. Thus, if you want to link to:

http://images.google.com/images?num=30&q=larry+bird

you need to encode the URL as:

http://images.google.com/images?num=30&amp;q=larry+bird

in your anchor tag href attribute. Needless to say, this is easy to forget, and is probably the single most common source of HTML validation errors in otherwise well-marked-up web sites.

Markdown allows you to use these characters naturally, taking care of all the necessary escaping for you. If you use an ampersand as part of an HTML entity, it remains unchanged; otherwise it will be translated into &amp;.

So, if you want to include a copyright symbol in your article, you can write:

&copy;

and Markdown will leave it alone. But if you write:

AT&T

Markdown will translate it to:

AT&amp;T

Similarly, because Markdown supports inline HTML, if you use angle brackets as delimiters for HTML tags, Markdown will treat them as such. But if you write:

4 < 5

Markdown will translate it to:

4 &lt; 5

However, inside Markdown code spans and blocks, angle brackets and ampersands are always encoded automatically. This makes it easy to use Markdown to write about HTML code. (As opposed to raw HTML, which is a terrible format for writing about HTML syntax, because every single < and & in your example code needs to be escaped.)


Block Elements

Paragraphs and Line Breaks

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines. (A blank line is any line that looks like a blank line – a line containing nothing but spaces or tabs is considered blank.) Normal paragraphs should not be indented with spaces or tabs.

The implication of the “one or more consecutive lines of text” rule is that Markdown supports “hard-wrapped” text paragraphs. This differs significantly from most other text-to-HTML formatters (including Movable Type’s “Convert Line Breaks” option) which translate every line break character in a paragraph into a <br /> tag.

When you do want to insert a <br /> break tag using Markdown, you end a line with two or more spaces, then type return.

Yes, this takes a tad more effort to create a <br />, but a simplistic “every line break is a <br />” rule wouldn’t work for Markdown. Markdown’s email-style blockquoting and multi-paragraph list items work best – and look better – when you format them with hard breaks.

Markdown supports two styles of headers, [Setext] 1 and [atx] 2.

Setext-style headers are “underlined” using equal signs (for first-level headers) and dashes (for second-level headers). For example:

This is an H1
=============

This is an H2
-------------

Any number of underlining =’s or -’s will work.

Atx-style headers use 1–6 hash characters at the start of the line, corresponding to header levels 1–6. For example:

# This is an H1

## This is an H2

###### This is an H6

Optionally, you may “close” atx-style headers. This is purely cosmetic – you can use this if you think it looks better. The closing hashes don’t even need to match the number of hashes used to open the header. (The number of opening hashes determines the header level.) :

# This is an H1 #

## This is an H2 ##

### This is an H3 ######

Blockquotes

Markdown uses email-style > characters for blockquoting. If you’re familiar with quoting passages of text in an email message, then you know how to create a blockquote in Markdown. It looks best if you hard wrap the text and put a > before every line:

> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
> 
> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
> id sem consectetuer libero luctus adipiscing.

Markdown allows you to be lazy and only put the > before the first line of a hard-wrapped paragraph:

> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.

> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
id sem consectetuer libero luctus adipiscing.

Blockquotes can be nested (i.e. a blockquote-in-a-blockquote) by adding additional levels of >:

> This is the first level of quoting.
>
> > This is nested blockquote.
>
> Back to the first level.

Blockquotes can contain other Markdown elements, including headers, lists, and code blocks:

> ## This is a header.
> 
> 1.   This is the first list item.
> 2.   This is the second list item.
> 
> Here's some example code:
> 
>     return shell_exec("echo $input | $markdown_script");

Any decent text editor should make email-style quoting easy. For example, with BBEdit, you can make a selection and choose Increase Quote Level from the Text menu.

Lists

Markdown supports ordered (numbered) and unordered (bulleted) lists.

Unordered lists use asterisks, pluses, and hyphens – interchangably – as list markers:

*   Red
*   Green
*   Blue

is equivalent to:

+   Red
+   Green
+   Blue

and:

-   Red
-   Green
-   Blue

Ordered lists use numbers followed by periods:

1.  Bird
2.  McHale
3.  Parish

It’s important to note that the actual numbers you use to mark the list have no effect on the HTML output Markdown produces. The HTML Markdown produces from the above list is:

<ol>
<li>Bird</li>
<li>McHale</li>
<li>Parish</li>
</ol>

If you instead wrote the list in Markdown like this:

1.  Bird
1.  McHale
1.  Parish

or even:

3. Bird
1. McHale
8. Parish

you’d get the exact same HTML output. The point is, if you want to, you can use ordinal numbers in your ordered Markdown lists, so that the numbers in your source match the numbers in your published HTML. But if you want to be lazy, you don’t have to.

If you do use lazy list numbering, however, you should still start the list with the number 1. At some point in the future, Markdown may support starting ordered lists at an arbitrary number.

List markers typically start at the left margin, but may be indented by up to three spaces. List markers must be followed by one or more spaces or a tab.

To make lists look nice, you can wrap items with hanging indents:

*   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
    viverra nec, fringilla in, laoreet vitae, risus.
*   Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
    Suspendisse id sem consectetuer libero luctus adipiscing.

But if you want to be lazy, you don’t have to:

*   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.
*   Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing.

If list items are separated by blank lines, Markdown will wrap the items in <p> tags in the HTML output. For example, this input:

*   Bird
*   Magic

will turn into:

<ul>
<li>Bird</li>
<li>Magic</li>
</ul>

But this:

*   Bird

*   Magic

will turn into:

<ul>
<li><p>Bird</p></li>
<li><p>Magic</p></li>
</ul>

List items may consist of multiple paragraphs. Each subsequent paragraph in a list item must be intended by either 4 spaces or one tab:

1.  This is a list item with two paragraphs. Lorem ipsum dolor
    sit amet, consectetuer adipiscing elit. Aliquam hendrerit
    mi posuere lectus.

    Vestibulum enim wisi, viverra nec, fringilla in, laoreet
    vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
    sit amet velit.

2.  Suspendisse id sem consectetuer libero luctus adipiscing.

It looks nice if you indent every line of the subsequent paragraphs, but here again, Markdown will allow you to be lazy:

*   This is a list item with two paragraphs.

    This is the second paragraph in the list item. You're
only required to indent the first line. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit.

*   Another item in the same list.

To put a blockquote within a list item, the blockquote’s > delimiters need to be indented:

*   A list item with a blockquote:

    > This is a blockquote
    > inside a list item.

To put a code block within a list item, the code block needs to be indented twice – 8 spaces or two tabs:

*   A list item with a code block:

        <code goes here>

It’s worth noting that it’s possible to trigger an ordered list by accident, by writing something like this:

1986. What a great season.

In other words, a number-period-space sequence at the beginning of a line. To avoid this, you can backslash-escape the period:

1986\. What a great season.

Code Blocks

Pre-formatted code blocks are used for writing about programming or markup source code. Rather than forming normal paragraphs, the lines of a code block are interpreted literally. Markdown wraps a code block in both <pre> and <code> tags.

To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab. For example, given this input:

This is a normal paragraph:

    This is a code block.

Markdown will generate:

<p>This is a normal paragraph:</p>

<pre><code>This is a code block.
</code></pre>

One level of indentation – 4 spaces or 1 tab – is removed from each line of the code block. For example, this:

Here is an example of AppleScript:

    tell application "Foo"
        beep
    end tell

will turn into:

<p>Here is an example of AppleScript:</p>

<pre><code>tell application "Foo"
    beep
end tell
</code></pre>

A code block continues until it reaches a line that is not indented (or the end of the article).

Within a code block, ampersands (&) and angle brackets (< and >) are automatically converted into HTML entities. This makes it very easy to include example HTML source code using Markdown – just paste it and indent it, and Markdown will handle the hassle of encoding the ampersands and angle brackets. For example, this:

    <div class="footer">
        &copy; 2004 Foo Corporation
    </div>

will turn into:

<pre><code>&lt;div class="footer"&gt;
    &amp;copy; 2004 Foo Corporation
&lt;/div&gt;
</code></pre>

Regular Markdown syntax is not processed within code blocks. E.g., asterisks are just literal asterisks within a code block. This means it’s also easy to use Markdown to write about Markdown’s own syntax.

Horizontal Rules

You can produce a horizontal rule tag (<hr />) by placing three or more hyphens, asterisks, or underscores on a line by themselves. If you wish, you may use spaces between the hyphens or asterisks. Each of the following lines will produce a horizontal rule:

* * *

***

*****

- - -

---------------------------------------

Span Elements

Markdown supports two style of links: inline and reference.

In both styles, the link text is delimited by [square brackets].

To create an inline link, use a set of regular parentheses immediately after the link text’s closing square bracket. Inside the parentheses, put the URL where you want the link to point, along with an optional title for the link, surrounded in quotes. For example:

This is [an example](http://example.com/ "Title") inline link.

[This link](http://example.net/) has no title attribute.

Will produce:

<p>This is <a href="http://example.com/" title="Title">
an example</a> inline link.</p>

<p><a href="http://example.net/">This link</a> has no
title attribute.</p>

If you’re referring to a local resource on the same server, you can use relative paths:

See my [About](/about/) page for details.   

Reference-style links use a second set of square brackets, inside which you place a label of your choosing to identify the link:

This is [an example][id] reference-style link.

You can optionally use a space to separate the sets of brackets:

This is [an example] [id] reference-style link.

Then, anywhere in the document, you define your link label like this, on a line by itself:

[id]: http://example.com/  "Optional Title Here"

That is:

  • Square brackets containing the link identifier (optionally indented from the left margin using up to three spaces);
  • followed by a colon;
  • followed by one or more spaces (or tabs);
  • followed by the URL for the link;
  • optionally followed by a title attribute for the link, enclosed in double or single quotes, or enclosed in parentheses.

The following three link definitions are equivalent:

[foo]: http://example.com/  "Optional Title Here"
[foo]: http://example.com/  'Optional Title Here'
[foo]: http://example.com/  (Optional Title Here)

Note: There is a known bug in Markdown.pl 1.0.1 which prevents single quotes from being used to delimit link titles.

The link URL may, optionally, be surrounded by angle brackets:

[id]: <http://example.com/>  "Optional Title Here"

You can put the title attribute on the next line and use extra spaces or tabs for padding, which tends to look better with longer URLs:

[id]: http://example.com/longish/path/to/resource/here
    "Optional Title Here"

Link definitions are only used for creating links during Markdown processing, and are stripped from your document in the HTML output.

Link definition names may constist of letters, numbers, spaces, and punctuation – but they are not case sensitive. E.g. these two links:

[link text][a]
[link text][A]

are equivalent.

The implicit link name shortcut allows you to omit the name of the link, in which case the link text itself is used as the name. Just use an empty set of square brackets – e.g., to link the word “Google” to the google.com web site, you could simply write:

[Google][]

And then define the link:

[Google]: http://google.com/

Because link names may contain spaces, this shortcut even works for multiple words in the link text:

Visit [Daring Fireball][] for more information.

And then define the link:

[Daring Fireball]: http://daringfireball.net/

Link definitions can be placed anywhere in your Markdown document. I tend to put them immediately after each paragraph in which they’re used, but if you want, you can put them all at the end of your document, sort of like footnotes.

Here’s an example of reference links in action:

I get 10 times more traffic from [Google] [1] than from
[Yahoo] [2] or [MSN] [3].

  [1]: http://google.com/        "Google"
  [2]: http://search.yahoo.com/  "Yahoo Search"
  [3]: http://search.msn.com/    "MSN Search"

Using the implicit link name shortcut, you could instead write:

I get 10 times more traffic from [Google][] than from
[Yahoo][] or [MSN][].

  [google]: http://google.com/        "Google"
  [yahoo]:  http://search.yahoo.com/  "Yahoo Search"
  [msn]:    http://search.msn.com/    "MSN Search"

Both of the above examples will produce the following HTML output:

<p>I get 10 times more traffic from <a href="http://google.com/"
title="Google">Google</a> than from
<a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a>
or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p>

For comparison, here is the same paragraph written using Markdown’s inline link style:

I get 10 times more traffic from [Google](http://google.com/ "Google")
than from [Yahoo](http://search.yahoo.com/ "Yahoo Search") or
[MSN](http://search.msn.com/ "MSN Search").

The point of reference-style links is not that they’re easier to write. The point is that with reference-style links, your document source is vastly more readable. Compare the above examples: using reference-style links, the paragraph itself is only 81 characters long; with inline-style links, it’s 176 characters; and as raw HTML, it’s 234 characters. In the raw HTML, there’s more markup than there is text.

With Markdown’s reference-style links, a source document much more closely resembles the final output, as rendered in a browser. By allowing you to move the markup-related metadata out of the paragraph, you can add links without interrupting the narrative flow of your prose.

Emphasis

Markdown treats asterisks (*) and underscores (_) as indicators of emphasis. Text wrapped with one * or _ will be wrapped with an HTML <em> tag; double *’s or _’s will be wrapped with an HTML <strong> tag. E.g., this input:

*single asterisks*

_single underscores_

**double asterisks**

__double underscores__

will produce:

<em>single asterisks</em>

<em>single underscores</em>

<strong>double asterisks</strong>

<strong>double underscores</strong>

You can use whichever style you prefer; the lone restriction is that the same character must be used to open and close an emphasis span.

Emphasis can be used in the middle of a word:

un*fucking*believable

But if you surround an * or _ with spaces, it’ll be treated as a literal asterisk or underscore.

To produce a literal asterisk or underscore at a position where it would otherwise be used as an emphasis delimiter, you can backslash escape it:

\*this text is surrounded by literal asterisks\*

Code

To indicate a span of code, wrap it with backtick quotes (`). Unlike a pre-formatted code block, a code span indicates code within a normal paragraph. For example:

Use the `printf()` function.

will produce:

<p>Use the <code>printf()</code> function.</p>

To include a literal backtick character within a code span, you can use multiple backticks as the opening and closing delimiters:

``There is a literal backtick (`) here.``

which will produce this:

<p><code>There is a literal backtick (`) here.</code></p>

The backtick delimiters surrounding a code span may include spaces – one after the opening, one before the closing. This allows you to place literal backtick characters at the beginning or end of a code span:

A single backtick in a code span: `` ` ``

A backtick-delimited string in a code span: `` `foo` ``

will produce:

<p>A single backtick in a code span: <code>`</code></p>

<p>A backtick-delimited string in a code span: <code>`foo`</code></p>

With a code span, ampersands and angle brackets are encoded as HTML entities automatically, which makes it easy to include example HTML tags. Markdown will turn this:

Please don't use any `<blink>` tags.

into:

<p>Please don't use any <code>&lt;blink&gt;</code> tags.</p>

You can write this:

`&#8212;` is the decimal-encoded equivalent of `&mdash;`.

to produce:

<p><code>&amp;#8212;</code> is the decimal-encoded
equivalent of <code>&amp;mdash;</code>.</p>

Images

Admittedly, it’s fairly difficult to devise a “natural” syntax for placing images into a plain text document format.

Markdown uses an image syntax that is intended to resemble the syntax for links, allowing for two styles: inline and reference.

Inline image syntax looks like this:

![Alt text](/path/to/img.jpg)

![Alt text](/path/to/img.jpg "Optional title")

That is:

  • An exclamation mark: !;
  • followed by a set of square brackets, containing the alt attribute text for the image;
  • followed by a set of parentheses, containing the URL or path to the image, and an optional title attribute enclosed in double or single quotes.

Reference-style image syntax looks like this:

![Alt text][id]

Where “id” is the name of a defined image reference. Image references are defined using syntax identical to link references:

[id]: url/to/image  "Optional title attribute"

As of this writing, Markdown has no syntax for specifying the dimensions of an image; if this is important to you, you can simply use regular HTML <img> tags.


Miscellaneous

Markdown supports a shortcut style for creating “automatic” links for URLs and email addresses: simply surround the URL or email address with angle brackets. What this means is that if you want to show the actual text of a URL or email address, and also have it be a clickable link, you can do this:

<http://example.com/>

Markdown will turn this into:

<a href="http://example.com/">http://example.com/</a>

Automatic links for email addresses work similarly, except that Markdown will also perform a bit of randomized decimal and hex entity-encoding to help obscure your address from address-harvesting spambots. For example, Markdown will turn this:

<address@example.com>

into something like this:

<a href="&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:&#x61;&#x64;&#x64;&#x72;&#x65;
&#115;&#115;&#64;&#101;&#120;&#x61;&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;
&#109;">&#x61;&#x64;&#x64;&#x72;&#x65;&#115;&#115;&#64;&#101;&#120;&#x61;
&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;&#109;</a>

which will render in a browser as a clickable link to “address@example.com”.

(This sort of entity-encoding trick will indeed fool many, if not most, address-harvesting bots, but it definitely won’t fool all of them. It’s better than nothing, but an address published in this way will probably eventually start receiving spam.)

Backslash Escapes

Markdown allows you to use backslash escapes to generate literal characters which would otherwise have special meaning in Markdown’s formatting syntax. For example, if you wanted to surround a word with literal asterisks (instead of an HTML <em> tag), you can backslashes before the asterisks, like this:

\*literal asterisks\*

Markdown provides backslash escapes for the following characters:

\   backslash
`   backtick
*   asterisk
_   underscore
{}  curly braces
[]  square brackets
()  parentheses
#   hash mark
+	plus sign
-	minus sign (hyphen)
.   dot
!   exclamation mark

PHP Markdown Extra


Version 1.2.4 - Sat 10 Oct 2009

by Michel Fortin http://michelf.com/

based on Markdown by John Gruber
http://daringfireball.net/

Introduction

This is a special version of PHP Markdown with extra features. See http://michelf.com/projects/php-markdown/extra/ for details.

Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).

“Markdown” is two things: a plain text markup syntax, and a software tool, written in Perl, that converts the plain text markup to HTML. PHP Markdown is a port to PHP of the original Markdown program by John Gruber.

PHP Markdown can work as a plug-in for WordPress and bBlog, as a modifier for the Smarty templating engine, or as a remplacement for textile formatting in any software that support textile.

Full documentation of Markdown’s syntax is available on John’s Markdown page: http://daringfireball.net/projects/markdown/

Installation and Requirement

PHP Markdown requires PHP version 4.0.5 or later.

WordPress

PHP Markdown works with WordPress, version 1.2 or later.

  1. To use PHP Markdown with WordPress, place the “makrdown.php” file in the “plugins” folder. This folder is located inside “wp-content” at the root of your site:

    (site home)/wp-content/plugins/
    
  2. Activate the plugin with the administrative interface of WordPress. In the “Plugins” section you will now find Markdown. To activate the plugin, click on the “Activate” button on the same line than Markdown. Your entries will now be formatted by PHP Markdown.

  3. To post Markdown content, you’ll first have to disable the “visual” editor in the User section of WordPress.

You can configure PHP Markdown to not apply to the comments on your WordPress weblog. See the “Configuration” section below.

It is not possible at this time to apply a different set of filters to different entries. All your entries will be formated by PHP Markdown. This is a limitation of WordPress. If your old entries are written in HTML (as opposed to another formatting syntax, like Textile), they’ll probably stay fine after installing Markdown.

bBlog

PHP Markdown also works with bBlog.

To use PHP Markdown with bBlog, rename “markdown.php” to “modifier.markdown.php” and place the file in the “bBlog_plugins” folder. This folder is located inside the “bblog” directory of your site, like this:

    (site home)/bblog/bBlog_plugins/modifier.markdown.php

Select “Markdown” as the “Entry Modifier” when you post a new entry. This setting will only apply to the entry you are editing.

Replacing Textile in TextPattern

TextPattern use Textile to format your text. You can replace Textile by Markdown in TextPattern without having to change any code by using the Texitle Compatibility Mode. This may work with other software that expect Textile too.

  1. Rename the “markdown.php” file to “classTextile.php”. This will make PHP Markdown behave as if it was the actual Textile parser.

  2. Replace the “classTextile.php” file TextPattern installed in your web directory. It can be found in the “lib” directory:

    (site home)/textpattern/lib/
    

Contrary to Textile, Markdown does not convert quotes to curly ones and does not convert multiple hyphens (-- and ---) into en- and em-dashes. If you use PHP Markdown in Textile Compatibility Mode, you can solve this problem by installing the “smartypants.php” file from PHP SmartyPants beside the “classTextile.php” file. The Textile Compatibility Mode function will use SmartyPants automatically without further modification.

In Your Own Programs

You can use PHP Markdown easily in your current PHP program. Simply include the file and then call the Markdown function on the text you want to convert:

include_once "markdown.php";
$my_html = Markdown($my_text);

If you wish to use PHP Markdown with another text filter function built to parse HTML, you should filter the text after the Markdown function call. This is an example with PHP SmartyPants:

$my_html = SmartyPants(Markdown($my_text));

With Smarty

If your program use the Smarty template engine, PHP Markdown can now be used as a modifier for your templates. Rename “markdown.php” to “modifier.markdown.php” and put it in your smarty plugins folder.

If you are using MovableType 3.1 or later, the Smarty plugin folder is located at (MT CGI root)/php/extlib/smarty/plugins. This will allow Markdown to work on dynamic pages.

Updating Markdown in Other Programs

Many web applications now ship with PHP Markdown, or have plugins to perform the conversion to HTML. You can update PHP Markdown – or replace it with PHP Markdown Extra – in many of these programs by swapping the old “markdown.php” file for the new one.

Here is a short non-exhaustive list of some programs and where they hide the “markdown.php” file.

Program Path to Markdown
Pivot (site home)/pivot/includes/markdown/

If you’re unsure if you can do this with your application, ask the developer, or wait for the developer to update his application or plugin with the new version of PHP Markdown.

Configuration

By default, PHP Markdown produces XHTML output for tags with empty elements. E.g.:

<br />

Markdown can be configured to produce HTML-style tags; e.g.:

<br>

To do this, you must edit the “MARKDOWN_EMPTY_ELEMENT_SUFFIX” definition below the “Global default settings” header at the start of the “markdown.php” file.

WordPress-Specific Settings

By default, the Markdown plugin applies to both posts and comments on your WordPress weblog. To deactivate one or the other, edit the MARKDOWN_WP_POSTS or MARKDOWN_WP_COMMENTS definitions under the “WordPress settings” header at the start of the “markdown.php” file.

Bugs

To file bug reports please send email to: michel.fortin@michelf.com

Please include with your report: (1) the example input; (2) the output you expected; (3) the output PHP Markdown actually produced.

Version History

1.0.1n (10 Oct 2009):

  • Enabled reference-style shortcut links. Now you can write reference-style links with less brakets:
    This is [my website].
    
    [my website]: http://example.com/
    
    This was added in the 1.0.2 betas, but commented out in the 1.0.1 branch, waiting for the feature to be officialized. But half of the other Markdown implementations are supporting this syntax, so it makes sense for compatibility’s sake to allow it in PHP Markdown too.
  • Now accepting many valid email addresses in autolinks that were previously rejected, such as:

    <abc+mailbox/department=shipping@example.com>
    <!#$%&'*+-/=?^_`.{|}~@example.com>
    <"abc@def"@example.com>
    <"Fred Bloggs"@example.com>
    <jsmith@[192.0.2.1]>
    
  • Now accepting spaces in URLs for inline and reference-style links. Such URLs need to be surrounded by angle brakets. For instance:

    [link text](<http://url/with space> "optional title")
    
    [link text][ref]
    [ref]: <http://url/with space> "optional title"
    

    There is still a quirk which may prevent this from working correctly with relative URLs in inline-style links however.

  • Fix for adjacent list of different kind where the second list could end as a sublist of the first when not separated by an empty line.

  • Fixed a bug where inline-style links wouldn’t be recognized when the link definition contains a line break between the url and the title.

  • Fixed a bug where tags where the name contains an underscore aren’t parsed correctly.

  • Fixed some corner-cases mixing underscore-ephasis and asterisk-emphasis.

Extra 1.2.4:

  • Fixed a problem where unterminated tags in indented code blocks could prevent proper escaping of characaters in the code block.

Extra 1.2.3 (31 Dec 2008):

  • In WordPress pages featuring more than one post, footnote id prefixes are now automatically applied with the current post ID to avoid clashes between footnotes belonging to different posts.

  • Fix for a bug introduced in Extra 1.2 where block-level HTML tags where not detected correctly, thus the addition of erroneous <p> tags and interpretation of their content as Markdown-formatted instead of HTML-formatted.

Extra 1.2.2 (21 Jun 2008):

  • Fixed a problem where abbreviation definitions, footnote definitions and link references were stripped inside fenced code blocks.

  • Fixed a bug where characters such as " in abbreviation definitions weren’t properly encoded to HTML entities.

  • Fixed a bug where double quotes " were not correctly encoded as HTML entities when used inside a footnote reference id.

1.0.1m (21 Jun 2008):

  • Lists can now have empty items.

  • Rewrote the emphasis and strong emphasis parser to fix some issues with odly placed and overlong markers.

Extra 1.2.1 (27 May 2008):

  • Fixed a problem where Markdown headers and horizontal rules were transformed into their HTML equivalent inside fenced code blocks.

Extra 1.2 (11 May 2008):

  • Added fenced code block syntax which don’t require indentation and can start and end with blank lines. A fenced code block starts with a line of consecutive tilde (~) and ends on the next line with the same number of consecutive tilde. Here’s an example:

    ~~~~~~~~~~~~
    Hello World!
    ~~~~~~~~~~~~
    
  • Rewrote parts of the HTML block parser to better accomodate fenced code blocks.

  • Footnotes may now be referenced from within another footnote.

  • Added programatically-settable parser property predef_attr for predefined attribute definitions.

  • Fixed an issue where an indented code block preceded by a blank line containing some other whitespace would confuse the HTML block parser into creating an HTML block when it should have been code.

1.0.1l (11 May 2008):

  • Now removing the UTF-8 BOM at the start of a document, if present.

  • Now accepting capitalized URI schemes (such as HTTP:) in automatic links, such as <HTTP://EXAMPLE.COM/>.

  • Fixed a problem where <hr@example.com> was seen as a horizontal rule instead of an automatic link.

  • Fixed an issue where some characters in Markdown-generated HTML attributes weren’t properly escaped with entities.

  • Fix for code blocks as first element of a list item. Previously, this didn’t create any code block for item 2:

    *   Item 1 (regular paragraph)
    
    *       Item 2 (code block)
    
  • A code block starting on the second line of a document wasn’t seen as a code block. This has been fixed.

  • Added programatically-settable parser properties predef_urls and predef_titles for predefined URLs and titles for reference-style links. To use this, your PHP code must call the parser this way:

    $parser = new Markdwon_Parser;
    $parser->predef_urls = array('linkref' => 'http://example.com');
    $html = $parser->transform($text);
    

    You can then use the URL as a normal link reference:

    [my link][linkref]	
    [my link][linkRef]
    

    Reference names in the parser properties must be lowercase. Reference names in the Markdown source may have any case.

  • Added setup and teardown methods which can be used by subclassers as hook points to arrange the state of some parser variables before and after parsing.

Extra 1.1.7 (26 Sep 2007):

1.0.1k (26 Sep 2007):

  • Fixed a problem introduced in 1.0.1i where three or more identical uppercase letters, as well as a few other symbols, would trigger a horizontal line.

Extra 1.1.6 (4 Sep 2007):

1.0.1j (4 Sep 2007):

  • Fixed a problem introduced in 1.0.1i where the closing code and pre tags at the end of a code block were appearing in the wrong order.

  • Overriding configuration settings by defining constants from an external before markdown.php is included is now possible without producing a PHP warning.

Extra 1.1.5 (31 Aug 2007):

1.0.1i (31 Aug 2007):

  • Fixed a problem where an escaped backslash before a code span would prevent the code span from being created. This should now work as expected:

    Litteral backslash: \\`code span`
    
  • Overall speed improvements, especially with long documents.

Extra 1.1.4 (3 Aug 2007):

1.0.1h (3 Aug 2007):

  • Added two properties (no_markup and no_entities) to the parser allowing HTML tags and entities to be disabled.

  • Fix for a problem introduced in 1.0.1g where posting comments in WordPress would trigger PHP warnings and cause some markup to be incorrectly filtered by the kses filter in WordPress.

Extra 1.1.3 (3 Jul 2007):

  • Fixed a performance problem when parsing some invalid HTML as an HTML block which was resulting in too much recusion and a segmentation fault for long documents.

  • The markdown=“” attribute now accepts unquoted values.

  • Fixed an issue where underscore-emphasis didn’t work when applied on the first or the last word of an element having the markdown=“1” or markdown=“span” attribute set unless there was some surrounding whitespace. This didn’t work:

    <p markdown="1">_Hello_ _world_</p>
    

    Now it does produce emphasis as expected.

  • Fixed an issue preventing footnotes from working when the parser’s footnote id prefix variable (fn_id_prefix) is not empty.

  • Fixed a performance problem where the regular expression for strong emphasis introduced in version 1.1 could sometime be long to process, give slightly wrong results, and in some circumstances could remove entirely the content for a whole paragraph.

  • Fixed an issue were abbreviations tags could be incorrectly added inside URLs and title of links.

  • Placing footnote markers inside a link, resulting in two nested links, is no longer allowed.

1.0.1g (3 Jul 2007):

  • Fix for PHP 5 compiled without the mbstring module. Previous fix to calculate the length of UTF-8 strings in detab when mb_strlen is not available was only working with PHP 4.

  • Fixed a problem with WordPress 2.x where full-content posts in RSS feeds were not processed correctly by Markdown.

  • Now supports URLs containing literal parentheses for inline links and images, such as:

    [WIMP](http://en.wikipedia.org/wiki/WIMP_(computing))
    

    Such parentheses may be arbitrarily nested, but must be balanced. Unbalenced parentheses are allowed however when the URL when escaped or when the URL is enclosed in angle brakets <>.

  • Fixed a performance problem where the regular expression for strong emphasis introduced in version 1.0.1d could sometime be long to process, give slightly wrong results, and in some circumstances could remove entirely the content for a whole paragraph.

  • Some change in version 1.0.1d made possible the incorrect nesting of anchors within each other. This is now fixed.

  • Fixed a rare issue where certain MD5 hashes in the content could be changed to their corresponding text. For instance, this:

    The MD5 value for "+" is "26b17225b626fb9238849fd60eabdf60".
    

    was incorrectly changed to this in previous versions of PHP Markdown:

    <p>The MD5 value for "+" is "+".</p>
    
  • Now convert escaped characters to their numeric character references equivalent.

    This fix an integration issue with SmartyPants and backslash escapes. Since Markdown and SmartyPants have some escapable characters in common, it was sometime necessary to escape them twice. Previously, two backslashes were sometime required to prevent Markdown from “eating” the backslash before SmartyPants sees it:

    Here are two hyphens: \\--
    

    Now, only one backslash will do:

    Here are two hyphens: \--
    

Extra 1.1.2 (7 Feb 2007)

  • Fixed an issue where headers preceded too closely by a paragraph (with no blank line separating them) where put inside the paragraph.

  • Added the missing TextileRestricted method that was added to regular PHP Markdown since 1.0.1d but which I forgot to add to Extra.

1.0.1f (7 Feb 2007):

  • Fixed an issue with WordPress where manually-entered excerpts, but not the auto-generated ones, would contain nested paragraphs.

  • Fixed an issue introduced in 1.0.1d where headers and blockquotes preceded too closely by a paragraph (not separated by a blank line) where incorrectly put inside the paragraph.

  • Fixed an issue introduced in 1.0.1d in the tokenizeHTML method where two consecutive code spans would be merged into one when together they form a valid tag in a multiline paragraph.

  • Fixed an long-prevailing issue where blank lines in code blocks would be doubled when the code block is in a list item.

    This was due to the list processing functions relying on artificially doubled blank lines to correctly determine when list items should contain block-level content. The list item processing model was thus changed to avoid the need for double blank lines.

  • Fixed an issue with <% asp-style %> instructions used as inline content where the opening < was encoded as &lt;.

  • Fixed a parse error occuring when PHP is configured to accept ASP-style delimiters as boundaries for PHP scripts.

  • Fixed a bug introduced in 1.0.1d where underscores in automatic links got swapped with emphasis tags.

Extra 1.1.1 (28 Dec 2006)

  • Fixed a problem where whitespace at the end of the line of an atx-style header would cause tailing # to appear as part of the header’s content. This was caused by a small error in the regex that handles the definition for the id attribute in PHP Markdown Extra.

  • Fixed a problem where empty abbreviations definitions would eat the following line as its definition.

  • Fixed an issue with calling the Markdown parser repetitivly with text containing footnotes. The footnote hashes were not reinitialized properly.

1.0.1e (28 Dec 2006)

  • Added support for internationalized domain names for email addresses in automatic link. Improved the speed at which email addresses are converted to entities. Thanks to Milian Wolff for his optimisations.

  • Made deterministic the conversion to entities of email addresses in automatic links. This means that a given email address will always be encoded the same way.

  • PHP Markdown will now use its own function to calculate the length of an UTF-8 string in detab when mb_strlen is not available instead of giving a fatal error.

Extra 1.1 (1 Dec 2006)

  • Added a syntax for footnotes.

  • Added an experimental syntax to define abbreviations.

1.0.1d (1 Dec 2006)

  • Fixed a bug where inline images always had an empty title attribute. The title attribute is now present only when explicitly defined.

  • Link references definitions can now have an empty title, previously if the title was defined but left empty the link definition was ignored. This can be useful if you want an empty title attribute in images to hide the tooltip in Internet Explorer.

  • Made detab aware of UTF-8 characters. UTF-8 multi-byte sequences are now correctly mapped to one character instead of the number of bytes.

  • Fixed a small bug with WordPress where WordPress' default filter wpautop was not properly deactivated on comment text, resulting in hard line breaks where Markdown do not prescribes them.

  • Added a TextileRestrited method to the textile compatibility mode. There is no restriction however, as Markdown does not have a restricted mode at this point. This should make PHP Markdown work again in the latest versions of TextPattern.

  • Converted PHP Markdown to a object-oriented design.

  • Changed span and block gamut methods so that they loop over a customizable list of methods. This makes subclassing the parser a more interesting option for creating syntax extensions.

  • Also added a “document” gamut loop which can be used to hook document-level methods (like for striping link definitions).

  • Changed all methods which were inserting HTML code so that they now return a hashed representation of the code. New methods hashSpan and hashBlock are used to hash respectivly span- and block-level generated content. This has a couple of significant effects:

    1. It prevents invalid nesting of Markdown-generated elements which could occur occuring with constructs like *something [link*][1].
    2. It prevents problems occuring with deeply nested lists on which paragraphs were ill-formed.
    3. It removes the need to call hashHTMLBlocks twice during the the block gamut.

    Hashes are turned back to HTML prior output.

  • Made the block-level HTML parser smarter using a specially-crafted regular expression capable of handling nested tags.

  • Solved backtick issues in tag attributes by rewriting the HTML tokenizer to be aware of code spans. All these lines should work correctly now:

    <span attr='`ticks`'>bar</span>
    <span attr='``double ticks``'>bar</span>
    `<test a="` content of attribute `">`
    
  • Changed the parsing of HTML comments to match simply from <!--` to `--> instead using of the more complicated SGML-style rule with paired --. This is how most browsers parse comments and how XML defines them too.

  • <address> has been added to the list of block-level elements and is now treated as an HTML block instead of being wrapped within paragraph tags.

  • Now only trim trailing newlines from code blocks, instead of trimming all trailing whitespace characters.

  • Fixed bug where this:

    [text](http://m.com "title" )
    

    wasn’t working as expected, because the parser wasn’t allowing for spaces before the closing paren.

  • Filthy hack to support markdown=‘1’ in div tags.

  • _DoAutoLinks() now supports the ‘dict://’ URL scheme.

  • PHP- and ASP-style processor instructions are now protected as raw HTML blocks.

    <? ... ?>
    <% ... %>
    
  • Fix for escaped backticks still triggering code spans:

    There are two raw backticks here: \` and here: \`, not a code span
    

Extra 1.0 - 5 September 2005

  • Added support for setting the id attributes for headers like this:

    Header 1            {#header1}
    ========
    
    ## Header 2 ##      {#header2}
    

    This only work only for headers for now.

  • Tables will now work correctly as the first element of a definition list. For example, this input:

    Term
    
    :   Header  | Header
        ------- | -------
        Cell    | Cell
    

    used to produce no definition list and a table where the first header was named “: Header”. This is now fixed.

  • Fix for a problem where a paragraph following a table was not placed between <p> tags.

Extra 1.0b4 - 1 August 2005

  • Fixed some issues where whitespace around HTML blocks were trigging empty paragraph tags.

  • Fixed an HTML block parsing issue that would cause a block element following a code span or block with unmatched opening bracket to be placed inside a paragraph.

  • Removed some PHP notices that could appear when parsing definition lists and tables with PHP notice reporting flag set.

Extra 1.0b3 - 29 July 2005

  • Definition lists now require a blank line before each term. Solves an ambiguity where the last line of lazy-indented definitions could be mistaken by PHP Markdown as a new term in the list.

  • Definition lists now support multiple terms per definition.

  • Some special tags were replaced in the output by their md5 hash key. Things such as this now work as expected:

    ## Header <?php echo $number ?> ##
    

Extra 1.0b2 - 26 July 2005

  • Definition lists can now take two or more definitions for one term. This should have been the case before, but a bug prevented this from working right.

  • Fixed a problem where single column table with a pipe only at the end where not parsed as table. Here is such a table:

    | header
    | ------
    | cell
    
  • Fixed problems with empty cells in the first column of a table with no leading pipe, like this one:

    header | header
    ------ | ------
           | cell
    
  • Code spans containing pipes did not within a table. This is now fixed by parsing code spans before splitting rows into cells.

  • Added the pipe character to the backlash escape character lists.

Extra 1.0b1 (25 Jun 2005)

  • First public release of PHP Markdown Extra.

Copyright and License

PHP Markdown & Extra
Copyright (c) 2004–2009 Michel Fortin
http://michelf.com/
All rights reserved.

Based on Markdown
Copyright (c) 2003–2005 John Gruber
http://daringfireball.net/
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name “Markdown” nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

This software is provided by the copyright holders and contributors “as is” and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.

— ~ —