fix: avoid truncating links with story cutoff length

This commit is contained in:
Riccardo
2024-02-23 19:23:08 +01:00
parent 54e6ad8e2b
commit bc5e0cc195
2 changed files with 16 additions and 1 deletions

14
utils/textTruncate.ts Normal file
View File

@@ -0,0 +1,14 @@
export function textTruncate(text: string, length: number) {
let truncatedText = text.substring(0, length);
const lastSpaceIndex = truncatedText.lastIndexOf(' ');
const urlPattern = /(https?:\/\/[^\s]+)/g;
const lastWord = truncatedText.substring(lastSpaceIndex).trim();
if (urlPattern.test(lastWord)) {
truncatedText = truncatedText.substring(0, lastSpaceIndex);
}
truncatedText += '...';
return truncatedText;
}