npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@resoc/eleventy-plugin-social-image

v0.0.4

Published

Generate social images at runtime

Downloads

2

Readme

Resoc Social Image plugin for 11ty

Create automated social images for your 11ty website.

At the moment, this plugin works only for 11ty sites deployed to Netlify.

How it works:

Create an image template

Create a template:

cd my-11ty-project
npx itdk init resoc-templates/default -m title-description

This command generates a template files in resoc-templates/default and launch a viewer in a browser. The template expects two parameters: a title and a description. We can change them or add new ones.

To learn more, visit the Image Template Dev Kit.

Install the plugins

We are going to install two plugins:

  • @resoc/eleventy-plugin-social-image (this plugin)
  • @resoc/netlify-plugin-social-image, which will setup a Netlify function, in charge of generating the social images

Install the packages:

npm install --save-dev @resoc/eleventy-plugin-social-image @resoc/netlify-plugin-social-image

In .eleventy.js, add the plugin:

const pluginResoc = require("@resoc/eleventy-plugin-social-image");

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(pluginResoc, {
    // The directory of the Resoc templates
    templatesDir: 'resoc-templates',

    // The path when social images will be served, eg. /social-images/homepage.jpg
    openGraphBasePath: '/social-images',

    // A file which maps pages to templates and parameters
    slugToImageDataMappingFile: 'resoc-image-data.json',

    // Ask the plugin to configure netlify.toml accordingly
    patchNetlifyToml: true
  });
};

The plugin always makes sure the Netlify plugin is correctly configured in netlify.toml. When is it not:

  • patchNetlifyToml set to false (default): the plugin fails and explains what to do to fix the error.
  • patchNetlifyToml set to true: the plugin updates netlify.toml. When it happens, comments and formatting are lost. If your file is simple, this might be perfectly fine. If your file is complex and with comments, you don't want the plugin to touch it.

If you don't use patchNetlifyToml, in netlify.toml, declare the Netlify build plugin:

[[plugins]]
package = "@resoc/netlify-plugin-social-image"
  [plugins.inputs]
  templates_dir = "resoc-templates"
  open_graph_base_path = "/social-images"
  slug_to_image_data_mapping_file = "resoc-image-data.json"

The various inputs (templates_dir, etc.) must match what your declared in .eleventy.js as parameters or the 11ty plugin. If we don't do so, the 11ty plugin detects it and explains what to do.

Invoke the social images

Suppose we have a master layout in _includes/layouts/base.njk.

Near the top of the file, declare a variable socialImage:

{% set socialImage %}
{%- resoc
      template = "default",
      slug = (title or metadata.title) | slug,
      values = {
        title: title or metadata.title,
        description: description or metadata.description
      }
-%}
{% endset %}

The template is default. Because we set templatesDir to resoc-templates earlier as a plugin parameter, it means a template should exist in resoc-templates/default. This is the case, since we called npx itdk init resoc-templates/default.

The slug must be unique. It can be a slugified version of the page title, an identifier, etc.

The values will be fed as the template parameters.

Still in _includes/layouts/base.njk, head section, declare the Open Graph image:

<meta name="og:image" content="{{ socialImage }}"/>

Deploy

At build time, a mapping file is created (which name is set via the plugin parameter slugToImageDataMappingFile). It is filled with the information provided to the resoc short code.

When the project is deployed to Netlify, this file is generated. The Netlify plugin creates a Netlify function which uses this file to generate the images.