Skip to content

Commit 84879da

Browse files
committed
responsive og html template, siteUrl and title
1 parent 5f69f2d commit 84879da

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed
Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
11
import { html } from 'satori-html';
22

3+
import { limitString } from '@/utils/strings';
4+
35
export interface TemplateProps {
46
title: string;
57
heroImageUrl: string;
68
avatarImageUrl: string;
79
siteUrl: string;
810
}
911

10-
const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: TemplateProps) => html`
11-
<div class="flex p-8 h-full" style="background: linear-gradient(to right, #D1D5DB, #F3F4F6)">
12-
<div class="flex w-full flex-row justify-between text-slate-900">
13-
<!-- left column -->
14-
<div class="w-[550px] flex flex-col justify-between mr-6">
15-
<!-- title -->
16-
<div class="flex text-6xl font-semibold mb-4">${title}</div>
17-
18-
<!-- avatar and site -->
19-
<div class="flex items-center">
20-
<img
21-
src=${avatarImageUrl}
22-
alt="Nemanja Mitic"
23-
width="120"
24-
height="120"
25-
class="rounded-full mr-8"
26-
/>
27-
<div class="h-full flex items-center text-4xl break-words">
28-
<div>${siteUrl}</div>
12+
const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: TemplateProps) => {
13+
// 2 rows - max 30 chars
14+
// 1 row - max 18 chars
15+
const isLongSiteUrl = siteUrl.length > 17;
16+
17+
// max 6 rows x 10-15 chars
18+
const limitedTitle = limitString(title, 70);
19+
20+
return html`
21+
<div class="flex p-8 h-full" style="background: linear-gradient(to right, #D1D5DB, #F3F4F6)">
22+
<div class="flex w-full flex-row justify-between text-slate-900">
23+
<!-- left column -->
24+
<div class="w-[550px] flex flex-col justify-between mr-6">
25+
<!-- title -->
26+
<div class="flex flex-grow text-6xl font-semibold mb-4">${limitedTitle}</div>
27+
28+
<!-- avatar and site -->
29+
<div class="flex items-center ${isLongSiteUrl ? 'flex-col justify-end items-start' : ''}">
30+
<img
31+
src=${avatarImageUrl}
32+
alt="Nemanja Mitic"
33+
width="120"
34+
height="120"
35+
class="rounded-full mr-8"
36+
/>
37+
<div class="flex items-center ${isLongSiteUrl ? 'mt-4 text-3xl' : 'text-4xl'}">
38+
<div>${siteUrl}</div>
39+
</div>
2940
</div>
3041
</div>
31-
</div>
3242
33-
<!-- right column -->
34-
<div class="w-[550px] flex items-center">
35-
<img src="${heroImageUrl}" class="h-full w-full rounded-md" style="object-fit: cover" />
43+
<!-- right column -->
44+
<div class="w-[550px] flex items-center">
45+
<img src="${heroImageUrl}" class="h-full w-full rounded-md" style="object-fit: cover" />
46+
</div>
3647
</div>
3748
</div>
38-
</div>
39-
`;
49+
`;
50+
};
4051

4152
export default templateHtml;

src/utils/strings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export const getRandomInt = (max: number, min = 0) =>
44
export const getRandomLengthSubstring = (inputString: string, length: number, margin = 0) =>
55
inputString.substring(0, length + getRandomInt(margin));
66

7+
/** for satori og template */
8+
export const limitString = (str: string, maxLength: number) =>
9+
str.length > maxLength ? str.slice(0, maxLength) + '...' : str;
10+
711
export const trimHttpProtocol = (url: string) => {
812
const trailingSlashRegex = /\/$/;
913
const protocolRegex = /^(https?:\/\/)/i;

0 commit comments

Comments
 (0)