4.1.0 • Published 3 years ago

@manustays/eleventy-plugin-generate-social-images v4.1.0

Weekly downloads
4
License
ISC
Repository
github
Last release
3 years ago

Eleventy Plugin: Generate Social Images (using SVG)

Dynamically generate social media images for your Eleventy blog pages. Unlike other similar plugins, this one uses SVG & does not depend on Puppeteer!

GitHub issues npm (scoped) About Abhishek Twitter Follow


  1. Introduction
  2. Why another social-image generator?
  3. How does it work?
  4. Installation and Usage
  5. Config Options
  6. Custom Fonts
  7. Todo
  8. Credits

Introduction

This plugin provides an 11ty ShortCode that can be used to automatically generate social images in your Eleventy website or blog.

For example:

{% GenerateSocialImage "Eleventy plugin for generating social images (using SVG)" %}

will generate the following social image (website-name and author-image are set during configuration): npm.io

The social image is first created as SVG and then converted to PNG using Sharp.

Why another social-image generator?

There is already a very good plugin eleventy-plugin-social-images by Stephanie Eckles who has written a very good article about her approach. The plugin is very customizable and can serve most people very well.

I created a new plugin because the above mentioned plugin...

  • uses Puppeteer to generate the image from a webpage.
    • I faced some issues running Puppeteer on WSL2, so decided to get rid of the dependency.
  • uses a separate build process to generate the images.
    • While it is totally fine (even better, as it can be used with any other SSG), I wanted the workflow within the Eleventy build process, i.e, by using an Eleventy ShortCode.

How does it work?

  • Generates the image using SVG and then converts it into PNG using Sharp.
  • Custom logic to wrap the title line in SVG (as Sharp does not support SVG foreignObject).
  • Adds an author/promo image using Sharp composite (as Sharp does not support external image in SVG).

Installation and Usage

STEP 1: Install the package:

npm install @manustays/eleventy-plugin-generate-social-images

STEP 2: Include it in your .eleventy.js config file:

const generateSocialImages = require("@manustays/eleventy-plugin-generate-social-images");

module.exports = (eleventyConfig) => {
  eleventyConfig.addPlugin(generateSocialImages, {
    promoImage: "./src/img/my_profile_pic.jpg",
    outputDir: "./_site/img/preview",
    urlPath: "/img/preview",
	siteName: "abhi.page/",
	titleColor: "#fedb8b"
  });
};

Step 3: Use in your template

For example, in your base.njk template file, use it in the <head> for generating social image meta tags:

<meta property="og:image" content="{% GenerateSocialImage title %}" />
<meta name="twitter:image" content="{% GenerateSocialImage title %}" />

Note: For a complete implementation example, checkout my website on Github.

Config Options

OptionTypeDefaultDescription
promoImagestringPath to a promo Image (ideally, circular) that will be embedded in the social-images
outputDirstring"./_site/img/preview"Project-relative path to the output directory where images will be generated
urlPathstring"/img/preview"A path-prefix-esque directory for the <img src> attribute. e.g. /img/ for <img src="/img/MY_IMAGE.jpeg">
siteNamestringThe website name to show on the social-image
titleColorstring"white"The color of the page-title
bgColorstringOptional background color. Otherwise, shows the gradient pattern
terminalBgColorstring"#404040"Background color of the terminal window design
hideTerminalbooleanfalseIf true, hides the terminal window design behind the title
customSVGstringCustom SVG code to be added to the image. Use this to add your own design or text anywhere on the image
customFontFilenamestringFilename of custom local font used for title (see Custom Fonts)
lineBreakAtnumber35Maximum row length for wrapping the title. Required because SVG does not have auto-wrapping text. Should depends on the font used

Custom Fonts

The Sharp library uses librsvg that uses fontconfig to load external fonts. Therefore, the following steps are required: 1. Download your font file in project sub-folder. Eg: ./fonts/sans.ttf 2. Create a file fonts.conf with the following content:

	<?xml version="1.0"?>
	<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
	<fontconfig>
		<dir prefix="default">fonts</dir>
	</fontconfig>
	```
3. Setup the following environment variable on your build server (eg: Netlify):
```bash
FONTCONFIG_PATH=.

TODO

  • Cache result to avoid regenerating same image
  • Better text-wrap logic for the page-title in SVG
  • Custom SVG
  • Custom font
  • More customization options!

Credits

4.1.0

3 years ago

4.0.0

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.0.0

3 years ago