Hi there
I have written a plugin to parse traditional markdown image tags with the kirbytext:before hook. It gets the file name, caption text and alt text from ![Alt text](image.jpg "Alt text")
with a nifty regular expression and spits out the correct html.
Kirby::plugin('ote/markdown-image', [
'hooks' => [
'kirbytext:before' => function ($text, $data) {
$text = preg_replace_callback('/!\[(.*?)\]\((.*?)\)(?: "([^"]+)")?/', function ($matches) use ($data) {
$alt = $matches[1] ?? null;
$src = $matches[2];
$caption = $matches[3] ?? null;
return '<figure><img src="' . $src . '" alt="' . $alt . '"><figcaption>' . $caption . '</figcaption></figure>';
}, $text);
return $text;
}
]
]);
However, I am having trouble figuring out how to use the $page variable correctly in the plugin, so I can build the correct image file path with something like $page->image($src)->url()
, eg returning it like this:
return '<figure><img src="' . $page->image($src)->url() . '" alt="' . $alt . '"><figcaption>' . $caption . '</figcaption></figure>';
Obviously this results in an error: Undefined variable $page.
Anyone have any tips?
Best Oliver