Skip to content

Core\Markdown::toHtml

Converts a Markdown document to HTML that is safe to write into a page.

The result is a Core\Html\Markup, so you can print it or put it into a template as it is. Any text is a valid document, so toHtml never throws an error because of the text. It reads CommonMark with tables, task lists, strikethrough, autolinks and footnotes.

The document may come from a user. HTML inside it is cleaned the same way Core\Html::sanitize cleans it, so a <script> element or an onclick attribute is removed. A link with an unsafe URL, such as javascript:, is written as plain text. An image with an unsafe URL is removed. A URL that starts with http:, https:, mailto: or tel: is safe.

You can pass four optional functions by name. link and image change each URL, heading gives each heading an id, and code writes the HTML of a code block, for example with syntax highlighting.

Good to know: the time toHtml needs grows in step with the length of the document. A document written to be slow, such as thousands of open brackets, is still converted quickly.

Parameters

  • $markdownstringThe Markdown document. Any text is a valid document.
  • $image?callable(string): ?stringdefault nullA function that gets each image's URL and returns the URL to write. If it returns null, the image is removed. The URL it returns is checked like a URL in the document.
  • $heading?callable(int, string): ?stringdefault nullA function that gets each heading's level and text and returns its id. The id is written after md-, so intro gives md-intro. If it returns null, or an id with a space, the heading has no id.
  • $code?callable(string, string): ?Core\Html\Markupdefault nullA function that gets the language and the text of each fenced code block that names a language. The Core\Html\Markup it returns replaces the block, exactly as it is. If it returns null, the block is written as usual. Use it for syntax highlighting, math or diagrams.

Return value

Returns Core\Html\Markup. A Core\Html\Markup with the document's HTML. HTML inside the document is cleaned the same way Core\Html::sanitize cleans it. A link with an unsafe URL, such as javascript:, is written as plain text. An image with an unsafe URL is removed. If a callback throws an error, the conversion stops and your code gets that error.

Changelog

VersionChange
0.0.6Core\Markdown::toHtml keeps a link that starts with tel:. On a phone, such a link opens the dialer with the number.

Examples

A short post as html

<?nvs
// Shows how to convert a short Markdown post to HTML.
string $post = "# Opening hours\n\n"
. "We are open **every day** from 9 to 18.\n\n"
. "- Coffee\n"
. "- Cake\n\n"
. "Read the [full menu](/menu).\n";
// `toHtml` returns a `Core\Html\Markup`. You can print it or write it into a page as it is.
Core\Html\Markup $html = Core\Markdown::toHtml($post);
echo $html;
Output
<h1>Opening hours</h1>
<p>We are open <strong>every day</strong> from 9 to 18.</p>
<ul>
<li>Coffee</li>
<li>Cake</li>
</ul>
<p>Read the <a href="/menu">full menu</a>.</p>

A comment from a user

<?nvs
// Shows that HTML and links in a user's comment cannot run a script in your page.
string $comment = "Great post! <script>steal(document.cookie)</script>\n\n"
. "<b onclick=\"steal()\">Click me</b> or visit [my site](javascript:steal()).\n\n"
. "My [real site](https://example.com) is here.\n";
// The `<script>` element is removed. The `<b>` element stays, but without `onclick`.
// The `javascript:` link is written as plain text. The `https` link stays a link.
echo Core\Markdown::toHtml($comment);
Output
<p>Great post! </p>
<p><b>Click me</b> or visit my site.</p>
<p>My <a href="https://example.com">real site</a> is here.</p>

Links headings and code in a docs page

<?nvs
// Shows how to change links, heading ids and code blocks while a docs page is converted.
string $page = "# Install\n\n"
. "See the [setup guide](/guide/setup) and the [old notes](/old/notes).\n\n"
. "## First run\n\n"
. "```shell\nnvs run app.nvs\n```\n";
class Docs
{
// Each link gets the site's address in front. A link to `/old/` is removed and its text stays.
public static function link(string $url): ?string
{
if (Core\Str::startsWith($url, '/old/')) {
return null;
}
return 'https://docs.example.com' . $url;
}
// Each heading gets an `id` from its text. `toHtml` writes `md-` in front of it.
public static function heading(int $level, string $text): ?string
{
return Core\Str::replace(Core\Str::lower($text), ' ', '-');
}
// Each code block that names a language gets a label with the language above it.
// The HTML template escapes `$language` and `$code`.
public static function code(string $language, string $code): ?Core\Html\Markup
{
return html`<div class="label">{$language}</div><pre>{$code}</pre>`;
}
}
echo Core\Markdown::toHtml(
$page,
link: fn(string $url): ?string => Docs::link($url),
heading: fn(int $level, string $text): ?string => Docs::heading($level, $text),
code: fn(string $language, string $code): ?Core\Html\Markup => Docs::code($language, $code),
);
Output
<h1 id="md-install">Install</h1>
<p>See the <a href="https://docs.example.com/guide/setup">setup guide</a> and the old notes.</p>
<h2 id="md-first-run">First run</h2>
<div class="label">shell</div><pre>nvs run app.nvs
</pre>