Skip to main content

toHtml(md, options?): string

Converts a Markdown string to an HTML string.

Import​

import { toHtml } from 'md-to-rich'
// or sub-path:
import { toHtml } from 'md-to-rich/html'

Signature​

function toHtml(md: string, options?: HtmlOptions): string

Options​

OptionTypeDefaultDescription
headingIdsbooleantrueInject slug-based id attributes on headings (collision-safe)
classNamesPartial<Record<HtmlElement, string>>{}Map of element name → CSS class string
renderImagesbooleantrueRender <img> tags; set to false to suppress all images
allowRawHtmlbooleanfalsePass raw HTML nodes from Markdown through to output
gfmbooleantrueEnable GitHub Flavored Markdown
remarkPluginsPlugin[][]Additional remark plugins applied before serialization

Examples​

Basic​

toHtml('# Hello\n\n**Bold** and *italic*.')
// → '<h1 id="hello">Hello</h1><p><strong>Bold</strong> and <em>italic</em>.</p>'

Custom class names​

toHtml('# Title\n\nParagraph.', {
classNames: {
h1: 'heading-xl',
p: 'prose text-base',
},
})
// → '<h1 id="title" class="heading-xl">Title</h1>
// <p class="prose text-base">Paragraph.</p>'

Disable heading IDs​

toHtml('# Title', { headingIds: false })
// → '<h1>Title</h1>'

Suppress images​

toHtml('![alt](https://example.com/img.png)', { renderImages: false })
// → '' (image element omitted)

GFM Features​

When gfm: true (the default), the following GitHub Flavored Markdown extensions are enabled:

FeatureMarkdownOutput
Tables| a | b |<table> with style="text-align:..." per column
Task lists- [x] done<input type="checkbox" disabled checked>
Strikethrough~~text~~<del>text</del>
Autolinkshttps://example.com<a href="...">

URL Sanitisation​

URL sanitisation is always on regardless of options. Any href or src attribute containing a dangerous protocol is replaced with #.

See the Security page for the full list of blocked and allowed protocols.

allowRawHtml

Setting allowRawHtml: true passes raw HTML nodes from the Markdown source through to the output unchanged. This opens an XSS risk if the output is injected into a browser DOM.

Only enable this option when the Markdown source is fully trusted (e.g., stored in your own database, never user-supplied).