import { createServiceClient } from '../src/lib/supabase'; import { processMarkdown } from '../src/lib/blog-utils'; /** * This script processes the markdown content of all existing blog posts * and updates them with HTML content. */ async function main() { console.log('Starting to process markdown content for all blog posts...'); try { // Initialize Supabase client const supabase = createServiceClient(); // Fetch all blog posts const { data: posts, error } = await supabase .from('posts') .select('id, title, slug, content'); if (error) { throw error; } if (!posts || posts.length === 0) { console.log('No blog posts found to process.'); return; } console.log(`Found ${posts.length} blog posts to process.`); // Process each post let successCount = 0; let failureCount = 0; for (const post of posts) { try { if (!post.content) { console.log(`Skipping post "${post.title}" (ID: ${post.id}) - no content`); continue; } console.log(`Processing post "${post.title}" (ID: ${post.id})...`); // Process the markdown content const processedContent = await processMarkdown(post.content); // Update the post with processed content const { error: updateError } = await supabase .from('posts') .update({ content: processedContent }) .eq('id', post.id); if (updateError) { console.error(`Failed to update post "${post.title}" (ID: ${post.id}):`, updateError); failureCount++; } else { console.log(`Successfully processed and updated post "${post.title}" (ID: ${post.id})`); successCount++; } } catch (postError) { console.error(`Error processing post "${post.title}" (ID: ${post.id}):`, postError); failureCount++; } } console.log(` Process complete: - Successfully processed ${successCount} blog posts - Failed to process ${failureCount} blog posts `); } catch (error) { console.error('Error processing blog posts:', error); process.exit(1); } } // Run the script main() .then(() => { console.log('Script completed successfully.'); process.exit(0); }) .catch((error) => { console.error('Script failed with error:', error); process.exit(1); });