Skip to content

Commit 500c8cb

Browse files
authored
Don't crash ogimage generation on RTL text (#3341)
1 parent 6859f7d commit 500c8cb

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

.changeset/pink-windows-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Don't crash ogimage generation on RTL text, as a workaround until we can support it.

bun.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"assert-never": "^1.2.1",
8686
"bun-types": "^1.1.20",
8787
"classnames": "^2.5.1",
88+
"direction": "^2.0.1",
8889
"event-iterator": "^2.0.0",
8990
"framer-motion": "^10.16.14",
9091
"image-size": "^2.0.2",
@@ -1708,6 +1709,8 @@
17081709

17091710
"dir-glob": ["dir-glob@3.0.1", "", { "dependencies": { "path-type": "^4.0.0" } }, "sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA=="],
17101711

1712+
"direction": ["direction@2.0.1", "", { "bin": { "direction": "cli.js" } }, "sha512-9S6m9Sukh1cZNknO1CWAr2QAWsbKLafQiyM5gZ7VgXHeuaoUwffKN4q6NC4A/Mf9iiPlOXQEKW/Mv/mh9/3YFA=="],
1713+
17111714
"dlv": ["dlv@1.1.3", "", {}, "sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA=="],
17121715

17131716
"dom-serializer": ["dom-serializer@2.0.0", "", { "dependencies": { "domelementtype": "^2.3.0", "domhandler": "^5.0.2", "entities": "^4.2.0" } }, "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg=="],

packages/gitbook/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
"zod": "^3.24.2",
8383
"zod-to-json-schema": "^3.24.5",
8484
"zustand": "^5.0.3",
85-
"image-size": "^2.0.2"
85+
"image-size": "^2.0.2",
86+
"direction": "^2.0.1"
8687
},
8788
"devDependencies": {
8889
"@argos-ci/playwright": "^5.0.3",

packages/gitbook/src/routes/ogimage.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CustomizationDefaultFont, CustomizationHeaderPreset } from '@gitbook/api';
22
import { colorContrast } from '@gitbook/colors';
33
import { type FontWeight, getDefaultFont } from '@gitbook/fonts';
4+
import { direction } from 'direction';
45
import { imageSize } from 'image-size';
56
import { redirect } from 'next/navigation';
67
import { ImageResponse } from 'next/og';
@@ -218,7 +219,7 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
218219
) : (
219220
<div tw="flex">
220221
{favicon}
221-
<h3 tw="text-4xl my-0 font-bold">{site.title}</h3>
222+
<h3 tw="text-4xl my-0 font-bold">{transformText(site.title)}</h3>
222223
</div>
223224
)}
224225

@@ -227,10 +228,12 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
227228
<h1
228229
tw={`text-8xl my-0 tracking-tight leading-none text-left text-[${colors.title}] font-bold`}
229230
>
230-
{pageTitle}
231+
{transformText(pageTitle)}
231232
</h1>
232233
{pageDescription ? (
233-
<h2 tw="text-4xl mb-0 mt-8 w-[75%] font-normal">{pageDescription}</h2>
234+
<h2 tw="text-4xl mb-0 mt-8 w-[75%] font-normal">
235+
{transformText(pageDescription)}
236+
</h2>
234237
) : null}
235238
</div>
236239
</div>,
@@ -377,3 +380,17 @@ async function fetchImage(url: string) {
377380
return null;
378381
}
379382
}
383+
384+
/**
385+
* @vercel/og doesn't support RTL text, so we need to transform with a HACK for now.
386+
* We can remove it once support has been added.
387+
* https://github.com/vercel/satori/issues/74
388+
*/
389+
function transformText(text: string) {
390+
const dir = direction(text);
391+
if (dir !== 'rtl') {
392+
return text;
393+
}
394+
395+
return '';
396+
}

0 commit comments

Comments
 (0)