2.1.3 • Published 11 days ago

eleventy-plugin-og-image v2.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

Eleventy Plugin OG Image npm

This plugin helps to create Open Graph images in Eleventy using a template language of your choice^1 and CSS^3 via satori. No headless browser will be harmed 😉.

Usage

Install the package:

npm install eleventy-plugin-og-image --save-dev

Add the plugin to your .eleventy.js:

const EleventyPluginOgImage = require('eleventy-plugin-og-image');

module.exports = (eleventyConfig) => {
  eleventyConfig.addPlugin(EleventyPluginOgImage, {
    satoriOptions: {
      fonts: [
        {
          name: 'Inter',
          data: fs.readFileSync('../path/to/font-file/inter.woff'),
          weight: 700,
          style: 'normal',
        },
      ],
    },
  });
};

Create an OG-image-template, using the supported HTML elements^2 and CSS properties^3. CSS in <style> tags will be inlined, remote images fetched. This is an example og-image.og.njk:

<style>
    .root {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        background: linear-gradient(135deg, #ef629f, #eecda3);
    }

    .title {
        color: white;
        font-size: 80px;
        margin: auto 0;
    }
</style>

<div class="root">
    <h1 class="title">{{ title }}</h1>
</div>

Call the ogImage shortcode inside the <head> in a template. The first argument is the filePath of the OG-image-template (required), second argument is for data (optional). Usage example in Nunjucks, e.g. example-page.njk:

{% ogImage "./og-image.og.njk", { title: "Hello World!" } %}

Result

Generated OG image _site/og-images/s0m3h4sh.png:

Generated OG image

HTML output generated by the shortcode in _site/example-page/index.html (can be modified via the generateHTML option):

<meta property="og:image" content="/og-images/s0m3h4sh.png" />

For applied usage see the example.

Note The template language of the page and OG-image-template can be mixed and matched.^1

Configuration

The following options can be passed when adding the plugin:

PropertyTypeDefault
inputFileGlobglob**/*.og.*This must match the OG-image-templates to prevent HTML compilation.
getOutputFileSlugfunctionSee sourceGeneration of the output file slug. Return must be url safe and exclude the file extension.
outputFileExtensionsharp output file formatspng
outputDirstring_site/og-images/Directory into which OG images will be emitted. Change urlPath accordingly.
urlPathstring/og-images/URL-prefix which will be used in returned meta-tags. Change outputDir accordingly.
generateHTMLfunctionSee sourceChange the rendered HTML in pages.
satoriOptionssatori options{ width: 1200, height: 630, fonts: [] }If an OG-image-template contains text, it's required to load a font (example).
sharpOptionssharp output optionsundefinedOptions must be corresponding to chosen outputFileExtension.

Development Mode

During development the OG image file name is the url slug of the page it's generated from. In production builds, a hash of the content will be used. You'll have to handle this yourself, if you pass a custom getOutputFileSlug.

Advanced Usage

Custom Shortcode

If you would like to build your own shortcode, you can directly use the renderOgImage function.

const { renderOgImage } = require('eleventy-plugin-og-image/render');

const { html, svg, pngBuffer } = await renderOgImage({ inputPath, data, satoriOptions, templateConfig });

Capture Output URL

If you don't want to directly generate HTML with the shortcode, you can modify the generateHTML option to directly return the outputUrl:

eleventyConfig.addPlugin(EleventyPluginOgImage, {
  generateHTML: (outputUrl) => outputUrl,
});

Now you can capture the outputUrl in your page, e.g. in Nunjucks:

{% setAsync "ogOutputUrl" -%}
    {% ogImage "./og-image.og.njk", { title: "Hello World!" } %}
{%- endsetAsync %}

And use it anywhere below with {{ ogOutputUrl }}.

Acknowledgements & Attributions

This plugin is deeply inspired by @vercel/og.

Furthermore, it would not be possible without:

^1: Handlebars doesn't support async shortcodes and therefore can't use the ogImage shortcode. Nevertheless, an OG-image-template can use Handlebars (.og.hbs). ^2: Only a subset of HTML elements is supported by satori. ^3: Only a subset of CSS properties are supported by yoga-layout, which is used by satori.

4.0.0-beta.6

11 days ago

4.0.0-beta.5

12 days ago

4.0.0-beta.4

25 days ago

4.0.0-beta.3

1 month ago

4.0.0-beta.2

1 month ago

4.0.0-beta.1

1 month ago

2.1.2

2 months ago

2.1.3

2 months ago

3.2.0-beta.5

2 months ago

3.2.0-beta.4

4 months ago

3.2.0-beta.3

4 months ago

3.2.0-beta.2

5 months ago

3.2.0-beta.1

5 months ago

3.1.0

6 months ago

3.0.0

6 months ago

2.1.1

8 months ago

2.1.0

8 months ago

1.6.2

12 months ago

2.0.1

11 months ago

2.0.0

11 months ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago