Back
[Ghost] Fixing FancyBox Implementation for Ghost Blog Images

[Ghost] Fixing FancyBox Implementation for Ghost Blog Images

A global solution for integrating FancyBox with Ghost markdown image blocks.

Introduction

FancyBox is a popular image lightbox tool that uses jQuery.

It allows you to easily create slideshow-like effects for your images, and it is fully responsive (RWD).

The Ghost Fancybox Challenge

Unlike static site generators like Hexo or Hugo, Ghost's content handling doesn't allow for easy adjustments to image rendering directly from the theme.

In plain English: Ghost's content format is determined by the framework itself. You can't easily modify it unless you're willing to hack the core engine source code.

While Ghost version 3 introduced some changes—similar to how WordPress handles images by adding default classes that you can override via theme CSS (https://ghost.org/docs/themes/content/)—I found the implementation to be quite unintuitive and inconsistent.

Furthermore, there is a major issue:

The rendering of images inserted via Ghost's Markdown block differs from those inserted via the native Editor.

For someone like me who relies heavily on Markdown blocks to write posts, the standard methods simply don't work.

Existing Solutions Online

xiaoluoboding proposed a solution in his Ghost theme design:

The Correct Way to Use Fancybox in a Ghost Blog
Preview [/content/images/2020/06/view1.jpg] [/content/images/2020/06/view2.jpg] [/content/images/2020/06/view3.jpg] Usage: In the Ghost backend, use the Markdown editor. Ctrl + Shift + I for image shortcut, generating: ![](http://) Ctrl + K for link shortcut, generating: [](http://) The correct format for Fancybox is essentially an image link. So, put the image inside the link. Format: [![](http://)](http://) For example: […

Side note: When I first started my Ghost blog and wanted to learn how to customize themes, I referenced his theme, kaldorei, extensively.

Anyway, let's look at his proposed solution:

It might look a bit confusing at first, so let's cover some basics.

Background Knowledge

Ghost Markdown Block RenderingWhen we type ![](....) in the Ghost Markdown editor, the generated HTML looks like this:

<p><img src="..." /></p>

You read that right—that's it. No classes, no attributes. This is quite frustrating; perhaps I'll write another post about the drawbacks of using Ghost as a blog later...

Fancybox UsageFancybox requires an anchor tag (link) wrapping the image, and a data-fancybox attribute to act as a reference.

<a href="imgUrl" data-fancybox="..." /><img src="imgUrl /></a>

xiaoluoboding's Approach

xiaoluoboding suggests wrapping the Markdown image syntax with a link to solve the problem:

[![view1](http://__SITE__DOMAIN__/content/images/2020/06/view1.jpg)](http://__SITE__DOMAIN__/content/images/2020/06/view1.jpg)

Essentially, by wrapping it in a Markdown link, it generates the necessary <a> tag for Fancybox to grab and render.

Then, Fancybox can successfully select the image:

$('.content a:has(img)').addClass('fancybox');
$(".fancybox").attr('data-fancybox', 'images').fancybox({
    selector: '[data-fancybox="images"]',
But... I'm lazy. The method above makes the Markdown syntax very long and tedious to write. How can we solve this globally?

The Global Solution

We want to keep the Markdown input simple (![]()) while achieving this:

<p><img...></p>`  =>  `<p><a><img ... /></p>

Since we are already using jQuery, why not use a wrapper to encapsulate it?

Using CSS selectors for classes and images, we can easily target the images and use wrap() or wrapAll() to add the necessary <a> tag around the <img>.

Solution Script

var images = $('.post .content img');

for (i = 0; i < images.length; i++) {
    $(images[i]).wrap('<a href="' + $(images[i]).attr('src') + '">');
}

This allows you to keep writing your Markdown the lazy way.

Note: The CSS selector above is just a conceptual example. Remember to adjust it based on your specific theme and Ghost's native kg-* classes to avoid unintended side effects on your site.

That's it! Feel free to leave a comment if you have any questions.

Written by setsal.lan in