You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A similar question has been risen here, but I'm unable to make it properly work: #55
Relevant files in project:
_includes
layouts
post.njk
partials
head.njk
content
blog
2025
article-folder
index.md
header.png
blog.11tydata.js
head.njk is a file that contains all the tags that should go between <head> and </head>. The articles have front-matter that may contain property called image. (with value of "header.png") post.njkis a layout file for the posts defined in blog.11tydata.js.
I would like to add the following line to head.njk: {% if image %}<meta property="og:image" content="{{ ??? }}" />{% endif %}
What should be in the place of ????
I've tried multiple options like:
{{ page.url | absoluteUrl(metadata.url) }}{{ image }}
This file will not exist in the output.
Created a shortcode roughly based on the earlier issue above, but couldn't supply the image path to the Image object properly.
One important part is that all my posts get a "permalink" computed for them with the following config for eleventy, using title for slug instead of file path itself:
// Set default computed data for all templates
eleventyConfig.addGlobalData("eleventyComputed", {
permalink: data => {
if (!data.permalink) {
// Automatically slugify titles for URLs if no permalink is explicitly set
var year = data.date
? new Date(data.date).getFullYear() + "/"
: "";
var name = slugify(data.title ?? data.page.fileSlug, { lower: true, strict: true }) + "/";
return `/${year}${name}`;
}
return data.permalink; // Use the explicitly set permalink if available
}
});
So in the end, the page.url can not be used for the image source path calculation. It should be somehow relative to the input file of content/blog/2025/blog-folder/index.md.
How could I let the eleventyImageTransformPlugin process a image, that is defined in front-matter as relative path to provide me an URL that I could use in head.njk for tags?
The text was updated successfully, but these errors were encountered:
I think I was able to solve it, but please let me know if I've "overdone" it. It looks a bit cumbersome and maybe I'm reinventing a wheel. Please do say so, if I do.
So I created the following function:
asyncfunctioncontentImgUrlFilter(src){constinputDir=path.dirname(this.page.inputPath);constimagePath=path.resolve(inputDir,src);constoutputDir=path.dirname(this.page.outputPath);consturlPath=this.page.url;conststats=awaitImage(imagePath,{widths: [1200],// Width for Open Graph imageformats: ["png"],outputDir: outputDir,// Output directoryurlPath: urlPath,// Public URL pathfilenameFormat: function(hash,src,width,format){return`${hash}-${width}.${format}`;}});returnstats.png[0].url;// Return the URL of the processed image}
A similar question has been risen here, but I'm unable to make it properly work: #55
Relevant files in project:
head.njk
is a file that contains all the tags that should go between<head>
and</head>
. The articles have front-matter that may contain property calledimage
. (with value of "header.png")post.njk
is a layout file for the posts defined inblog.11tydata.js
.I would like to add the following line to
head.njk
:{% if image %}<meta property="og:image" content="{{ ??? }}" />{% endif %}
What should be in the place of
???
?I've tried multiple options like:
{{ page.url | absoluteUrl(metadata.url) }}{{ image }}
This file will not exist in the output.
shortcode
roughly based on the earlier issue above, but couldn't supply the image path to theImage
object properly.One important part is that all my posts get a "permalink" computed for them with the following config for eleventy, using title for slug instead of file path itself:
So in the end, the
page.url
can not be used for the image source path calculation. It should be somehow relative to the input file ofcontent/blog/2025/blog-folder/index.md
.How could I let the
eleventyImageTransformPlugin
process a image, that is defined in front-matter as relative path to provide me an URL that I could use inhead.njk
for tags?The text was updated successfully, but these errors were encountered: