import React, { useCallback } from "react"; import { HighlightConfig, HighlightTarget } from "@/training/TrainingInterfaces"; interface HighlightedContentProps { html: string; highlightConfigs: HighlightConfig[]; contentType: HighlightTarget; currentSegmentIndex?: number; } const HighlightedContent: React.FC = ({ html, highlightConfigs, contentType, currentSegmentIndex }) => { const createHighlightedContent = useCallback(() => { let highlightedHtml = html; highlightConfigs.forEach(config => { if (config.targets.includes(contentType) || config.targets.includes('all')) { const escapeRegExp = (string: string) => { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }; const regex = new RegExp(config.phrases.map(escapeRegExp).join('|'), 'g'); if (contentType === 'segment' && currentSegmentIndex !== undefined) { const segments = highlightedHtml.split(''); segments[currentSegmentIndex] = segments[currentSegmentIndex].replace(regex, (match) => { return `${match}`; }); highlightedHtml = segments.join(''); } else { highlightedHtml = highlightedHtml.replace(regex, (match) => { return `${match}`; }); } } }); return { __html: highlightedHtml }; }, [html, highlightConfigs, contentType, currentSegmentIndex]); return
; }; export default HighlightedContent;