diff --git a/lib/utils/text-direction.ts b/lib/utils/text-direction.ts index b198dd06..0a972247 100644 --- a/lib/utils/text-direction.ts +++ b/lib/utils/text-direction.ts @@ -67,28 +67,65 @@ export function applyTextDirection(htmlContent: string, textContent?: string): s * Process content to determine direction and return direction-enhanced HTML * * @param content Content to process (HTML or plain text) - * @returns Object containing processed HTML and detected direction + * @returns Object containing processed HTML, plain text, and detected direction */ -export function processContentWithDirection(content: string | undefined): { html: string; direction: 'ltr' | 'rtl' } { +export function processContentWithDirection(content: string | undefined | { html?: string; text?: string }): { + html: string; + text: string; + direction: 'ltr' | 'rtl' +} { if (!content) { - return { html: '', direction: 'ltr' }; + return { html: '', text: '', direction: 'ltr' }; } - // Extract text from the content for direction detection - const textContent = content.replace(/<[^>]*>/g, '') - .replace(/ /g, ' ') - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/&/g, '&'); + let htmlContent = ''; + let textContent = ''; + + // Handle different content types + if (typeof content === 'string') { + // String content might be HTML or plain text + if (content.includes('<') && (content.includes(']*>/g, '') + .replace(/ /g, ' ') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/&/g, '&'); + } else { + // It's plain text + textContent = content; + // Create simple HTML from the plain text (preserve whitespace) + htmlContent = content.replace(/\n/g, '
').replace(/\s\s/g, '  '); + } + } else { + // It's an object with html and/or text properties + htmlContent = content.html || ''; + textContent = content.text || ''; + + // If we have HTML but no text, extract text from HTML + if (htmlContent && !textContent) { + textContent = htmlContent.replace(/<[^>]*>/g, '') + .replace(/ /g, ' ') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/&/g, '&'); + } + + // If we have text but no HTML, create simple HTML from the text + if (textContent && !htmlContent) { + htmlContent = textContent.replace(/\n/g, '
').replace(/\s\s/g, '  '); + } + } // Detect the direction of the text const direction = detectTextDirection(textContent); // Apply the direction to the HTML content - const directionEnhancedHtml = applyTextDirection(content, textContent); + const directionEnhancedHtml = applyTextDirection(htmlContent, textContent); return { html: directionEnhancedHtml, + text: textContent, direction }; } \ No newline at end of file