Core\Markdown::live
Core\Markdown::live(
?callable(string): ?string $link = null,
?callable(string): ?string $image = null,
?callable(int, string): ?string $heading = null,
?callable(string, string): ?Core\Html\Markup $code = null,
): Core\Markdown\LiveCreates an object that converts Markdown to HTML while the text is still arriving.
Use it for text that comes in pieces, such as the answer of a language model that you show while it is written. Core\Markdown::live returns a Core\Markdown\Live with no text yet. You give it each piece with push, and you call finish when the text is complete. The functions link, image, heading and code are optional. They work the same way as in Core\Markdown::toHtml.
Good to know: a block (a paragraph, a list, a heading or a code block) is converted again at each push until it is finished. So your code or heading function can run more than once for the same block. It should return the same result each time.
The examples below show one reply in three pieces, how often a code function runs, and a chat reply whose links are checked.
Parameters
$link?callable(string): ?stringdefault nullThe samelink:function as inCore\Markdown::toHtml.$image?callable(string): ?stringdefault nullThe sameimagefunction as inCore\Markdown::toHtml.$heading?callable(int, string): ?stringdefault nullThe sameheadingfunction as inCore\Markdown::toHtml.$code?callable(string, string): ?Core\Html\Markupdefault nullThe samecodefunction as inCore\Markdown::toHtml. A block that is not finished is converted again at eachpush, so a function can run more than once for the same block.
Return value
Returns Core\Markdown\Live. A Core\Markdown\Live with no text yet.
Examples
One reply in three pieces
<?nvs// Shows how a reply that arrives in three pieces becomes HTML.
Core\Markdown\Live $live = Core\Markdown::live();
// The pieces end in the middle of a word, as the pieces of a model's answer often do.array<string> $pieces = ['## Shop', "ping list\n\n- bread\n- mi", "lk\n"];foreach ($pieces as string $piece) { Core\Markdown\Update $update = $live->push($piece); // `done()` is the HTML of the finished blocks. `open()` is the HTML of the rest. echo "done:\n", $update->done(); echo "open:\n", $update->open();}
// `finish` returns the HTML of the blocks that were still open.echo "finish:\n", $live->finish();done:open:<h2>Shop</h2>done:<h2>Shopping list</h2>open:<ul><li>bread</li><li>mi</li></ul>done:open:<ul><li>bread</li><li>milk</li></ul>finish:<ul><li>bread</li><li>milk</li></ul>How often a code function runs
<?nvs// Shows that a `code` function runs again for a code block that is not finished yet.
class Highlight{ public static uint $runs = 0;
// Puts each code block in a box with its language. A real program would color the code here. public static function code(string $language, string $code): ?Core\Html\Markup { Highlight::$runs = Highlight::$runs + 1; return html`<pre class="box" data-language="{$language}">{$code}</pre>`; }}
Core\Markdown\Live $live = Core\Markdown::live( code: fn(string $language, string $code): ?Core\Html\Markup => Highlight::code($language, $code),);
// The code block arrives in three pieces. Each `push` converts the open block again.$live->push("```shell\ncd app\n");$live->push("nvs run main.nvs\n");// The third piece finishes the code block, so `done()` contains the box.echo $live->push("```\n\nThat is all.\n")->done();// This prints `runs: 3`.echo 'runs: ', Highlight::$runs, "\n";
// The code block is finished now, so `finish` does not run the function again.echo $live->finish();echo 'runs: ', Highlight::$runs, "\n";<pre class="box" data-language="shell">cd appnvs run main.nvs</pre>runs: 3<p>That is all.</p>runs: 3A chat reply with checked links
<?nvs// Shows a chat reply from a language model, converted while it arrives, with only safe links kept.
class Reply{ // Only links to the help pages stay links. For any other link, only the text stays. public static function link(string $url): ?string { if (Core\Str::startsWith($url, 'https://help.example.com/')) { return $url; } return null; }}
Core\Markdown\Live $live = Core\Markdown::live(link: fn(string $url): ?string => Reply::link($url));
// The model sends its answer in small pieces.array<string> $pieces = [ 'To reset your password, open the [help pa', "ge](https://help.example.com/password).\n\n", "Do not use [this site](https://login.example.net) for it.\n",];
// Each finished block is added to the HTML that is saved in the database.// The second link is not a help page, so it prints as plain text.string $saved = '';foreach ($pieces as string $piece) { $saved = $saved . Core\Html::toSource($live->push($piece)->done(), 'the reply is saved in the database');}$saved = $saved . Core\Html::toSource($live->finish(), 'the reply is saved in the database');echo $saved;<p>To reset your password, open the <a href="https://help.example.com/password">help page</a>.</p><p>Do not use this site for it.</p>