feat(post-desktop): truncate long file links with ellipsis in middle

This commit is contained in:
plebeius
2026-03-01 17:03:28 +08:00
parent 2bc2827f83
commit f2264ba60c
2 changed files with 14 additions and 3 deletions
+11
View File
@@ -20,3 +20,14 @@ export function formatUserIDForDisplay(userID: string | undefined, maxDomainLeng
// Otherwise, shorten to 8 characters
return userID.slice(0, 8);
}
/**
* Truncate a string with "..." in the middle when it exceeds maxLength.
* Preserves the start and end of the string for readability (e.g. URLs, filenames).
*/
export function truncateWithEllipsisInMiddle(str: string, maxLength: number = 50): string {
if (!str || str.length <= maxLength) return str;
const ellipsis = '...';
const half = Math.floor((maxLength - ellipsis.length) / 2);
return str.slice(0, half) + ellipsis + str.slice(-half);
}