Apply featherlight lightbox to all images

Hi all,

Coming from Wordpress, I’d like to have all my images on all pages behaving the same way by default. That same way is: unless specified differently, a newly inserted image with the img tag should be centered and open in a lightbox.

For the centered part, I managed to define it in /themes/quark/css/custom.css

For automatically having featherlight, I am blocked. I find no option in the plugin to automatically apply to all images. Is there no smarter way than editing each page and add ?lightbox after each image file name?

Thanks

@jbd, If you like programming, you could create a little plugin that uses preg_replace() to alter the Markdown image definition.

Something like:

public function onPageContentRaw(Event $event)
{
  /** @var Page */
  $page = $event['page'];
  $content = $page->getRawContent();

  $content = preg_replace('#(!\[[^)]+)\)#', '$1?lightbox)', $content);

  $page->setRawContent($content);
}

This function will run on every page, but will only be executed once per page when the page is not yet cached, or its cache is stale after editing.

See:

Thanks @pamtbaau !
That’s actually very similar to editing the functions.php theme page in Wordpress. I’ll have to refresh my regex matching knowledge :slight_smile:

I thought there would be some solution native of Grav, e.g. with Twig/templating/shortcoding that is of a higher level than php. But the one you offered will work!

@jbd,

That’s actually very similar to editing the functions.php theme page in Wordpress.

Barely a week member of the Grav community and already insulting… :roll_eyes:

I thought there would be some solution native of Grav

I’m not aware of another solution, but I like coding, so I create my own…

This is the only solution I’m aware of that doesn’t need adding extra codes to the image definition (like shortcodes do) and which results are being cached and therefor doesn’t need run on every page request.

Ha, sorry :grin: is there some rivalry between Wordpress and Grav communities?

I’m trying Grav now as I find storing content and configurations in a DB overkill and impractical for some situations. I also think that sharing and adapting knowledge and components are foundational to science and progress and, when possible, very useful to keep moving technology fast. So, if it didn’t come across this way, I meant it in a positive way!