Skip to content

Commit e23778a

Browse files
jcllobetOpenWork Bot
andauthored
Improve OG share preview readability (anomalyco#964)
Co-authored-by: OpenWork Bot <[email protected]>
1 parent 6143397 commit e23778a

2 files changed

Lines changed: 54 additions & 42 deletions

File tree

‎services/openwork-share/server/_lib/render-og-image-response.tsx‎

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const canvasStyle: CSSProperties = {
1414

1515
const monoStyle: CSSProperties = {
1616
fontFamily: "monospace",
17-
fontSize: "15px",
18-
lineHeight: "22px",
17+
fontSize: "24px",
18+
lineHeight: "34px",
1919
color: "#475569",
2020
};
2121

@@ -53,7 +53,7 @@ function segmentStyle(className: OgTokenClass): CSSProperties {
5353
case "hl-frontmatter":
5454
return { color: "#94a3b8" };
5555
case "hl-key":
56-
return { color: "#475569" };
56+
return { color: "#be123c" };
5757
case "hl-url":
5858
return { color: "#2563eb" };
5959
case "hl-string":
@@ -65,15 +65,15 @@ function segmentStyle(className: OgTokenClass): CSSProperties {
6565
case "hl-number":
6666
return { color: "#f97316" };
6767
case "hl-keyword":
68-
return { color: "#be123c" };
68+
return { color: "#7c3aed", fontWeight: 600 };
6969
case "hl-comment":
7070
return { color: "#94a3b8", fontStyle: "italic" };
7171
case "hl-heading":
7272
return { color: "#0f172a", fontWeight: 700 };
7373
case "hl-field":
7474
return { color: "#be123c" };
7575
case "hl-inline-code":
76-
return { color: "#7c3aed" };
76+
return { color: "#0891b2", background: "rgba(8,145,178,0.08)", borderRadius: "4px", paddingLeft: "2px", paddingRight: "2px" };
7777
case "hl-bold":
7878
return { color: "#0f172a", fontWeight: 700 };
7979
case "hl-type":
@@ -132,51 +132,40 @@ function OgImage({ model }: { model: OgImageModel }): ReactElement {
132132
>
133133
<div
134134
style={{
135-
height: "66px",
135+
height: "78px",
136136
borderBottom: "1px solid rgba(226,232,240,0.92)",
137137
display: "flex",
138138
alignItems: "center",
139139
justifyContent: "space-between",
140-
padding: "0 34px",
140+
padding: "0 28px",
141141
}}
142142
>
143-
<div style={{ ...sansStyle, fontSize: "28px", fontWeight: 800, letterSpacing: "-1.2px" }}>SKILL.md</div>
143+
<div style={{ ...sansStyle, fontSize: "40px", fontWeight: 900, letterSpacing: "-1.2px" }}>SKILL.md</div>
144144
<div style={{ display: "flex", alignItems: "center", gap: "13px" }}>
145145
<div
146146
style={{
147-
width: "14px",
148-
height: "14px",
147+
width: "18px",
148+
height: "18px",
149149
borderRadius: "999px",
150150
background: "linear-gradient(135deg, #f97316 0%, #facc15 100%)",
151151
}}
152152
/>
153-
<div style={{ ...monoStyle, fontSize: "16px", color: "#94a3b8" }}>{model.filename}</div>
153+
<div style={{ ...monoStyle, fontSize: "30px", fontWeight: 700, color: "#94a3b8", lineHeight: "34px" }}>{model.filename}</div>
154154
</div>
155155
</div>
156156

157157
<div
158158
style={{
159159
display: "flex",
160160
flexDirection: "column",
161-
padding: "50px 34px 0 34px",
161+
padding: "48px 34px 0 34px",
162162
gap: "0px",
163163
...monoStyle,
164164
}}
165165
>
166166
{model.lines.map((line, index) => (
167-
<div key={index} style={{ display: "flex", minHeight: "22px", alignItems: "baseline" }}>
168-
<div
169-
style={{
170-
width: "44px",
171-
color: "#cbd5e1",
172-
fontSize: "13px",
173-
fontWeight: 600,
174-
lineHeight: "22px",
175-
}}
176-
>
177-
{line.lineNumber == null ? "" : String(line.lineNumber).padStart(2, "0")}
178-
</div>
179-
<div style={{ display: "flex", whiteSpace: "pre", flexWrap: "nowrap" }}>
167+
<div key={index} style={{ display: "flex", minHeight: `${line.height}px`, alignItems: "baseline" }}>
168+
<div style={{ display: "flex", whiteSpace: "pre", flexWrap: "nowrap", letterSpacing: "-0.3px" }}>
180169
{line.segments.length ? (
181170
line.segments.map((segment, segmentIndex) => (
182171
<span key={segmentIndex} style={segmentStyle(segment.className)}>

‎services/openwork-share/server/_lib/render-og-image.ts‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type TokenSegment = {
3030
type WrappedLine = {
3131
lineNumber: number | null;
3232
segments: TokenSegment[];
33+
height: number;
3334
};
3435

3536
export type OgTokenClass = TokenClass;
@@ -47,13 +48,13 @@ type SkillCardInput = {
4748
shareUrl: string;
4849
};
4950

50-
const FONT_SIZE = 15;
51-
const LINE_HEIGHT = 22;
51+
const FONT_SIZE = 24;
52+
const LINE_HEIGHT = 34;
5253
const BODY_X = 34;
53-
const BODY_Y = 116;
54-
const BODY_WIDTH = 1084;
55-
const BODY_HEIGHT = 454;
56-
const CHAR_WIDTH = 8.9;
54+
const BODY_Y = 126;
55+
const BODY_WIDTH = 1110;
56+
const BODY_HEIGHT = 410;
57+
const CHAR_WIDTH = 12.2;
5758
const OG_SANS_FONT = "DejaVu Sans, Liberation Sans, Arial, sans-serif";
5859
const OG_MONO_FONT = "DejaVu Sans Mono, Liberation Mono, Courier New, monospace";
5960
const MAX_LINES = Math.floor(BODY_HEIGHT / LINE_HEIGHT);
@@ -242,6 +243,17 @@ function buildWrappedLines(text: string, shareUrl: string): WrappedLine[] {
242243
let truncated = false;
243244

244245
for (const [index, htmlLine] of htmlLines.entries()) {
246+
if (!(rawLines[index] ?? "").trim()) {
247+
if (wrapped.length && wrapped[wrapped.length - 1]?.segments.length) {
248+
wrapped.push({
249+
lineNumber: null,
250+
segments: [],
251+
height: 14,
252+
});
253+
}
254+
continue;
255+
}
256+
245257
const quotedCodeSegments = detectQuotedCodeSegments(rawLines[index] ?? "");
246258
const parsed = quotedCodeSegments
247259
? quotedCodeSegments
@@ -255,6 +267,7 @@ function buildWrappedLines(text: string, shareUrl: string): WrappedLine[] {
255267
wrapped.push({
256268
lineNumber: wrapped.length + 1,
257269
segments: lineGroup,
270+
height: LINE_HEIGHT,
258271
});
259272
}
260273
if (truncated) break;
@@ -264,11 +277,13 @@ function buildWrappedLines(text: string, shareUrl: string): WrappedLine[] {
264277
wrapped.push({
265278
lineNumber: null,
266279
segments: [],
280+
height: 18,
267281
});
268282
for (const continuationLine of continuationLines) {
269283
wrapped.push({
270284
lineNumber: null,
271285
segments: continuationLine,
286+
height: LINE_HEIGHT,
272287
});
273288
}
274289
}
@@ -288,7 +303,7 @@ function classStyle(className: TokenClass): { fill: string; weight?: string; sty
288303
case "hl-frontmatter":
289304
return { fill: "#94a3b8" };
290305
case "hl-key":
291-
return { fill: "#475569" };
306+
return { fill: "#be123c" };
292307
case "hl-url":
293308
return { fill: "#2563eb" };
294309
case "hl-string":
@@ -300,15 +315,15 @@ function classStyle(className: TokenClass): { fill: string; weight?: string; sty
300315
case "hl-number":
301316
return { fill: "#f97316" };
302317
case "hl-keyword":
303-
return { fill: "#be123c" };
318+
return { fill: "#7c3aed", weight: "600" };
304319
case "hl-comment":
305320
return { fill: "#94a3b8", style: "italic" };
306321
case "hl-heading":
307322
return { fill: "#0f172a", weight: "700" };
308323
case "hl-field":
309324
return { fill: "#be123c" };
310325
case "hl-inline-code":
311-
return { fill: "#7c3aed" };
326+
return { fill: "#0891b2" };
312327
case "hl-bold":
313328
return { fill: "#0f172a", weight: "700" };
314329
case "hl-type":
@@ -328,16 +343,17 @@ function classStyle(className: TokenClass): { fill: string; weight?: string; sty
328343

329344
function renderLine(line: WrappedLine, y: number): string {
330345
let x = BODY_X;
331-
const gutter = line.lineNumber == null ? "" : `<text x="${BODY_X}" y="${y}" fill="#cbd5e1" font-family="${OG_MONO_FONT}" font-size="13" font-weight="600">${String(line.lineNumber).padStart(2, "0")}</text>`;
332-
x += 44;
346+
const gutter = "";
333347

334348
return `${gutter}${line.segments
335349
.map((segment, index) => {
336350
const style = classStyle(segment.className);
337351
const width = segmentWidth(segment.text);
338352
const pill =
339353
segment.className === "hl-codeblock"
340-
? `<rect x="${x - 1}" y="${y - 14}" width="${width + 8}" height="22" rx="5" fill="#f8fafc" stroke="#cbd5e1" />`
354+
? `<rect x="${x - 1}" y="${y - 18}" width="${width + 10}" height="28" rx="6" fill="#f8fafc" stroke="#cbd5e1" />`
355+
: segment.className === "hl-inline-code"
356+
? `<rect x="${x - 2}" y="${y - 18}" width="${width + 6}" height="28" rx="5" fill="rgba(8,145,178,0.08)" />`
341357
: "";
342358
const node = `<text x="${x}" y="${y}" fill="${style.fill}" font-family="${OG_MONO_FONT}" font-size="${FONT_SIZE}"${style.weight ? ` font-weight="${style.weight}"` : ""}${style.style ? ` font-style="${style.style}"` : ""}${segment.className === "hl-cta-url" ? ` text-decoration="underline"` : ""}>${escapeSvgText(segment.text)}</text>`;
343359
x += width;
@@ -348,15 +364,22 @@ function renderLine(line: WrappedLine, y: number): string {
348364

349365
function renderPreviewSurface(input: { title: string; filename: string; text: string; shareUrl: string }): string {
350366
const model = buildSkillCardModel(input);
367+
let currentY = BODY_Y;
351368

352369
return `
353370
<g transform="translate(18 12)">
354371
<rect width="1164" height="606" rx="38" fill="rgba(255,255,255,0.96)" stroke="rgba(148,163,184,0.16)" />
355-
<path d="M0 66H1164" stroke="rgba(226,232,240,0.92)" />
356-
<text x="34" y="42" fill="#0f172a" font-family="${OG_SANS_FONT}" font-size="28" font-weight="800" letter-spacing="-1.2">SKILL.md</text>
357-
<circle cx="930" cy="33" r="7" fill="url(#skillGradient)" />
358-
<text x="950" y="40" fill="#94a3b8" font-family="${OG_MONO_FONT}" font-size="16">${escapeSvgText(input.filename)}</text>
359-
${model.lines.map((line, index) => renderLine(line, BODY_Y + index * LINE_HEIGHT)).join("")}
372+
<path d="M0 78H1164" stroke="rgba(226,232,240,0.92)" />
373+
<text x="34" y="52" fill="#0f172a" font-family="${OG_SANS_FONT}" font-size="40" font-weight="900" letter-spacing="-1.2">SKILL.md</text>
374+
<circle cx="888" cy="39" r="9" fill="url(#skillGradient)" />
375+
<text x="912" y="48" fill="#94a3b8" font-family="${OG_MONO_FONT}" font-size="30" font-weight="700">${escapeSvgText(input.filename)}</text>
376+
${model.lines
377+
.map((line) => {
378+
const y = currentY;
379+
currentY += line.height;
380+
return renderLine(line, y);
381+
})
382+
.join("")}
360383
</g>`;
361384
}
362385

0 commit comments

Comments
 (0)