Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Image Directive :target: Broken #49

Open
jandiorio opened this issue Nov 12, 2019 · 1 comment
Open

Image Directive :target: Broken #49

jandiorio opened this issue Nov 12, 2019 · 1 comment

Comments

@jandiorio
Copy link

Problem

The image() function generates the RST directive which includes the :target: attribute. Since Sphinx copies all images to /_images, any images in subfolders will have a broken link using :target: which which include the subdirectory path. Since the :target: attribute still references the relative path based on the source directory and not the /_images directory, clicking the links generates a 404.

Example Output of Broken href

You can see the difference in paths. the href path will not exists. In source, these images are in a subdirectory.

<a class="reference external image-reference" href="../_images/curl_jokes_json.gif"><img alt="cURL with JSON" src="../../_images/curl_jokes_json.gif" /></a>

Possible Fix

Since it seems that Sphinx always copies all images to the /_images folder, modifying the method like this could work:

def image(self, src, title, text):
        """Rendering a image with title and text.

        :param src: source link of the image.
        :param title: title text of the image.
        :param text: alt text of the image.
        """

        image_name = os.path.basename(src)
        target = f"/_images/{image_name}"

        # rst does not support title option
        # and I couldn't find title attribute in HTML standard
        return '\n'.join([
            '',
            '.. image:: {}'.format(src),
            '   :target: {}'.format(target),
            '   :alt: {}'.format(text),
            '',
        ])

Essentially, parsing the filename from the original source and generating target based on where we know sphinx is going to put the images.

@sk-dev79
Copy link

sk-dev79 commented Jan 29, 2020

Had a similar problem. I'm cloning a gitlab wiki (which is in markdown) into my sphinx project via .gitlab_ci.yml
Wanted to have Lightbox feature from: sphinxcontrib-images.

Didn't work as it only parses .rst images. So I found m2r. Still didn't work - because of target.
Removed it - fine.

I actually don't wanna have target at all - becaus click opens the lightbox. Maybe make it configurable?

But currently I'm converting via shell - so conf.py option wouldn't help. Have markdown extension installed (currently) - and you can install markdown and m2r (which does make some sense ...).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants