Interface MarkdownItOptions

Source
Expand description

Options controlling Markdown parsing and rendering.


interface MarkdownItOptions {
    html?: boolean;
    xhtmlOut?: boolean;
    breaks?: boolean;
    langPrefix?: string;
    linkify?: boolean;
    typographer?: boolean;
    quotes?: string | string[];
    highlight?: ((str: string, lang: string, attrs: string) => string) | null;
    maxNesting?: number;
}

Properties§

§html?: boolean

Enable HTML tags in source.

§xhtmlOut?: boolean

Use '/' to close single tags (<br />).

§breaks?: boolean

Convert '\n' in paragraphs into <br>.

§langPrefix?: string

CSS language prefix for fenced blocks, used by external syntax highlighters.

§linkify?: boolean

Autoconvert URL-like text to links.

§typographer?: boolean

Enable language-neutral replacements and quotes beautification.

See the replacement rules for the full list.

§quotes?: string | string[]

Double + single quotes replacement pairs, when typographer is enabled and smartquotes are on. Can be either a string or an array.

For example, use '«»„“' for Russian, '„“‚‘' for German, and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).

§highlight?: ((str: string, lang: string, attrs: string) => string) | null

Highlighter function. Should return escaped HTML, or an empty string if the source string was not changed and should be escaped externally. If the result starts with <pre, the internal wrapper is skipped.

The highlighter is called by the default fence renderer rule. If needed, you can replace that renderer rule completely; see the renderer source.

example
import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js' // https://highlightjs.org

const md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value
} catch (__) {}
}

return '' // use external default escaping
}
});
example

Or with full wrapper override (if you need assign class to <pre> or <code>):

import MarkdownIt from 'markdown-it'
import hljs from 'highlight.js' // https://highlightjs.org

const md = new MarkdownIt({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return `<pre><code class="hljs">${hljs.highlight(str,
{ language: lang, ignoreIllegals: true }).value}</code></pre>`
} catch (__) {}
}

return `<pre><code class="hljs">${md.utils.escapeHtml(str)}</code></pre>`
}
});
§maxNesting?: number

Internal protection against excessive recursion.