Skip to content

Commit 16ede61

Browse files
committed
Fix header image in meta tags.
1 parent b8435ae commit 16ede61

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

_config/filters.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { DateTime } from "luxon";
22
import striptags from "striptags";
3+
import Image from "@11ty/eleventy-img";
4+
import path from 'node:path';
5+
import { Console } from "node:console";
36

47
export default function(eleventyConfig) {
58
eleventyConfig.addFilter("readableDate", (dateObj, format, zone) => {
@@ -70,6 +73,8 @@ export default function(eleventyConfig) {
7073
extractExcerpt(post)
7174
);
7275

76+
eleventyConfig.addFilter("contentImgUrlShortcode", contentImgUrlShortcode);
77+
7378
};
7479

7580
// Taken from here => https://keepinguptodate.com/pages/2019/06/creating-blog-with-eleventy/
@@ -85,3 +90,21 @@ function extractExcerpt(article) {
8590

8691
return striptags(content.slice(0, content.indexOf("\n")));
8792
}
93+
94+
async function contentImgUrlShortcode(src) {
95+
const projectPath = this.eleventy.env.root;
96+
const contentPath = path.dirname(this.page.inputPath).replace("./", this.eleventy.env.root + "/");
97+
const imageSrc = (contentPath + "/" + src).replace("//", "/");
98+
99+
let metadata = await Image(imageSrc, {
100+
widths: [600],
101+
formats: ["png"],
102+
urlPath: this.page.url,
103+
outputDir: `./_site/${this.page.url}`,
104+
});
105+
106+
const data = metadata.png[0];
107+
// data.url might be /blog/hello-world/xfO_genLg4-600.jpeg
108+
// note the filename is a content hash-width combination
109+
return data.url;
110+
}

_config/shortcodes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export default function(eleventyConfig) {
2-
};
3-
2+
};

_includes/layouts/post.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ h1.post-title {
3737
}
3838

3939
{%- endcss %}
40+
4041
<article>
4142
{%- if image %}
4243
<img src="{{ image }}" alt="" class="post-header" />

_includes/partials/head.njk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"headline": "{{ title or metadata.title }}",
2020
"description": "{{ description or metadata.description }}",
21-
{% if image %}"image": "{{ page.url | absoluteUrl(metadata.url) }}{{ image }}",{% endif %}
21+
{% if image %}"image": "{{ image | contentImgUrlShortcode | absoluteUrl(metadata.url) }}",{% endif %}
2222
"author": {
2323
"@type": "Person",
2424
"name": "{{ metadata.author.name }}"
@@ -42,12 +42,12 @@
4242
<meta property="og:type" content="website">
4343
<meta property="og:title" content="{% if title %}{{ title }} | {% endif %}{{ metadata.title }}">
4444
<meta property="og:description" content="{% if description %}{{ description }}{% else %}{{ metadata.description }}{% endif %}">
45-
{% if image %}<meta property="og:image" content="{{ page.url | absoluteUrl(metadata.url) }}{{ image }}" />{% endif %}
45+
{% if image %}<meta property="og:image" content="{{ image | contentImgUrlShortcode | absoluteUrl(metadata.url) }}" />{% endif %}
4646

4747
<meta property="twitter:card" content="summary_large_image">
4848
<meta name="twitter:title" content="{% if title %}{{ title }} | {% endif %}{{ metadata.title }}" />
4949
<meta name="twitter:description" content="{{ description or metadata.description }}" />
50-
{% if image %}<meta name="twitter:image" content="{{ page.url | absoluteUrl(metadata.url) }}{{ image }}" />{% endif %}
50+
{% if image %}<meta name="twitter:image" content="{{ image | contentImgUrlShortcode | absoluteUrl(metadata.url) }}" />{% endif %}
5151

5252
<meta name="robots" content="index, follow" />
5353
<link rel="alternate" href="{{ 'feed/feed.xml' | absoluteUrl(metadata.url) }}" type="application/atom+xml" title="{{ metadata.title }}">

eleventy.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ export const config = {
146146

147147
// These are all optional:
148148
dir: {
149-
input: "content", // default: "."
149+
input: "./content", // default: "."
150150
includes: "../_includes", // default: "_includes" (`input` relative)
151151
data: "../_data", // default: "_data" (`input` relative)
152-
output: "_site"
152+
output: "./_site"
153153
},
154154

155155
// -----------------------------------------------------------------

0 commit comments

Comments
 (0)