Extending image Kirbytag produces extra unwanted html

I’m trying to extend the image Kirbytag and wrap the image in another tag—very simple.

However, the below code results in extra junk. Images get wrapped in the intended <a> tag, but Kirby also generates additional <p> and <br> elements all around where the images are rendered. I can’t find a logic to it. In some places it’s an empty <p> and other times it’s a <p> with an empty <a> inside of it.

It’s almost as if my html is being reinterpreted as Kirbytext and the whitespace is getting re-parsed. Is that possible? Am I missing something really obvious?

		"imageLightbox" => [
			"html" => function ($tag) use ($originalTag) {
				$file = $tag->file($tag->value());
				$ratio = $file->ratio();
				$result = $originalTag["html"]($tag);

				// look for this
				$pattern = ["/<figure/i", "/<\/figure>/i"];

				// replace with this
				$figure = [
					'<a class="photoset__photo lightbox__photo" href="' . $file->url()  .'"><figure', "</figure></a>",
				];

				// replace the old image tag
				$result = preg_replace($pattern, $figure, $result);

				return $result;
			},
		],

Hi, I don’t know what the replacement code is good for in your code, because you replace the figure tags only to re-add them again. You might as well just wrap the original code into the a tags-

What causes the issue though is the inline wrapper. If you have multiple HTML elements, they need to be wrapped inside a block element.

Your point about rewriting the tag is well taken! I’m still working on the behavior. But regardless, I’m confused why it would matter whether I’m wrapping this in an inline or block element.

I didn’t realize my html would receive additional parsing after this transformation happens. Let’s say for the sake of argument that I need to rewrite figure to wrap in <a> without a parent block element. How can I achieve that?

Thanks for your help.