Add Site + Open Graph Metadata (#1660)

* adding site data to `.eleventy.js`

* adding open graph metadata to `/eleventy.js`

* adding Open Graph metadata to head.njk

* adding default site Open Graph image

* updating formatting for title metadata

* addressing PR feedback
This commit is contained in:
Brian Talbot
2025-10-27 18:17:01 -04:00
committed by GitHub
parent d18d44eaa6
commit 986b8c4d99
3 changed files with 31 additions and 1 deletions

View File

@@ -94,6 +94,16 @@ export default async function (eleventyConfig) {
flashes: '',
});
// Site metadata for social sharing (Open Graph, canonical URLs, etc.)
const siteMetadata = {
url: 'https://webawesome.com',
name: 'Web Awesome',
description: 'Build better with Web Awesome, the open source library of web components from Font Awesome.',
image: 'https://webawesome.com/assets/images/open-graph/default.png',
};
eleventyConfig.addGlobalData('siteMetadata', siteMetadata);
// Template filters - {{ content | filter }}
eleventyConfig.addFilter('inlineMarkdown', content => markdown.renderInline(content || ''));
eleventyConfig.addFilter('markdown', content => markdown.render(content || ''));
@@ -172,6 +182,16 @@ export default async function (eleventyConfig) {
// Attach lastUpdatedISO to page data so templates can use {{ lastUpdatedISO }} directly
eleventyConfig.addGlobalData('eleventyComputed', {
lastUpdatedISO: data => getLastModifiedISO(data.page?.inputPath, data.lastUpdated),
// Open Graph metadata with smart defaults
ogTitle: data => data.ogTitle || data.title,
ogDescription: data => data.ogDescription || data.description,
ogImage: data => data.ogImage || siteMetadata.image,
ogUrl: data => {
if (data.ogUrl) return data.ogUrl;
const url = data.page?.url || '';
return url ? `${siteMetadata.url}${url}` : siteMetadata.url;
},
ogType: data => data.ogType || 'website',
});
// Trims whitespace and pipes from the start and end of a string. Useful for CEM types, which can be pipe-delimited.
// With Prettier 3, this means a leading pipe will exist be present when the line wraps.

View File

@@ -3,7 +3,17 @@
<meta name="description" content="{{ description }}">
{% if noindex or unlisted %}<meta name="robots" content="noindex">{% endif %}
<title>{{ title }}</title>
<title>{{ title }} | {{ siteMetadata.name }}</title>
{# Skip OG tags for unlisted/noindex pages to prevent social sharing #}
{% if not (noindex or unlisted) %}
<meta property="og:type" content="{{ ogType }}" />
<meta property="og:url" content="{{ ogUrl }}" />
<meta property="og:title" content="{{ ogTitle }} | {{ siteMetadata.name }}" />
<meta property="og:description" content="{{ ogDescription }}" />
<meta property="og:image" content="{{ ogImage }}" />
<meta property="og:site_name" content="{{ siteMetadata.name }}" />
{% endif %}
{# Dark mode #}
<script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 235 KiB