feat(posts): support short titles for resource cards

Add optional post-level and localized title fields, and use a new
postTitleText helper for Resource.title so card/list surfaces prefer short
backend-provided titles instead of full body text. When title is missing,
fallback to the first non-empty body line, then filename, then post id.

Document the backend handoff in docs/posts-title-api.md alongside the other
backend task docs.
This commit is contained in:
TerryM
2026-05-30 03:11:03 +08:00
parent 2b9ab9eb2c
commit 6798e90708
4 changed files with 82 additions and 2 deletions

View File

@@ -11,3 +11,19 @@ export function postDisplayText(post: Post, lang: string): string {
""
);
}
export function postTitleText(post: Post, lang: string): string {
const key = localizationKey(lang);
const localized =
post.localizations?.[key as keyof typeof post.localizations];
const explicitTitle = localized?.title?.trim() || post.title?.trim();
if (explicitTitle) return explicitTitle;
const text = postDisplayText(post, lang);
return (
text
.split(/\n{1,}/)
.map((line) => line.trim())
.find(Boolean) || ""
);
}