Skip to content

Commit c998896

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

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

_config/filters.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DateTime } from "luxon";
22
import striptags from "striptags";
3+
import Image from "@11ty/eleventy-img";
34

45
export default function(eleventyConfig) {
56
eleventyConfig.addFilter("readableDate", (dateObj, format, zone) => {
@@ -70,6 +71,8 @@ export default function(eleventyConfig) {
7071
extractExcerpt(post)
7172
);
7273

74+
eleventyConfig.addFilter("contentImgUrlShortcode", contentImgUrlShortcode);
75+
7376
};
7477

7578
// Taken from here => https://keepinguptodate.com/pages/2019/06/creating-blog-with-eleventy/
@@ -85,3 +88,19 @@ function extractExcerpt(article) {
8588

8689
return striptags(content.slice(0, content.indexOf("\n")));
8790
}
91+
92+
async function contentImgUrlShortcode(src) {
93+
// src might be small.png - taken from frontmatter
94+
const imageSrc = (this.eleventy.directories.input + this.page.filePathStem + "/../" + src).replace("//", "/");
95+
let metadata = await Image(imageSrc, {
96+
widths: [600],
97+
formats: ["png"],
98+
urlPath: this.page.url,
99+
outputDir: `./_site/${this.page.url}`,
100+
});
101+
102+
const data = metadata.png[0];
103+
// data.url might be /blog/hello-world/xfO_genLg4-600.jpeg
104+
// note the filename is a content hash-width combination
105+
return data.url;
106+
}

_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 }}">

0 commit comments

Comments
 (0)