Skip to content

Core\Markdown::fromHtml

Converts HTML to Markdown. Text in the HTML stays text, so it never becomes Markdown syntax.

You can pass HTML as a string, as a Core\Html\Markup or as a Core\Xml\Node from Core\Html::parse. A character such as *, [ or # in the text gets a backslash, so it shows as the same character. Elements that are not safe, such as <script>, are deleted with their text. A link to a URL that is not safe, such as javascript:, keeps only its text. The result is a tainted string, because it is made from text you did not write.

Good to know: a table that Markdown cannot show, for example one with colspan, is written as safe HTML on one line. A URL is written percent-encoded, so /Straße is /Stra%C3%9Fe. fromHtml does not look for the main content of a page. Pass it the element you want.

The examples below show a short piece of HTML, one article from a whole page, and a server that returns a page as Markdown when the request has the header Accept: text/markdown.

Parameters

  • $htmlstring|Core\Html\Markup|Core\Xml\NodeThe HTML. It can be a string, a Core\Html\Markup, or a node from Core\Html::parse. Pass a node to convert only that part of a page. Elements are kept or removed as Core\Html::sanitize keeps or removes them.

Return value

Returns tainted string. The Markdown, as a tainted string. A table that Markdown cannot write, such as one with colspan, is written as HTML. A link with an unsafe URL is written as its text.

Examples

Text that looks like markdown

<?nvs
// Shows how a short piece of HTML becomes Markdown, and how its text stays text.
string $comment = '<p>Use <b>2 * 3</b> or [see the docs](/help).</p>'
. '<p># This is not a heading.</p>'
. '<script>alert("hi")</script>';
// `<b>` becomes `**`. The `*`, `[`, `]` and `#` in the text get a backslash,
// so they show as the characters they are. The `<script>` element and its text are deleted.
echo Core\Markdown::fromHtml($comment);
Output
Use **2 \* 3** or \[see the docs\](/help).
\# This is not a heading.

Only the article of a page

<?nvs
// Shows how to convert only the article of a web page, without its menu and footer.
class Find
{
// Returns the first element with this tag name under `$node`, or `null` if there is none.
public static function element(Core\Xml\Node $node, string $name): ?Core\Xml\Node
{
foreach ($node->children() as Core\Xml\Node $child) {
if ($child->kind() == Core\Xml\Node::Kind::Element && $child->name() == $name) {
return $child;
}
?Core\Xml\Node $found = Find::element($child, $name);
if ($found != null) {
return $found;
}
}
return null;
}
}
string $page = '<html><body>'
. '<nav><a href="/">Home</a> <a href="/blog">Blog</a></nav>'
. '<article><h1>Opening hours</h1>'
. '<p>The shop is open <em>Monday to Friday</em>.</p>'
. '<ul><li>Morning: 9 to 12</li><li>Afternoon: 14 to 18</li></ul></article>'
. '<footer>Copyright Shop</footer>'
. '</body></html>';
// `fromHtml` converts everything it gets. Pass it only the `article` element,
// so the menu and the footer are not in the result.
?Core\Xml\Node $article = Find::element(Core\Html::parse($page), 'article');
if ($article != null) {
echo Core\Markdown::fromHtml($article);
}
Output
# Opening hours
The shop is open *Monday to Friday*.
- Morning: 9 to 12
- Afternoon: 14 to 18

A page as markdown for an ai agent

<?nvs
// Shows how a server returns a page as Markdown to a client that sends `Accept: text/markdown`.
Core\Html\Markup $page = html`<article>
<h1>Returns</h1>
<p>You can return an item within <strong>30 days</strong>.</p>
<p>Read the <a href="/help/returns/policy">return policy</a>.</p>
</article>`;
// An AI agent or a command-line tool often sends `Accept: text/markdown`.
// A browser sends `text/html`, so it gets the HTML page.
tainted string $accept = Core\Request::header('Accept') ?? '';
if (Core\Str::contains($accept, 'text/markdown')) {
tainted string $markdown = Core\Markdown::fromHtml($page);
Core\Response::sendBytes($markdown as bytes, 'text/markdown; charset=utf-8');
} else {
Core\Response::sendHtml($page);
}
Output
# Returns
You can return an item within **30 days**.
Read the [return policy](/help/returns/policy).