Skip to content

Core\Markdown\Live::finish

$live->finish(): Core\Html\Markup

Ends the Markdown text and returns the HTML of the blocks that are not finished yet.

Call finish after the last piece. It returns a Core\Html\Markup with the last blocks of the text and its footnotes. Add it after the done() parts, and remove the last open() part. The done() parts and the result of finish together are the whole text as HTML. If no link uses an address that is defined later in the text, this is the same HTML that Core\Markdown::toHtml returns.

Good to know: after finish, the object is closed. A second finish or another push throws a RuntimeError. For the next text, create a new one with Core\Markdown::live.

Return value

Returns Core\Html\Markup. A Core\Html\Markup with the HTML of the last blocks and the footnotes. Add it after the done() parts, and remove the last open() part. A link that uses a definition from a later block is written as text.

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 last block

<?nvs
// Shows that `finish` returns the last block, which no `push` finished.
Core\Markdown\Live $live = Core\Markdown::live();
echo $live->push("# Release 2.4\n\nFaster sea")->done();
echo $live->push("rch.\n\n- New filters\n- Fewer bugs")->done();
// The list is the last block. No block comes after it, so only `finish` returns it.
echo "-- finish:\n";
echo $live->finish();
Output
<h1>Release 2.4</h1>
<p>Faster search.</p>
-- finish:
<ul>
<li>New filters</li>
<li>Fewer bugs</li>
</ul>

A text ends once

<?nvs
// Shows that the object is closed after `finish`, and how to start the next text.
Core\Markdown\Live $live = Core\Markdown::live();
$live->push('First answer.');
echo $live->finish();
// The text has ended, so another `push` throws a `RuntimeError`. So does a second `finish`.
try {
$live->push('More text.');
} catch (RuntimeError $e) {
echo "push: closed\n";
}
try {
$live->finish();
} catch (RuntimeError $e) {
echo "finish: closed\n";
}
// For the next text, create a new object.
$live = Core\Markdown::live();
$live->push('Second answer.');
echo $live->finish();
Output
<p>First answer.</p>
push: closed
finish: closed
<p>Second answer.</p>

Save the whole answer

<?nvs
// Shows how to build the whole HTML of an answer from its pieces, to save it when the answer is complete.
array<string> $pieces = ["## Returns\n\nYou can return an item for 30 d", "ays.\n\n> Keep the receipt", ".\n"];
Core\Markdown\Live $live = Core\Markdown::live();
Core\Html\Markup $answer = '' as Core\Html\Markup;
foreach ($pieces as string $piece) {
// A page would show `open()` too. Only the finished blocks go into the saved answer.
$answer = $answer + $live->push($piece)->done();
}
// `finish` returns the last block, the quote. Add it after the finished blocks.
$answer = $answer + $live->finish();
echo $answer;
// The saved HTML is the same HTML that `Core\Markdown::toHtml` returns for the whole text.
Core\Html\Markup $whole = Core\Markdown::toHtml(Core\Str::join($pieces, ''));
echo Core\Html::toSource($answer, 'the two results are compared') == Core\Html::toSource($whole, 'the two results are compared') ? "same\n" : "different\n";
Output
<h2>Returns</h2>
<p>You can return an item for 30 days.</p>
<blockquote>
<p>Keep the receipt.</p>
</blockquote>
same