Skip to content

Core\Markdown\Update::done

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

Returns the HTML of the blocks that the last piece finished.

A block is a paragraph, a heading, a list, a code block or another top-level part of the document. A block is finished when the first line of the next block is complete. Add the result of done after the HTML you already have. A finished block never changes, so you never replace it. When the piece finished no block, done returns an empty Core\Html\Markup.

Good to know: a reference definition that arrives after a block is finished does not change that block. A link in that block that needs the definition stays plain text.

The examples below show when a paragraph is finished, how to build the whole page from every done part, and when a code block is finished.

Return value

Returns Core\Html\Markup. A Core\Html\Markup. Add it after the HTML you already have.

Examples

A block is done when the next one starts

<?nvs
// Shows when `done` returns a paragraph: after the first line of the next block arrives.
Core\Markdown\Live $live = Core\Markdown::live();
// The blank line alone does not finish the first paragraph. The complete line after it does.
array<string> $pieces = ['Our shop is open ', "from 9 to 18.\n", "\n", "Closed on Sundays.\n"];
foreach ($pieces as string $piece) {
string $done = Core\Html::toSource($live->push($piece)->done(), 'the example prints the HTML');
// An empty `done` means that this piece finished no block.
echo 'piece ', Core\Json::encode($piece), ' -> done: ', $done == '' ? "(empty)\n" : $done;
}
// The last paragraph is not finished yet. `finish` returns it.
echo 'finish: ', $live->finish();
Output
piece "Our shop is open " -> done: (empty)
piece "from 9 to 18.\n" -> done: (empty)
piece "\n" -> done: (empty)
piece "Closed on Sundays.\n" -> done: <p>Our shop is open from 9 to 18.</p>
finish: <p>Closed on Sundays.</p>

Build the page from every done

<?nvs
// Shows how to build the whole page: add every `done` part, and then the result of `finish`.
string $post = "# Release notes\n\nVersion 2 is **faster**.\n\n- New search\n- Dark mode\n\nThanks for reading.\n";
// The post arrives in pieces of 10 characters, like an answer that is still being written.
array<string> $pieces = Core\Str::chunk($post, 10);
Core\Markdown\Live $live = Core\Markdown::live();
Core\Html\Markup $page = '' as Core\Html\Markup;
foreach ($pieces as string $piece) {
// A finished block never changes, so it is added once and kept.
$page = $page + $live->push($piece)->done();
}
$page = $page + $live->finish();
echo $page;
// The page is the same HTML that `Core\Markdown::toHtml` returns for the whole post. This prints `true`.
string $whole = Core\Html::toSource(Core\Markdown::toHtml($post), 'the example compares the HTML');
echo Core\Html::toSource($page, 'the example compares the HTML') == $whole ? 'true' : 'false', "\n";
Output
<h1>Release notes</h1>
<p>Version 2 is <strong>faster</strong>.</p>
<ul>
<li>New search</li>
<li>Dark mode</li>
</ul>
<p>Thanks for reading.</p>
true

A code block is done after its fence

<?nvs
// Shows that a code block is finished only after its closing ``` line and the next line.
Core\Markdown\Live $live = Core\Markdown::live();
// The blank line inside the code block does not finish it.
array<string> $pieces = ["```sh\n", "cd shop\n", "\n", "make\n", "```\n", "Then open the browser.\n"];
int $step = 1;
foreach ($pieces as string $piece) {
string $done = Core\Html::toSource($live->push($piece)->done(), 'the example prints the HTML');
echo $step, '. done: ', $done == '' ? "(empty)\n" : $done;
$step = $step + 1;
}
Output
1. done: (empty)
2. done: (empty)
3. done: (empty)
4. done: (empty)
5. done: (empty)
6. done: <pre><code class="language-sh">cd shop
make
</code></pre>