Spanish Total.js
English
Documentation For AI agents
Contents

Markdown

String.markdown() and its options.

.md GitHub
On this page

API

method

String.markdown([opt])

Converts a Markdown string to HTML.
ParameterTypeDescription
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.

Render a README
var html = '# Hello\n\n- one\n- two\n\nWrite to [us](info@company.com).'.markdown();
Only basic formatting
var html = text.markdown({ tables: false, code: false, images: false, wrap: false });
property

String.markdown({ wrap: Boolean })

Wraps the output in a div container.

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.

Without the container
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.

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.

Lines as div
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.

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.

Headlines without id
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.

Returns String Default ''.

Useful when more Markdown documents are on one page and their headlines would get the same id.

Prefixed bookmarks
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.
ParameterTypeDescription
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.

Upper-case h1
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.
ParameterTypeDescription
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.

Replace a placeholder
var html = text.markdown({ custom: function(line) {
	return line.replace(/\{year\}/g, NOW.getFullYear());
}});
Made in Europe