Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I hyperlink to a full size image? #227

Open
matt-y opened this issue May 3, 2024 · 2 comments
Open

How can I hyperlink to a full size image? #227

matt-y opened this issue May 3, 2024 · 2 comments

Comments

@matt-y
Copy link

matt-y commented May 3, 2024

Hello I am using the 11ty base template which defines the image short code as follows:

eleventyConfig.addAsyncShortcode("image", async function imageShortcode(src, alt, widths, sizes) {
		// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
		// Warning: Avif can be resource-intensive so take care!
		let formats = ["avif", "webp", "auto"];
		let input;
		if(isFullUrl(src)) {
			  input = src;
		} else {
			  input = relativeToInputPath(this.page.inputPath, src);
		}

		let metadata = await eleventyImage(input, {
			  widths: widths || ["auto"],
			  formats,
			  outputDir: path.join(eleventyConfig.dir.output, "img"), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
		});

		// TODO loading=eager and fetchpriority=high
		let imageAttributes = {
			  alt,
			  sizes,
			  loading: "lazy",
			  decoding: "async",
		};

		return eleventyImage.generateHTML(metadata, imageAttributes);
});

I am using the shortcode like so:

{% image "./my-image.jpg", "Alt text" %}

What is the shortest way to get this image to display at a smaller size but hyperlink to the full size? Modifying the default value of ["auto"] for the width field in the default site's image shortcode implementation I am seeing the 600px image used in the generated blog post. However, I still have the full size image I'd like to link to. Is there a way to do this out of the box?

@SunfireWeb
Copy link

You may want to consider wrapping your shortcode in a standard link tag with the href set to the original image filepath.

Such as:

<a href="path/to/original/file.jpg" title="View full size image in new tab" target="_blank" rel="noopener">{% image "./my-image.jpg", "Alt text" %}</a>

@zeroby0
Copy link
Contributor

zeroby0 commented Jan 24, 2025

You can wrap the 11ty-img generated HTML in custom HTML and return that. You can even wrap an entire shortcode inside another.

    return `<a href="${full_size_output_url}">
                ${eleventyImage.generateHTML(metadata, imageAttributes)}
            </a>`

You can use statsSync or it's friends to get the full size output image url with hash.

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

No branches or pull requests

4 participants