Core\Markdown::toHtmlEach
Core\Markdown::toHtmlEach(
Core\Bytes::Source|Core\Bytes::Chunks $markdown,
?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\GroupsConverts a Markdown document to HTML one part at a time, so a large document does not have to be in memory all at once.
You read the result with a foreach loop. Each part is a Core\Html\Markup with one or more whole blocks, such as a heading, a paragraph or a list. The parts joined together are the same HTML that Core\Markdown::toHtml returns. The document can be an open file, an uploaded file, Core\Request::bodyStream(), bytes, or a list or loop that gives bytes pieces. The link, image, heading and code functions work as they do for toHtml.
A file is read twice. The first read finds every link definition, so a link can use a definition at the end of the file. After that, only one part of the file is in memory at a time. Every other source is kept in memory until the loop ends.
Good to know: a block that is never closed is one part. A code block with no closing fence, or one long list, is one part, however large it is. Bytes that are not UTF-8 throw a ParseError when the loop reaches them.
Parameters
$markdownCore\Bytes::Source|Core\Bytes::ChunksThe Markdown document: abytesvalue, an open file, an upload part, a request body stream, or a list ofbyteschunks. The bytes must be UTF-8. A file is read twice, and only one part is in memory at a time. Every other source is kept in memory until the loop ends.$link?callable(string): ?stringdefault nullThe samelink:function as inCore\Markdown::toHtml. It runs before the loop receives the part with the link.$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.
Return value
Returns Core\Markdown\Groups. A Core\Markdown\Groups that a foreach loop reads. Each part is a Core\Html\Markup. The parts joined together are the same HTML that Core\Markdown::toHtml returns for the whole document. Nothing is read until the loop starts.
Errors
ParseErrorThe document is not valid UTF-8. The message gives the position of the first wrong byte. The loop throws it when it reaches that byte.
Examples
Print a file part by part
<?nvs// Shows how to convert a Markdown file to HTML one part at a time.
// The program writes a small file first, so that it has one to read.string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'changes.md');Core\IO::write( $path, "# Version 2.1\n\nSearch is faster now.\n\n## Fixed\n\n" . "- Dates in March were shown as April.\n- Long names were cut off.\n",);
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// Each part is a `Core\Html\Markup` with one or more whole blocks.// The list is one part, because a list is never cut in two.int $number = 0;foreach (Core\Markdown::toHtmlEach($file) as Core\Html\Markup $part) { $number = $number + 1; echo 'part ', $number, ":\n"; echo $part;}$file->close();part 1:<h1>Version 2.1</h1>part 2:<p>Search is faster now.</p>part 3:<h2>Fixed</h2>part 4:<ul><li>Dates in March were shown as April.</li><li>Long names were cut off.</li></ul>A link defined at the end of the file
<?nvs// Shows that a link in the first part can use a definition from the last line of the file.
// `[the install guide][install]` is a link. Its URL is on the last line of the file.string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'readme.md');Core\IO::write( $path, "Read [the install guide][install] before you start.\n\n" . "# Usage\n\nRun the program with no arguments.\n\n" . "[install]: https://example.com/install\n",);
// The loop reads the file twice. The first read finds the definition,// so the first part already has the link.Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);foreach (Core\Markdown::toHtmlEach($file) as Core\Html\Markup $part) { echo $part;}$file->close();<p>Read <a href="https://example.com/install">the install guide</a> before you start.</p><h1>Usage</h1><p>Run the program with no arguments.</p>Send a help page in parts
<?nvs// Shows a help page that is sent to the browser one part at a time.
Core\Response\Stream $page = Core\Response::startStream('text/html; charset=utf-8');$page->write("<!doctype html>\n<title>Install</title>\n");
// `pages/install.md` is next to this program. The response gets each part// as soon as it is converted, so only one part is in memory at a time.Core\IO\File $file = Core\IO::open(Core\Path::thisDirectory('pages/install.md'), Core\IO::FileMode::Read);foreach (Core\Markdown::toHtmlEach($file) as Core\Html\Markup $part) { $page->write(Core\Html::toSource($part, 'the stream sends text'));}$file->close();<!doctype html><title>Install</title><h1>Install</h1><p>Download the archive for your system and unpack it.</p><pre><code class="language-sh">tar -xzf shop-2.1.tar.gz</code></pre><h2>Check the version</h2><p>Run <code>shop --version</code>. It prints <code>2.1</code>.</p>