Manipulate the resulting HTML Output

At what point in the script could I grab the HTML output of the page and manipulate it?

When I look at the Hooks, I see that either route:after or kirbytext:after is the last one - unfortunately, I don’t use kirbytext all the time.

I guess I need an entry point, maybe when a template was rendered?

My use case is DOM Manipulation, which could be either remove Whitespace between tags or DOM Document stuff.

Maybe this will do what you want?

As for white space im guessing you minify, there are plugins for that too :slight_smile:

tree-methods almost looks like what I want (and I’ll save that for later) but it only has access to a field, not the entire document.

One of the use cases I have in mind is:

Building a TOC even if you used the Kirby Editor (I read through the Cookbook entry about TOC and it’s limited to kirbytext fields)

Or adding custom attributes to all links in a page as JS hooks.

Some things could be done on the template/snippet level, but something like the TOC seems impossible otherwise.

I saw that in route:after one has access to $page->render() but I don’t know how to stuff html back into the page object.

That is even easier then using textarea fields, because you can filter blocks. No need to manipulate the DOM for this purpose.

Editor snippet for headline

<h<?= $level ?> id="<?= Str::slug($content) ?>"><?= $content ?></h<?= $level ?>>


$page->text()->blocks()->filterby('type', 'h2')

You can of course do that for multiple headline levels.