feat: make tweet field optional in PostDetailResponse and refactor metadata generation functions
This commit is contained in:
parent
2d67da4ba7
commit
0d70401a8f
1 changed files with 22 additions and 5 deletions
|
|
@ -13,7 +13,7 @@ type PostDetailResponse = {
|
|||
url?: string;
|
||||
author?: string;
|
||||
tags?: string[];
|
||||
tweet: {
|
||||
tweet?: {
|
||||
text?: string;
|
||||
title?: string;
|
||||
};
|
||||
|
|
@ -37,6 +37,22 @@ function createDetailDescription(post: PostDetailResponse) {
|
|||
return `${author} | ${source} post${tagText}`;
|
||||
}
|
||||
|
||||
function createDetailTitle(post: PostDetailResponse) {
|
||||
const author = post.author?.trim() || "unknown";
|
||||
const source = getSourceLabel(post.type);
|
||||
const rawText = post.type === "twitter" ? post.tweet?.text : post.tweet?.title;
|
||||
const contentText = rawText?.trim() || post._id;
|
||||
return `${author} - ${contentText} | ${source}`;
|
||||
}
|
||||
|
||||
function toAbsoluteUrl(value: string, baseUrl: string) {
|
||||
try {
|
||||
return new URL(value, baseUrl).toString();
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function getApiBaseUrl() {
|
||||
|
|
@ -122,12 +138,13 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin
|
|||
|
||||
const source = getSourceLabel(post.type);
|
||||
const author = post.author?.trim() || "unknown";
|
||||
const title = `${author} - ${post.type == "twitter" ? post.tweet.text : post.tweet.title} | ${source}`;
|
||||
const title = createDetailTitle(post);
|
||||
const description = createDetailDescription(post);
|
||||
const ogImages = post.mediaUrl
|
||||
const ogImageUrl = post.mediaUrl ? toAbsoluteUrl(post.mediaUrl, apiBaseUrl) : undefined;
|
||||
const ogImages = ogImageUrl
|
||||
? [
|
||||
{
|
||||
url: post.mediaUrl,
|
||||
url: ogImageUrl,
|
||||
alt: `${author} ${source}`,
|
||||
},
|
||||
]
|
||||
|
|
@ -151,7 +168,7 @@ export async function generateMetadata({ params }: { params: Promise<{ id: strin
|
|||
card: post.mediaUrl ? "summary_large_image" : "summary",
|
||||
title,
|
||||
description,
|
||||
images: post.mediaUrl ? [post.mediaUrl] : undefined,
|
||||
images: ogImageUrl ? [ogImageUrl] : undefined,
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue