fix: avoid truncating links with story cutoff length
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { NewsSchema } from '../../utils/schemas';
|
import { NewsSchema } from '../../utils/schemas';
|
||||||
|
import { textTruncate } from '../../utils/textTruncate';
|
||||||
import { sayings } from './helpers/sayings';
|
import { sayings } from './helpers/sayings';
|
||||||
import Template from './template';
|
import Template from './template';
|
||||||
|
|
||||||
@@ -58,7 +59,7 @@ export default function NewsletterTemplate(
|
|||||||
dangerouslySetInnerHTML={{
|
dangerouslySetInnerHTML={{
|
||||||
__html:
|
__html:
|
||||||
story.text.length > 500
|
story.text.length > 500
|
||||||
? story.text.substring(0, 500) + '...'
|
? textTruncate(story.text, 500) + '...'
|
||||||
: story.text
|
: story.text
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
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