-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Dynamic OG images for docs pages #8527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+441
−5
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93a4c9a
Add dynamic OG images for docs pages
aurorascharff 533541a
Match OG card design to existing static images
aurorascharff 5e41056
Generate OG images statically at build time
aurorascharff 0145ae8
Clean up orphaned lockfile entry
aurorascharff e457db2
Show section label on OG cards and guard pages without a card
aurorascharff 2aad56c
Center title and enlarge section label on OG cards
aurorascharff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,3 +46,6 @@ public/rss.xml | |
|
|
||
| # worktrees | ||
| .worktrees/ | ||
|
|
||
| # Generated OG images (scripts/generateOgImages.mjs) | ||
| public/images/og/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| // Generates a static OG image for every content page, so social | ||
| // cards show the page title without any runtime image generation. | ||
| // Run after `next build` (see the `build` script in package.json). | ||
|
|
||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import satori from 'satori'; | ||
| import {Resvg} from '@resvg/resvg-js'; | ||
| import matter from 'gray-matter'; | ||
|
|
||
| const ROOT = process.cwd(); | ||
| const CONTENT_DIR = path.join(ROOT, 'src', 'content'); | ||
| const OUT_DIR = path.join(ROOT, 'public', 'images', 'og'); | ||
| const CONCURRENCY = 8; | ||
|
|
||
| const bold = fs.readFileSync( | ||
| path.join(ROOT, 'public', 'fonts', 'Optimistic_Display_W_Bd.ttf') | ||
| ); | ||
| const medium = fs.readFileSync( | ||
| path.join(ROOT, 'public', 'fonts', 'Optimistic_Display_W_Md.ttf') | ||
| ); | ||
|
|
||
| function el(type, style, children) { | ||
| return {type, props: {style, children}}; | ||
| } | ||
|
|
||
| function card(title, pagePath) { | ||
| const footer = ('react.dev' + pagePath).toUpperCase(); | ||
| const logo = { | ||
| type: 'svg', | ||
| props: { | ||
| width: 80, | ||
| height: 72, | ||
| viewBox: '-10.5 -9.45 21 18.9', | ||
| fill: 'none', | ||
| children: [ | ||
| {type: 'circle', props: {cx: 0, cy: 0, r: 2, fill: '#58c4dc'}}, | ||
| { | ||
| type: 'g', | ||
| props: { | ||
| stroke: '#58c4dc', | ||
| strokeWidth: 1, | ||
| fill: 'none', | ||
| children: [ | ||
| {type: 'ellipse', props: {rx: 10, ry: 4.5}}, | ||
| { | ||
| type: 'ellipse', | ||
| props: {rx: 10, ry: 4.5, transform: 'rotate(60)'}, | ||
| }, | ||
| { | ||
| type: 'ellipse', | ||
| props: {rx: 10, ry: 4.5, transform: 'rotate(120)'}, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| return el( | ||
| 'div', | ||
| { | ||
| width: '100%', | ||
| height: '100%', | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| padding: '72px 80px', | ||
| backgroundColor: '#23272f', | ||
| backgroundImage: | ||
| 'radial-gradient(circle at 25% 30%, #343a46 0%, #23272f 55%)', | ||
| }, | ||
| [ | ||
| el('div', {display: 'flex', alignItems: 'center', gap: '20px'}, [ | ||
| logo, | ||
| el( | ||
| 'div', | ||
| { | ||
| fontSize: 48, | ||
| fontFamily: 'Optimistic Display Bold', | ||
| color: '#f6f7f9', | ||
| }, | ||
| 'React' | ||
| ), | ||
| ]), | ||
| el( | ||
| 'div', | ||
| { | ||
| marginTop: 'auto', | ||
| marginBottom: 'auto', | ||
| fontSize: title.length > 24 ? 72 : 96, | ||
| fontFamily: 'Optimistic Display Bold', | ||
| color: '#f6f7f9', | ||
| lineHeight: 1.1, | ||
| wordBreak: 'break-word', | ||
| }, | ||
| title | ||
| ), | ||
| el( | ||
| 'div', | ||
| { | ||
| fontSize: 28, | ||
| fontFamily: 'Optimistic Display Medium', | ||
| color: '#99a1b3', | ||
| letterSpacing: '0.08em', | ||
| }, | ||
| footer | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| async function renderCard(title, pagePath) { | ||
| const svg = await satori(card(title, pagePath), { | ||
| width: 1200, | ||
| height: 630, | ||
| fonts: [ | ||
| { | ||
| name: 'Optimistic Display Bold', | ||
| data: bold, | ||
| weight: 700, | ||
| style: 'normal', | ||
| }, | ||
| { | ||
| name: 'Optimistic Display Medium', | ||
| data: medium, | ||
| weight: 500, | ||
| style: 'normal', | ||
| }, | ||
| ], | ||
| }); | ||
| return new Resvg(svg, {fitTo: {mode: 'width', value: 1200}}).render().asPng(); | ||
| } | ||
|
|
||
| function collectSidebarTitles() { | ||
| const titles = new Map(); | ||
| for (const name of fs.readdirSync(path.join(ROOT, 'src'))) { | ||
| if (!/^sidebar.*\.json$/.test(name)) continue; | ||
| const walk = (node) => { | ||
| if (node.path && node.title) { | ||
| titles.set(node.path, node.title); | ||
| } | ||
| for (const child of node.routes ?? []) walk(child); | ||
| }; | ||
| walk(JSON.parse(fs.readFileSync(path.join(ROOT, 'src', name), 'utf8'))); | ||
| } | ||
| return titles; | ||
| } | ||
|
|
||
| const sidebarTitles = collectSidebarTitles(); | ||
|
|
||
| function collectPages(dir, out) { | ||
| for (const entry of fs.readdirSync(dir, {withFileTypes: true})) { | ||
| const full = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| collectPages(full, out); | ||
| } else if (entry.name.endsWith('.md')) { | ||
| const rel = path.relative(CONTENT_DIR, full).replace(/\.md$/, ''); | ||
| const segments = rel.split(path.sep); | ||
| if (segments[segments.length - 1] === 'index') { | ||
| segments.pop(); | ||
| } | ||
| const pagePath = '/' + segments.join('/'); | ||
| const {data} = matter(fs.readFileSync(full, 'utf8')); | ||
| const title = data.title ?? sidebarTitles.get(pagePath); | ||
| if (title) { | ||
| out.push({title: String(title), pagePath}); | ||
| } | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| export function ogImageFileName(pagePath) { | ||
| return pagePath.replace(/^\//, '').replace(/\//g, '-') + '.png'; | ||
| } | ||
|
|
||
| async function main() { | ||
| const pages = collectPages(CONTENT_DIR, []); | ||
| fs.mkdirSync(OUT_DIR, {recursive: true}); | ||
| let done = 0; | ||
| const queue = [...pages]; | ||
| async function worker() { | ||
| for (;;) { | ||
| const page = queue.shift(); | ||
| if (!page) return; | ||
| const png = await renderCard(page.title, page.pagePath); | ||
| fs.writeFileSync(path.join(OUT_DIR, ogImageFileName(page.pagePath)), png); | ||
| done++; | ||
| if (done % 100 === 0) { | ||
| console.log(`og-images: ${done}/${pages.length}`); | ||
| } | ||
| } | ||
| } | ||
| await Promise.all(Array.from({length: CONCURRENCY}, worker)); | ||
| console.log(`og-images: generated ${done} images in public/images/og`); | ||
| } | ||
|
|
||
| main().catch((error) => { | ||
| console.error(error); | ||
| process.exit(1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.