import { useCallback } from 'react'; import { BlogOutlineSection } from '../services/blogWriterApi'; export const useMarkdownProcessor = ( outline: BlogOutlineSection[], sections: Record ) => { const buildFullMarkdown = useCallback(() => { if (!outline.length) return ''; return outline.map(s => `## ${s.heading}\n\n${sections[s.id] || ''}`).join('\n\n'); }, [outline, sections]); const convertMarkdownToHTML = useCallback((md: string) => { return md .replace(/^### (.*$)/gim, '

$1

') .replace(/^## (.*$)/gim, '

$1

') .replace(/^# (.*$)/gim, '

$1

') .replace(/\*\*(.*)\*\*/gim, '$1') .replace(/\*(.*)\*/gim, '$1') .replace(/\n\n/g, '

'); }, []); const getTotalWords = useCallback(() => { const fullMarkdown = buildFullMarkdown(); return fullMarkdown.split(/\s+/).filter(word => word.length > 0).length; }, [buildFullMarkdown]); const getSectionWordCount = useCallback((sectionId: string) => { const content = sections[sectionId] || ''; return content.split(/\s+/).filter(word => word.length > 0).length; }, [sections]); const getOutlineStats = useCallback(() => { const totalWords = getTotalWords(); const totalSections = outline.length; const totalSubheadings = outline.reduce((sum, section) => sum + section.subheadings.length, 0); const totalKeyPoints = outline.reduce((sum, section) => sum + section.key_points.length, 0); return { totalWords, totalSections, totalSubheadings, totalKeyPoints, averageWordsPerSection: totalSections > 0 ? Math.round(totalWords / totalSections) : 0 }; }, [outline, getTotalWords]); return { buildFullMarkdown, convertMarkdownToHTML, getTotalWords, getSectionWordCount, getOutlineStats }; };