Skip to content

Commit 4e91f7b

Browse files
authored
Sync with react.dev @ 7b6c3ce (#151)
This PR was automatically generated. Merge changes from [react.dev](https://github.com/reactjs/react.dev/commits/main) at 7b6c3ce The following files have conflicts and may need new translations: * [ ] [yarn.lock](/reactjs/react.dev/commits/main/yarn.lock) Please fix the conflicts by pushing new commits to this pull request, either by editing the files directly on GitHub or by checking out this branch. ## DO NOT SQUASH MERGE THIS PULL REQUEST! Doing so will "erase" the commits from main and cause them to show up as conflicts the next time we merge.
2 parents 20324ed + 8060f6e commit 4e91f7b

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@babel/preset-react": "^7.18.6",
5353
"@mdx-js/mdx": "^2.1.3",
5454
"@resvg/resvg-js": "^2.6.2",
55+
"@shuding/opentype.js": "1.4.0-beta.0",
5556
"@types/body-scroll-lock": "^2.6.1",
5657
"@types/classnames": "^2.2.10",
5758
"@types/debounce": "^1.2.1",

scripts/generateOgImages.mjs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import path from 'path';
1414
import satori from 'satori';
1515
import {Resvg} from '@resvg/resvg-js';
1616
import matter from 'gray-matter';
17+
import opentype from '@shuding/opentype.js';
1718

1819
const ROOT = process.cwd();
1920
const CONTENT_DIR = path.join(ROOT, 'src', 'content');
@@ -34,6 +35,33 @@ const medium = fs.readFileSync(
3435
path.join(ROOT, 'public', 'fonts', 'Optimistic_Display_W_Md.ttf')
3536
);
3637

38+
// Title area width: card width minus the horizontal padding (80px per side).
39+
const TITLE_MAX_WIDTH = 1200 - 80 * 2;
40+
const TITLE_MAX_FONT = 96;
41+
const TITLE_MIN_FONT = 56;
42+
// Small margin so borderline titles don't wrap and orphan a single trailing
43+
// character (e.g. the "p" in "renderToStaticMarkup", which is 1px too wide
44+
// to fit on one line at 96px).
45+
const TITLE_SAFETY = 8;
46+
47+
const boldFont = opentype.parse(
48+
bold.buffer.slice(bold.byteOffset, bold.byteOffset + bold.byteLength)
49+
);
50+
51+
// Multi-word titles wrap cleanly at spaces, so a length bucket is fine for
52+
// them. Single-word titles (most API names) can only break mid-word, which
53+
// leaves an orphaned letter on its own line, so instead shrink them just
54+
// enough to fit on a single line.
55+
function titleFontSize(title) {
56+
const trimmed = title.trim();
57+
if (/\s/.test(trimmed)) {
58+
return trimmed.length > 24 ? 72 : 96;
59+
}
60+
const widthPerPx = boldFont.getAdvanceWidth(trimmed, 1);
61+
const fit = Math.floor((TITLE_MAX_WIDTH - TITLE_SAFETY) / widthPerPx);
62+
return Math.max(TITLE_MIN_FONT, Math.min(TITLE_MAX_FONT, fit));
63+
}
64+
3765
function el(type, style, children) {
3866
return {type, props: {style, children}};
3967
}
@@ -103,7 +131,7 @@ function card(title, pagePath) {
103131
flexGrow: 1,
104132
display: 'flex',
105133
alignItems: 'center',
106-
fontSize: title.length > 24 ? 72 : 96,
134+
fontSize: titleFontSize(title),
107135
fontFamily: 'Optimistic Display Bold',
108136
color: '#f6f7f9',
109137
lineHeight: 1.1,

yarn.lock

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,21 +2693,12 @@ camelcase-css@^2.0.1:
26932693
resolved "https://registry.npmjs.org/camelcase-css/-/camelcase-css-2.0.1.tgz"
26942694
integrity sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==
26952695

2696-
<<<<<<< HEAD
2697-
caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001286, caniuse-lite@^1.0.30001297, caniuse-lite@^1.0.30001370, caniuse-lite@^1.0.30001579, caniuse-lite@^1.0.30001688:
2698-
=======
26992696
camelize@^1.0.0:
27002697
version "1.0.1"
27012698
resolved "https://registry.yarnpkg.com/camelize/-/camelize-1.0.1.tgz#89b7e16884056331a35d6b5ad064332c91daa6c3"
27022699
integrity sha512-dU+Tx2fsypxTgtLoE36npi3UqcjSSMNYfkqgmoEhtZrraP5VWq0K7FkWVTYa8eMPtnU/G2txVsfdCJTn9uzpuQ==
27032700

2704-
caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001286, caniuse-lite@^1.0.30001297, caniuse-lite@^1.0.30001370, caniuse-lite@^1.0.30001579:
2705-
version "1.0.30001636"
2706-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001636.tgz#b15f52d2bdb95fad32c2f53c0b68032b85188a78"
2707-
integrity sha512-bMg2vmr8XBsbL6Lr0UHXy/21m84FTxDLWn2FSqMd5PrlbMxwJlQnC2YWYxVgp66PZE+BBNF2jYQUBKCo1FDeZg==
2708-
2709-
caniuse-lite@^1.0.30001688:
2710-
>>>>>>> 6be2b020a0cabf2fd6dbff5c42c399b8ac323bca
2701+
caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001286, caniuse-lite@^1.0.30001297, caniuse-lite@^1.0.30001370, caniuse-lite@^1.0.30001579, caniuse-lite@^1.0.30001688:
27112702
version "1.0.30001692"
27122703
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001692.tgz#4585729d95e6b95be5b439da6ab55250cd125bf9"
27132704
integrity sha512-A95VKan0kdtrsnMubMKxEKUKImOPSuCpYgxSQBo036P5YYgVIcOYJEgt/txJWqObiRQeISNCfef9nvlQ0vbV7A==
@@ -2866,11 +2857,7 @@ color-name@1.1.3:
28662857
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz"
28672858
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=
28682859

2869-
<<<<<<< HEAD
2870-
color-name@~1.1.4:
2871-
=======
28722860
color-name@^1.0.0, color-name@^1.1.4, color-name@~1.1.4:
2873-
>>>>>>> 6be2b020a0cabf2fd6dbff5c42c399b8ac323bca
28742861
version "1.1.4"
28752862
resolved "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz"
28762863
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

0 commit comments

Comments
 (0)