GameCult-Quartz/quartz/util/social.ts
2026-05-01 15:02:07 +01:00

22 lines
762 B
TypeScript

import { FullSlug, joinSegments, simplifySlug } from "./path"
export function canonicalSocialUrl(baseUrl: string, slug?: FullSlug): string {
const rootUrl = `https://${baseUrl}`
return slug ? joinSegments(rootUrl, simplifySlug(slug)) : rootUrl
}
export function normalizeSocialDescription(description: string, maxLength = 220): string {
const normalized = description.replace(/\s+/g, " ").trim()
if (normalized.length <= maxLength) {
return normalized
}
const softCut = normalized.slice(0, maxLength + 1)
const boundary = softCut.lastIndexOf(" ")
const truncated =
boundary > Math.floor(maxLength * 0.6)
? softCut.slice(0, boundary)
: normalized.slice(0, maxLength)
return truncated.replace(/[.,;:\s]+$/g, "") + "..."
}