-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.md.tmpl
273 lines (204 loc) · 10.7 KB
/
README.md.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.rawgit.com/shepherdjerred/astro-opengraph-images/main/assets/logo-dark.png">
<source media="(prefers-color-scheme: light)" srcset="https://cdn.rawgit.com/shepherdjerred/astro-opengraph-images/main/assets/logo-light.png">
<img alt="project logo" src="https://cdn.rawgit.com/shepherdjerred/astro-opengraph-images/main/assets/logo-light.png" height=75>
</picture>
[![astro-opengraph-images](https://img.shields.io/npm/v/astro-opengraph-images.svg)](https://www.npmjs.com/package/astro-opengraph-images)
Generate Open Graph images for your Astro site.
This project is actively maintained. If you have a feature request or need help, please [create an issue](https://github.com/shepherdjerred/astro-opengraph-images/issues/new).
</div>
## What is Open Graph?
[Open Graph](https://ogp.me/) is a protocol created by Facebook. It allows pages on your site to be richly embedded into other sites and applications.
You've probably seen this in action when posting a link on Facebook, Twitter, Slack, iMessage, or Discord. Links posted in supported applications will display the Open Graph metadata which often includes an image. This library will generate those images for you.
## Features
> [!WARNING]
> This integration has only been tested with statically rendered sites. It is untested with server-side rendering.
- Written in TypeScript
- Generate Open Graph images for every page on your site.
- Use a preset renderer to get started quickly.
- Images are fully customizable using [Satori](https://github.com/vercel/satori).
- Use React/JSX + Tailwind syntax or vanilla JavaScript to define your own custom images.
- Supports both static pages and Astro content collections.
- Pages can be written in Markdown, MDX, HTML, or any other format.
## Quick Start
To better illustrate these steps, I've created a [video](https://www.loom.com/share/a66a65be8a4e48ec8612b78489db590d?sid=3e9f7cd6-68ec-49da-b942-75eb4608cb5e) following them to help others follow along.
1. Add this integration to your Astro config:
- Option 1: use the `astro` command:
```bash
npx astro add astro-opengraph-images
```
- Option 2: install the package and add the integration to your Astro config:
```bash
npm i astro-opengraph-images
```
```diff
+import opengraphImages from "astro-opengraph-images";
export default defineConfig({
integrations: [
+ opengraphImages()
],
});
```
1. Install React. React is used by the presets, and can be used to easily author custom images. Note that React is only used for generating the images and will not be shipped to clients.
```bash
npm i -D react
```
1. Install the fonts you want to use. Fonts must be explicitly declared to be used for images. System fonts are _not_ available. For this quick start guide, we'll install the [Roboto](https://fontsource.org/fonts/roboto) font:
```bash
npm i @fontsource/roboto
```
You can find more fonts on [Fontsource](https://fontsource.org/), or you can use any font file that you have. See [Satori's font documentation](https://github.com/vercel/satori?tab=readme-ov-file#fonts) for more information.
1. Configure the integration in your Astro config:
```diff
-import opengraphImages from "astro-opengraph-images";
+import opengraphImages, { presets } from "astro-opengraph-images";
export default defineConfig({
integrations: [
- opengraphImages()
+ opengraphImages({
+ options: {
+ fonts: [
+ {
+ name: "Roboto",
+ weight: 400,
+ style: "normal",
+ data: fs.readFileSync("node_modules/@fontsource/roboto/files/roboto-latin-400-normal.woff"),
+ },
+ ],
+ },
+ render: presets.blackAndWhite,
+ }),
],
});
```
1. Set the [`site`](https://docs.astro.build/en/reference/configuration-reference/#site) property in your Astro config:
Open Graph requires URLs to be absolute, including the domain your site is hosted at. This integration uses the site defined in your Astro config to create the correct URLs for Open Graph which is `site` must be defined.
```diff
export default defineConfig({
+ site: "https://<your site>.com",
integrations: [
opengraphImages({
options: {
fonts: [
{
name: "Roboto",
weight: 400,
style: "normal",
data: fs.readFileSync("node_modules/@fontsource/roboto/files/roboto-latin-400-normal.woff"),
},
],
},
render: presets.blackAndWhite,
}),
],
});
```
1. Update your main Astro layout with the appropriate `meta` tags. The [Open Graph site](https://ogp.me/) has more information possible tags.
The following `meta` tags must be defined:
* `og:title`
* This field may be used when generating images.
* `og:type`
* See the [Open Graph documentation](https://ogp.me/#types) for valid values.
* `og:image`
* Set this to the return value of `getImagePath` (example shown below).
* If the value of `og:image` does not match what this integration expects then your site will fail to build. This will ensure your site is correctly configured to display Open Graph images.
* `og:description`
* Optional. This field may be used when generating images.
Your site will fail to build if the tags above are not set.
* Option 1: Use the [`astro-seo`](https://github.com/jonasmerlin/astro-seo) package:
Install the `astro-seo` package:
```bash
npm i astro-seo
```
Update your Astro layout to use the `SEO` component:
```diff
---
+import { SEO } from "astro-seo";
+import { getImagePath } from "astro-opengraph-images";
interface Props {
title: string;
}
const { title } = Astro.props;
+const { url, site } = Astro;
+const openGraphImageUrl = getImagePath({ url, site });
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
+ <SEO
+ openGraph={
+ {
+ basic: {
+ title: title,
+ type: "website",
+ image: openGraphImageUrl,
+ url: url,
+ },
+ optional: {
+ description: "My page description",
+ },
+ }
+ }
+ />
</head>
<body>
<slot />
</body>
</html>
```
* Option 2: Manually add the `meta` tags to your Astro layout.
1. Build your site. You should see a `.png` file next to each `.html` page in your `dist` folder. Double-check that the `og:image` proprety in your `.html` file matches the path to the `.png` file.
1. Deploy your site. You can verify that your images are correct by:
* Sending a link to your site in an application that supports Open Graph like iMessage, Slack, Discord, etc.
* Visit [opengraph.xyz](http://opengraph.xyz/) and test your site.
## Examples
There are example sites using this integration under [`examples/`](https://github.com/shepherdjerred/astro-opengraph-images/tree/main/examples).
### Sites Using This
If you're using this project, [open a PR](https://github.com/shepherdjerred/astro-opengraph-images/compare) to add your site to this list.
* [sjer.red](https://sjer.red) ([source](https://github.com/shepherdjerred/sjer.red))
## Custom Renderers
You can create your own custom images with a render function. Take a look at how [a preset](https://github.com/shepherdjerred/astro-opengraph-images/blob/main/src/presets/blackAndWhite.tsx) works.
Renderers have access to the page's DOM using [jsdom](https://github.com/jsdom/jsdom). You can use this to render your Open Graph image using any of the content from the associated HTML page. An example of this is shown in the [custom property preset](https://github.com/shepherdjerred/astro-opengraph-images/blob/main/src/presets/customProperty.tsx) which shows a preview of the page's body text in the Open Graph image.
This library uses [Satori](https://github.com/vercel/satori) to convert React components to SVG. The SVG is then converted to a PNG using [resvg-js](https://github.com/yisibl/resvg-js).
> [!TIP]
> Satori supports [a subset of CSS](https://github.com/vercel/satori?tab=readme-ov-file#css). Be sure to familiarize yourself with its limitations.
>
> You can use the [Satori playground](https://og-playground.vercel.app/) to work on your images.
>
> You can use Tailwind syntax with [tw-to-css](https://github.com/vinicoder/tw-to-css). An example is the [Tailwind preset](https://github.com/shepherdjerred/astro-opengraph-images/blob/main/src/presets/tailwind.tsx). You'll need to install this package yourself.
## Presets
Presets are located in [`src/presets/`](https://github.com/shepherdjerred/astro-opengraph-images/tree/main/src/presets). [Open a pull request](https://github.com/shepherdjerred/astro-opengraph-images/compare) to contribute a preset you've created.
Note: some presets use the [`tw-to-css`](https://github.com/vinicoder/tw-to-css) library. You'll need to install this dependency separately when using one of these presets. You'll see an error if the library is not already installed.
```bash
npm i tw-to-css
```
{{ range $preset := (ds "presets") -}}
### `{{ strings.ReplaceAll ".png" "" $preset }}`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";
export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.{{ strings.ReplaceAll ".png" "" $preset }},
}),
],
});
```
![](assets/presets/{{ $preset }})
{{ end }}
## Alternatives
Here are some similar libraries using Satori and Astro. I haven't done a feature comparison.
- https://github.com/florian-lefebvre/satori-astro (This library looks excellent)
- https://github.com/delucis/astro-og-canvas (Doesn't allow arbitrary layouts)
- https://github.com/thewebforge/astro-og-images (Only allows you to choose from a list of templates)
- https://github.com/tomaskebrle/astro-og-image (Seems limited)
- https://github.com/cijiugechu/astro-satori (Possibly dead, hasn't been updated in a year)
- https://github.com/kevinzunigacuellar/astro-satori (Possibly dead, hasn't been updated in a year)
- https://github.com/rumaan/astro-vercel-og (Possibly dead, hasn't been updated in a year)