Markdown
String.markdown() and its options.
On this page
API
method
String.markdown([opt])
Converts a Markdown string to HTML.
String.markdown([opt])| Parameter | Type | Description |
|---|---|---|
opt? |
Object |
Options: wrap, linetag, bookmarks, prefix, html, custom and the feature switches listed above. Default: {} |
Returns String HTML.
The framework loads the parser at start, so every string has this method. HTML tags in the source are encoded. Every block gets the class markdown-line and its line number in data-line (data-index for headlines). By default the output is wrapped in <div class="markdown markdown-container"> and tabs become four spaces.
Each feature can be turned off with false in opt: headlines, ul (lists), code, images, links, formatting, icons, tables, br, hr, blockquotes, sections, blocks, footnotes, urlify, keywords and emptynewline. Bullet lists can start with -, * or +. Links to an e-mail address or a phone number become mailto: and tel: links.
var html = '# Hello\n\n- one\n- two\n\nWrite to [us](info@company.com).'.markdown();
var html = text.markdown({ tables: false, code: false, images: false, wrap: false });
property
String.markdown({ wrap: Boolean })
Wraps the output in a div container.
String.markdown({ wrap: Boolean })Returns Boolean Default true.
With true the output is <div class="markdown markdown-container">...</div>. Use false to insert the HTML into your own element.
var html = '**Bold** text'.markdown({ wrap: false });
// <p class="markdown-line" data-line="0"><strong>Bold</strong> text</p>
property
String.markdown({ linetag: String })
The tag of a normal paragraph line.
String.markdown({ linetag: String })Returns String Default 'p'.
Each line that is not a headline, list, table or other block is wrapped in this tag. An empty string writes the lines without a tag.
var html = text.markdown({ linetag: 'div' });
property
String.markdown({ bookmarks: Boolean })
since 0.0.96
Adds an id (a slug of the text) to headlines.
String.markdown({ bookmarks: Boolean })Returns Boolean Default true.
# Hello world becomes <h1 id="hello-world" ...>, so you can link to #hello-world. Set false to leave the id out.
var html = '# Title'.markdown({ bookmarks: false, wrap: false });
// <h1 class="markdown-line" data-index="0">Title</h1>
property
String.markdown({ prefix: String })
since 0.0.96
A prefix for the id of headlines.
String.markdown({ prefix: String })Returns String Default ''.
Useful when more Markdown documents are on one page and their headlines would get the same id.
var html = '# Hello world'.markdown({ prefix: 'doc-' });
// <h1 id="doc-hello-world" ...>
delegate
String.markdown({ html: function(line, type) })
Changes the HTML of a block before it is written.
String.markdown({ html: function(line, type) })| Parameter | Type | Description |
|---|---|---|
line |
String |
The content of the block. |
type |
String |
The block type. |
Returns String The new content.
Called with the content and the block type: # to ##### for headlines, li, blockquote, section, block or the value of linetag for normal lines. Return the new content.
var html = text.markdown({ html: function(line, type) {
return type === '#' ? line.toUpperCase() : line;
}});
delegate
String.markdown({ custom: function(line) })
Changes each raw line before the inline formatting is applied.
String.markdown({ custom: function(line) })| Parameter | Type | Description |
|---|---|---|
line |
String |
One line of the source. |
Returns String The new line.
Use it for your own inline syntax. The line is already HTML-encoded. Return the new line.
var html = text.markdown({ custom: function(line) {
return line.replace(/\{year\}/g, NOW.getFullYear());
}});