fix: avoid truncating links with story cutoff length
This commit is contained in:
14
utils/textTruncate.ts
Normal file
14
utils/textTruncate.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user