Skip to content

Core\Markdown\Live::push

Adds the next piece of Markdown text and returns the HTML that changed.

A piece can end anywhere, also in the middle of a word or a line. push returns a Core\Markdown\Update with two parts. done() is the HTML of the blocks that this piece finished. Add it after the HTML you already show. open() is the HTML of the rest, converted as if the text ended there. Show it after the finished HTML, and replace it at the next push.

A block (a paragraph, a list, a heading or a code block) is finished when the first line of the next block is complete. Its HTML does not change after that.

Good to know: a link like [the guide] gets its address from a line such as [the guide]: /guide. If that line comes after the block is finished, the link stays plain text. After finish, push throws a RuntimeError.

The examples below show the two parts after each piece, a block that does not change, and a reply sent to a browser while it is written.

Parameters

  • $piecestringThe next text of the document.

Return value

Returns Core\Markdown\Update. A Core\Markdown\Update. Its done() is the HTML of the blocks this piece finished. Add it after the HTML you already have. Its open() is the HTML of the rest. Show it after that, and replace it at the next push. A block is finished when the next block starts, and its HTML does not change after that.

Errors

  • RuntimeErrorfinish was already called, or a callback threw an error. A callback that calls push or finish on the same object also gets this error.

Examples

The two parts after each piece

<?nvs
// Shows what `done()` and `open()` contain after each piece.
Core\Markdown\Live $live = Core\Markdown::live();
// After the first piece, `**` is not closed yet. So `open()` shows the two stars as text.
array<string> $pieces = ['The order is **pa', "id**.\n\nIt ships on Mon", "day.\n"];
foreach ($pieces as string $piece) {
Core\Markdown\Update $update = $live->push($piece);
// The finished blocks. Add this HTML after what you already show.
echo "done:\n", $update->done();
// The rest, as if the text ended here. Replace the last `open()` with this.
echo "open:\n", $update->open();
}
echo "finish:\n", $live->finish();
Output
done:
open:
<p>The order is **pa</p>
done:
open:
<p>The order is <strong>paid</strong>.</p>
<p>It ships on Mon</p>
done:
<p>The order is <strong>paid</strong>.</p>
open:
<p>It ships on Monday.</p>
finish:
<p>It ships on Monday.</p>

A finished block does not change

<?nvs
// Shows that the HTML of a finished block stays the same, even when later text could change it.
Core\Markdown\Live $live = Core\Markdown::live();
// The second piece finishes the first paragraph. Its link has no address yet, so it is plain text.
$live->push("Read [the guide] first.\n\n");
echo $live->push("Then start.\n\n")->done();
// The address arrives now. The first paragraph is already finished, so it does not change.
echo $live->push("[the guide]: https://example.com/guide\n")->done();
echo $live->finish();
// The same text converted in one call gets the link.
echo "-- in one call:\n";
echo Core\Markdown::toHtml("Read [the guide] first.\n\nThen start.\n\n[the guide]: https://example.com/guide\n");
Output
<p>Read [the guide] first.</p>
<p>Then start.</p>
-- in one call:
<p>Read <a href="https://example.com/guide">the guide</a> first.</p>
<p>Then start.</p>

Stream a model answer to the browser

<?nvs
// Shows a server that sends a language model's answer to the browser as HTML while the model writes it.
// A real program reads these pieces from the model's API.
array<string> $pieces = ['Three ste', "ps:\n\n1. Pick a size\n2. Pay", "\n\nThe parcel ships **today**.\n"];
Core\Sse $sse = Core\Sse::startStream();
Core\Markdown\Live $live = Core\Markdown::live();
foreach ($pieces as string $piece) {
Core\Markdown\Update $update = $live->push($piece);
string $done = Core\Html::toSource($update->done(), 'the HTML is sent to the browser as JSON');
string $open = Core\Html::toSource($update->open(), 'the HTML is sent to the browser as JSON');
// The page adds an `append` event to the end of the answer. There is no event when nothing is finished.
if ($done != '') {
$sse->send(['html' => $done], event: 'append');
}
// The page removes the last `replace` part and shows this one instead.
$sse->send(['html' => $open], event: 'replace');
}
// The last event has the rest of the answer. The page removes the last `replace` part and adds this.
$sse->send(['html' => Core\Html::toSource($live->finish(), 'the HTML is sent to the browser as JSON')], event: 'end');
Output
event: replace
data: {"html":"\u003cp\u003eThree ste\u003c/p\u003e\n"}
event: append
data: {"html":"\u003cp\u003eThree steps:\u003c/p\u003e\n"}
event: replace
data: {"html":"\u003col\u003e\n\u003cli\u003ePick a size\u003c/li\u003e\n\u003cli\u003ePay\u003c/li\u003e\n\u003c/ol\u003e\n"}
event: append
data: {"html":"\u003col\u003e\n\u003cli\u003ePick a size\u003c/li\u003e\n\u003cli\u003ePay\u003c/li\u003e\n\u003c/ol\u003e\n"}
event: replace
data: {"html":"\u003cp\u003eThe parcel ships \u003cstrong\u003etoday\u003c/strong\u003e.\u003c/p\u003e\n"}
event: end
data: {"html":"\u003cp\u003eThe parcel ships \u003cstrong\u003etoday\u003c/strong\u003e.\u003c/p\u003e\n"}