Skip to content

Core\Markdown\Update::open

$update->open(): Core\Html\Markup

Returns the HTML of the text that is not in a finished block yet, as if the document ended after the last piece.

Show this HTML after the finished blocks. At the next push, replace it with the new open part. The open part is usually the last block, such as a paragraph that is still being written. It can also be a list that is still growing, or an HTML element whose end tag has not arrived. Markdown that is not complete yet shows as text: a ** without its closing ** shows as two stars until the closing pair arrives.

Good to know: open is empty until the first block starts. For example, it is empty when the text so far has only blank lines or reference definitions.

The examples below show half-written bold text, a list that grows, and a chat page that shows an answer while it arrives.

Return value

Returns Core\Html\Markup. A Core\Html\Markup. Replace the open() part of the last push with it.

Examples

Half written bold is text

<?nvs
// Shows that `open` renders the text as if it ended after the last piece.
Core\Markdown\Live $live = Core\Markdown::live();
// The closing `**` has not arrived yet, so the stars are shown as text.
echo $live->push('Please read this **very')->open();
// Now the bold text is complete. `open` returns the paragraph again, with `<strong>`.
echo $live->push(' carefully** before you pay.')->open();
Output
<p>Please read this **very</p>
<p>Please read this <strong>very carefully</strong> before you pay.</p>

A list that grows

<?nvs
// Shows how `open` returns the whole list again each time a piece of it arrives.
Core\Markdown\Live $live = Core\Markdown::live();
array<string> $pieces = ["- milk\n", '- eg', "gs\n", "- bread\n"];
foreach ($pieces as string $piece) {
echo 'after ', Core\Json::encode($piece), ":\n";
// The list is not finished yet, so all of it is in `open`.
echo $live->push($piece)->open();
}
Output
after "- milk\n":
<ul>
<li>milk</li>
</ul>
after "- eg":
<ul>
<li>milk</li>
<li>eg</li>
</ul>
after "gs\n":
<ul>
<li>milk</li>
<li>eggs</li>
</ul>
after "- bread\n":
<ul>
<li>milk</li>
<li>eggs</li>
<li>bread</li>
</ul>

Show an answer while it arrives

<?nvs
// Shows how a chat page shows an answer while it arrives: the finished part, then the open part.
Core\Markdown\Live $live = Core\Markdown::live();
array<string> $pieces = ['Sure! Run `npm ', "install`.\n\nThen st", "art the server.\n"];
// `$finished` grows with every `done` part. `$open` is replaced by every new `open` part.
Core\Html\Markup $finished = '' as Core\Html\Markup;
Core\Html\Markup $open = '' as Core\Html\Markup;
foreach ($pieces as string $piece) {
Core\Markdown\Update $update = $live->push($piece);
$finished = $finished + $update->done();
$open = $update->open();
echo '--- the screen after ', Core\Json::encode($piece), "\n", $finished + $open;
}
Output
--- the screen after "Sure! Run `npm "
<p>Sure! Run `npm</p>
--- the screen after "install`.\n\nThen st"
<p>Sure! Run <code>npm install</code>.</p>
<p>Then st</p>
--- the screen after "art the server.\n"
<p>Sure! Run <code>npm install</code>.</p>
<p>Then start the server.</p>