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

@sahithyan/og

v1.0.0

Published

A small library for generating static social card images (also called Open Graph images) with Node.

Downloads

178

Readme

@sahithyan/og

@sahithyan/og is a small library for generating static social card images (also called Open Graph images) locally with the Node runtime.

Reason behind it

Vercel recently announced @vercel/og, which is a library for doing the same thing but on the Edge, instead. You can learn more about it here.

While it's great to have dynamic social card images, not everyone will want them all the time; at least I won't. Sometimes we just want to run it on our machines locally. That's why it's built.

Overview

@sahithyan/og is built on top of Satori and Resvg. These are the exact same technologies @vercel/og uses.

/** @jsx j */
import og, { j } from "@sahithyan/og";
import { writeFile } from "fs";

og(
	<div>
		<h1>Hi</h1>
		<div>This is amazing!</div>
	</div>,
	{
		width: 600,
		height: 400,
	}
).then((pngData) => {
	// save as a png
	writeFile("./test.png", pngData);
});

NOTE: This library is using a forked version of Satori. And it is published under @sahithyan/satori on npm. Whenever a new version of Satori is released, the changes will be merged to the fork and @sahithyan/satori will be released (by me, manually). To make this easy to manage, I am using the exact version number of satori, for @sahithyan/satori.

Documentation

import og, { j } from "@sahithyan/og";

These are the only exports: og (default export) and j.

og()

The og function is a wrapper for Satori and Resvg. It passes the arguments to Satori (with a few changes) to get the SVG, converts the SVG to PNG using Resvg, and returns the PNG data as Promise<Buffer>.

You can then use the fs.writeFile to save the file.

The og() function's parameters are quite the same with Satori's. You can check its documentation here. You can see the differences between them below.

declare function og(
	element: ReactNode,
	/**
	 * CustomSatoriOptions is identical to SatoriOptions, except:
	 *
	 * - `fonts` is not required. Inter will be used when no fonts are provided.
	 */
	options: CustomSatoriOptions
): Promise<Buffer>;

And here is an example:

og(
	// the input element
	<div style={{ color: "black" }}>Hello, World!</div>,
	// satori options
	{
		width: 600,
		height: 400,
	}
).then((pngData) => {
	fs.writeFile("./test.png", pngData);
});

Here, you don't need to proivde a default font. The library will use Inter font, if none of them are provided.

j()

The j function is a JSX Pragma function. I recommended using this pragma when transpiling JSX to be run with @sahithyan/og (or with Satori, in general).

To use as j as the pragma, add this line at the top of the file. You can declare it in your babel configuration as well, but this is recommended.

/** @jsx j */

j is included as Satori didn't work with Preact's h when I tried. If it does work for you, then it's totally fine to avoid using j.