Skip to content

Commit 028cdd1

Browse files
committed
feat(og-image): ✨ Enhance Open Graph image generation with new layout and branding features
- Refactored `DefaultOgImageTemplate` to improve layout handling and branding display. - Introduced `OgGlow` and `OgBrand` components for better visual representation. - Removed unused `icon` and `image` properties from `OgImageTemplateProps`. - Updated related routes and types to reflect changes in Open Graph image handling. - Improved overall styling and text presentation for better user experience.
1 parent 1d1f62e commit 028cdd1

27 files changed

Lines changed: 276 additions & 673 deletions

File tree

‎apps/api/src/vitnode.api.config.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ export const vitNodeApiConfig = buildApiConfig({
188188
title: "VitNode API",
189189
shortTitle: "VitNode",
190190
},
191-
// The VitNode wordmark on every social card, drawn from SVG.
192191
ogImage: {
193192
logo: { svg: VITNODE_LOGO_SVG, wordmark: true },
194193
},

‎apps/web/content/docs/content-engine/seo.mdx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ delivery: {
7070
| `noIndexField` | boolean, not localized | `robots` tag and sitemap, see [noindex](#hide-a-record-from-search-engines) |
7171
| `openGraph.titleField` | text | Open Graph title, falls back to the page title |
7272
| `openGraph.descriptionField` | text, textarea, richText | Open Graph description, falls back to the page description |
73-
| `openGraph.imageField` | file, single, not localized | The cover behind the card, see [Social images](#social-images) |
73+
| `openGraph.imageField` | file, single, not localized | The cover used as `og:image` with `generateImage: false`, see [Social images](#social-images) |
7474
| `openGraph.generateImage` | boolean, default `true` | `false` uses the cover itself as `og:image`, see [Social images](#social-images) |
7575

7676
Every field you name must also be in `publicApi.fields`, because the page shows it to anyone. Leave one out and the definition throws at startup with `... which is not in publicApi.fields`. A field inside a group works (`"seo.title"`). A field inside a repeatable does not, since a page has one title, not a list of them.
@@ -82,7 +82,7 @@ delivery: {
8282
seo: {
8383
titleField: "title",
8484
descriptionField: "content",
85-
openGraph: { imageField: "coverImage" },
85+
openGraph: { generateImage: false, imageField: "coverImage" },
8686
},
8787
},
8888
```
@@ -117,8 +117,7 @@ The page's `head` calls `contentDeliveryPageHead` with the resolve route's answe
117117

118118
With `delivery.seo.openGraph` set, a record page gets an `og:image`, the picture link previews show. The API draws it as a 1200 × 630 card at `/delivery/og/{slug}`, next to the other delivery routes: the Open Graph description as a large quote, signed with the title. `openGraph: {}` is enough to get one.
119119

120-
- **A cover behind the card.** `openGraph.imageField` names a shared, single `field.file()` in `publicApi.fields`; the blog uses its cover. The card shows it on the right side, fading smoothly into the background, and keeps the quote in its own column so no word sits on the picture. Only an image a reader may open counts: a GIF, JPEG, PNG or WebP that no [attachment rule](/docs/content-engine/access-attachments) restricts. The API reads it from your storage, and a new cover gives the card a new URL.
121-
- **The cover on its own.** `generateImage: false` skips the card and uses the `imageField` image itself as `og:image`, with the file's alt text in the page's language (the record's title when the file has none). A record without a cover then falls back to the site card, drawn with its title and description. The blog plugin works this way.
120+
The card never draws pictures from the record, only text on the site's colours. To share a record's own cover instead, name it in `openGraph.imageField` and set `generateImage: false`: the field is a shared, single `field.file()` in `publicApi.fields`, and the image itself becomes `og:image`, with the file's alt text in the page's language (the record's title when the file has none). Only an image a reader may open counts: a GIF, JPEG, PNG or WebP that no [attachment rule](/docs/content-engine/access-attachments) restricts. A record without a cover then falls back to the site card, drawn with its title and description. The blog plugin works this way.
122121

123122
The resolve route reports the choice under `openGraph.image`:
124123

4.73 KB
Loading

‎apps/web/content/docs/dev/social-images.mdx‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,36 @@ icon: Image
77
import { ImgDocs } from "@/components/fumadocs/img";
88
import cardExample from "./social-images-card.webp";
99

10-
When someone shares a VitNode link on Facebook, LinkedIn, X, Slack or Discord, the preview shows the page's `og:image`. VitNode fills it for you: the API draws a social card, and every page head points at it. The card is headed by the site's logo, with the page's title as the headline, its description underneath, and the page's cover, when it has one, on the right side, fading smoothly into the background beside the text. No headless browser runs, and nothing is fetched from the internet: the fonts ship with VitNode and covers come from your own storage.
10+
When someone shares a VitNode link on Facebook, LinkedIn, X, Slack or Discord, the preview shows the page's `og:image`. VitNode fills it for you: the API draws a social card, and every page head points at it. The card is headed by the site's logo, with the page's title as the headline, its description underneath, and a soft glow of your accent colour lighting up the right side. No headless browser runs, and nothing is fetched from the internet: the fonts ship with VitNode.
1111

1212
<ImgDocs
1313
src={cardExample}
14-
alt="A dark social card with the VitNode logo at the top left, the story's excerpt as a large quote, and the title Hello Content Engine and the domain below it"
14+
alt="A dark social card with the VitNode logo at the top left, the story's excerpt as a large quote, the title Hello Content Engine and the domain below it, and a soft blue glow on the right"
1515
/>
1616

1717
## What each page gets
1818

1919
| Page | `og:image` |
2020
| ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------ |
21-
| A Content Engine record whose type sets `delivery.seo.openGraph` | Its card at `/api/{plugin}/content/{path}/delivery/og/{slug}`, with the [`imageField`](/docs/content-engine/seo#social-images) cover in the background |
22-
| A member profile (`/users/{nameCode}`) | The member's card at `/api/@vitnode/core/og/users/{nameCode}`: name, profile headline and avatar, profile cover in the background |
21+
| A Content Engine record whose type sets `delivery.seo.openGraph` | Its card at `/api/{plugin}/content/{path}/delivery/og/{slug}` |
22+
| A member profile (`/users/{nameCode}`) | The member's card at `/api/@vitnode/core/og/users/{nameCode}`: name, profile headline and avatar |
2323
| Any other page with a title | The site card at `/api/@vitnode/core/og/site`, drawn with the page's own `title` and `description` from the URL |
2424

25-
Give the site card a coloured label and an icon with `openGraph`:
25+
Give the site card a coloured label with `openGraph`:
2626

2727
```ts title="apps/web/src/routes/_docs/docs.$.tsx"
2828
head: ({ loaderData }) =>
2929
pageHead({
3030
title: loaderData?.metaTitle,
3131
description: loaderData?.description,
32-
// [!code ++:4]
32+
// [!code ++:3]
3333
openGraph: {
3434
imageCategory: { title: "Content Engine", color: "#61bd67" },
35-
imageIcon: "M12 2v10M18.4 6.6a9 9 0 1 1-12.77.04",
3635
},
3736
}),
3837
```
3938

40-
- `imageCategory` puts a label above the title, and its `color` (`#rrggbb`) tints the label, the icon and the bar at the bottom. VitNode's docs use it for the section a page lives in.
41-
- `imageIcon` frames an icon in the top-right corner: SVG path data on Lucide's 24 × 24 grid, drawn as a stroke. Anything but plain path data is dropped, so the card never draws markup from the URL.
39+
- `imageCategory` puts a label above the title, and its `color` (`#rrggbb`) tints the label, the glow and the bar at the bottom. VitNode's docs use it for the section a page lives in.
4240
- `imageLayout: "quote"` swaps the headline for a pull quote: the description large, signed with the title.
4341

4442
A page with a title also gets `og:title`, `og:description`, `og:type`, `og:site_name`, `og:url` (its canonical URL), `og:locale` (as `en_US`, even when the locale is just `en`), one `og:locale:alternate` per other language it has `alternates` in, and the X card tags `twitter:card`, `twitter:title`, `twitter:description`, `twitter:image` and `twitter:image:alt`. A layout that only sets `robots` gets none of them.
@@ -191,7 +189,7 @@ export const StoryCard: OgImageTemplate = ({ siteName, theme, title }) => (
191189
);
192190
```
193191

194-
The props are `title`, `description`, `siteName`, `domain`, `locale`, `theme`, `logo` and `kind` (`content`, `profile` or `site`), plus `contentTypeId` and `pluginId` on a content card, `avatar` on a profile card, and `image` (`{ src }`, a `data:` URI) when the record or member has a cover. `logo` is `{ src, width, height, wordmark }`, ready for an `<img>`. A template that puts the logo on a background other than `theme.background` recolors its `currentColor` with `ogImageLogoSrc(logo, "#18181b")` from `@vitnode/api/lib/og-image`.
192+
The props are `title`, `description`, `siteName`, `domain`, `locale`, `theme`, `logo` and `kind` (`content`, `profile` or `site`), plus `contentTypeId` and `pluginId` on a content card and `avatar` on a profile card. `logo` is `{ src, width, height, wordmark }`, ready for an `<img>`. A template that puts the logo on a background other than `theme.background` recolors its `currentColor` with `ogImageLogoSrc(logo, "#18181b")` from `@vitnode/api/lib/og-image`.
195193

196194
A plugin registers templates for its own content types in `buildApiPlugin`:
197195

‎apps/web/src/docs/docs.css‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
}
3636
}
3737

38+
#nd-page-panel > div:has(> #nd-page) {
39+
background-image: radial-gradient(
40+
ellipse 70% 100% at 50% 0%,
41+
color-mix(in oklab, var(--color-fd-primary) 14%, transparent),
42+
color-mix(in oklab, var(--color-fd-primary) 5%, transparent) 50%,
43+
transparent 100%
44+
);
45+
background-repeat: no-repeat;
46+
background-size: 100% --spacing(96);
47+
background-attachment: local;
48+
}
49+
3850
:root:not(.dark) #fd-spacious-layout .prose {
3951
--tw-prose-body: oklch(0.373 0.003 264);
4052
}

‎apps/web/src/docs/icons.server.ts‎

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -58,96 +58,6 @@ export const loadDocsPageTreeIcons = async (
5858
return Object.fromEntries(entries.flat())
5959
}
6060

61-
const numbers = (attributes: Record<string, string>, ...names: string[]) =>
62-
names.map((name) => Number(attributes[name] ?? 0))
63-
64-
const pointsPath = (points: string | undefined, closed: boolean) => {
65-
const coordinates = (points ?? '').trim()
66-
67-
return coordinates ? `M${coordinates}${closed ? 'Z' : ''}` : ''
68-
}
69-
70-
const ellipsePath = (cx: number, cy: number, rx: number, ry: number) =>
71-
`M${cx - rx} ${cy}a${rx} ${ry} 0 1 0 ${rx * 2} 0a${rx} ${ry} 0 1 0 ${-rx * 2} 0`
72-
73-
const rectPath = (attributes: Record<string, string>) => {
74-
const [x, y, width, height] = numbers(attributes, 'x', 'y', 'width', 'height')
75-
const rx = Math.min(Number(attributes.rx ?? attributes.ry ?? 0), width / 2)
76-
const ry = Math.min(Number(attributes.ry ?? attributes.rx ?? 0), height / 2)
77-
78-
if (rx === 0 && ry === 0) return `M${x} ${y}h${width}v${height}h${-width}Z`
79-
80-
return [
81-
`M${x + rx} ${y}`,
82-
`H${x + width - rx}`,
83-
`A${rx} ${ry} 0 0 1 ${x + width} ${y + ry}`,
84-
`V${y + height - ry}`,
85-
`A${rx} ${ry} 0 0 1 ${x + width - rx} ${y + height}`,
86-
`H${x + rx}`,
87-
`A${rx} ${ry} 0 0 1 ${x} ${y + height - ry}`,
88-
`V${y + ry}`,
89-
`A${rx} ${ry} 0 0 1 ${x + rx} ${y}`,
90-
'Z',
91-
].join('')
92-
}
93-
94-
const NUMBER = String.raw`-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?`
95-
const LEADING_RELATIVE_MOVE = new RegExp(
96-
String.raw`^\s*m\s*(${NUMBER})[\s,]*(${NUMBER})[\s,]*`,
97-
'i',
98-
)
99-
100-
const standalonePath = (d: string) => {
101-
const match = LEADING_RELATIVE_MOVE.exec(d)
102-
if (!match || d.trimStart().startsWith('M')) return d
103-
104-
const rest = d.slice(match[0].length)
105-
const implicitLines = rest !== '' && !/^[a-z]/i.test(rest)
106-
107-
return `M${match[1]} ${match[2]}${implicitLines ? 'l' : ''}${rest}`
108-
}
109-
110-
const shapePath = ([tag, attributes]: DocsIconNode[number]) => {
111-
switch (tag) {
112-
case 'circle': {
113-
const [cx, cy, r] = numbers(attributes, 'cx', 'cy', 'r')
114-
115-
return ellipsePath(cx, cy, r, r)
116-
}
117-
case 'ellipse': {
118-
const [cx, cy, rx, ry] = numbers(attributes, 'cx', 'cy', 'rx', 'ry')
119-
120-
return ellipsePath(cx, cy, rx, ry)
121-
}
122-
case 'line': {
123-
const [x1, y1, x2, y2] = numbers(attributes, 'x1', 'y1', 'x2', 'y2')
124-
125-
return `M${x1} ${y1}L${x2} ${y2}`
126-
}
127-
case 'path':
128-
return standalonePath(attributes.d ?? '')
129-
case 'polygon':
130-
return pointsPath(attributes.points, true)
131-
case 'polyline':
132-
return pointsPath(attributes.points, false)
133-
case 'rect':
134-
return rectPath(attributes)
135-
default:
136-
return ''
137-
}
138-
}
139-
140-
export const loadDocsIconPathData = async (
141-
name: string | undefined,
142-
): Promise<string | undefined> => {
143-
const load = name ? iconImports.get(name) : undefined
144-
if (!load) return undefined
145-
146-
const node = toDocsIconNode((await load()).__iconData.node)
147-
148-
return node.map(shapePath).filter(Boolean).join('') || undefined
149-
}
150-
15161
const treePathOf = (nodes: readonly Node[], url: string): Node[] | null => {
15262
for (const node of nodes) {
15363
if (node.type === 'page' && node.url === url) return [node]
@@ -161,19 +71,10 @@ const treePathOf = (nodes: readonly Node[], url: string): Node[] | null => {
16171
return null
16272
}
16373

164-
const iconNameOf = (node: Node) =>
165-
'icon' in node && typeof node.icon === 'string' ? node.icon : undefined
74+
export const docsSectionTitle = (tree: Root, url: string) => {
75+
const [top] = treePathOf(tree.children, url) ?? []
16676

167-
export const docsPagePlacement = (tree: Root, url: string) => {
168-
const path = treePathOf(tree.children, url) ?? []
169-
const [top] = path
170-
const sectionTitle =
171-
top?.type === 'folder' && top.root && typeof top.name === 'string'
172-
? top.name
173-
: undefined
174-
175-
return {
176-
iconName: path.map(iconNameOf).filter(Boolean).at(-1),
177-
sectionTitle,
178-
}
77+
return top?.type === 'folder' && top.root && typeof top.name === 'string'
78+
? top.name
79+
: undefined
17980
}

‎apps/web/src/docs/transport.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { createServerFn } from '@tanstack/react-start'
33
import { getBreadcrumbItems } from 'fumadocs-core/breadcrumb'
44
import { z } from 'zod'
55

6-
import {
7-
docsPagePlacement,
8-
loadDocsIconPathData,
9-
loadDocsPageTreeIcons,
10-
} from './icons.server'
6+
import { docsSectionTitle, loadDocsPageTreeIcons } from './icons.server'
117
import { docsGithubUrl, encodeMarkdownUrl } from './shared'
128
import { source } from './source.server'
139

@@ -18,7 +14,7 @@ const docsSlugSchema = z
1814

1915
export const getDocsPage = createServerFn({ method: 'GET' })
2016
.validator(docsSlugSchema)
21-
.handler(async ({ data: slugs }) => {
17+
.handler(({ data: slugs }) => {
2218
const page = source.getPage(slugs)
2319

2420
// oxlint-disable-next-line typescript/only-throw-error
@@ -30,17 +26,14 @@ export const getDocsPage = createServerFn({ method: 'GET' })
3026
.reverse()
3127
.flatMap((item) => (typeof item.name === 'string' ? [item.name] : []))
3228
const section = page.slugs[0]
33-
const { iconName, sectionTitle } = docsPagePlacement(tree, page.url)
34-
35-
const icon = await loadDocsIconPathData(iconName)
29+
const sectionTitle = docsSectionTitle(tree, page.url)
3630

3731
return {
3832
description: page.data.description,
3933
githubUrl: docsGithubUrl(page.path),
4034
markdownUrl: encodeMarkdownUrl(page.slugs),
4135
metaTitle: [page.data.title, ...ancestors].join(' - '),
4236
path: page.path,
43-
ogIcon: icon ?? null,
4437
section:
4538
section && sectionTitle ? { id: section, title: sectionTitle } : null,
4639
url: page.url,

‎apps/web/src/routes/_docs/docs.$.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export const Route = createFileRoute('/_docs/docs/$')({
4141
openGraph: {
4242
description: loaderData?.description,
4343
imageCategory: docsSectionOgCategory(loaderData?.section),
44-
imageIcon: loaderData?.ogIcon ?? undefined,
4544
title: loaderData?.metaTitle,
4645
type: 'article',
4746
},

‎apps/web/src/vitnode.api.config.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ export const vitNodeApiConfig = buildApiConfig({
115115
metadata: {
116116
title: 'VitNode',
117117
shortTitle: 'VitNode',
118-
// Shown on the site's social card, under the title.
119118
description: 'Build modern community apps with plugins, React and Hono.',
120119
},
121-
// The VitNode wordmark on every social card, drawn from SVG.
122120
ogImage: {
123121
logo: { svg: VITNODE_LOGO_SVG, wordmark: true },
124122
},

‎packages/api/src/content/delivery.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -444,16 +444,6 @@ export const resolveContentDelivery = ({
444444
});
445445
}
446446

447-
if (
448-
seo.openGraph?.generateImage !== undefined &&
449-
typeof seo.openGraph.generateImage !== "boolean"
450-
) {
451-
throw new ContentEngineError(
452-
`delivery.seo.openGraph.generateImage is ${JSON.stringify(seo.openGraph.generateImage)}; it is either true or false.`,
453-
{ contentTypeId: id },
454-
);
455-
}
456-
457447
if (seo.noIndexField !== undefined) {
458448
assertSeoField({
459449
exposed,

0 commit comments

Comments
 (0)