AI Resource Pro / website / scripts / process-blog-markdown.ts
| 1 | import { createServiceClient } from '../src/lib/supabase'; |
| 2 | import { processMarkdown } from '../src/lib/blog-utils'; |
| 3 | |
| 4 | /** |
| 5 | * This script processes the markdown content of all existing blog posts |
| 6 | * and updates them with HTML content. |
| 7 | */ |
| 8 | async function main() { |
| 9 | console.log('Starting to process markdown content for all blog posts...'); |
| 10 | |
| 11 | try { |
| 12 | // Initialize Supabase client |
| 13 | const supabase = createServiceClient(); |
| 14 | |
| 15 | // Fetch all blog posts |
| 16 | const { data: posts, error } = await supabase |
| 17 | .from('posts') |
| 18 | .select('id, title, slug, content'); |
| 19 | |
| 20 | if (error) { |
| 21 | throw error; |
| 22 | } |
| 23 | |
| 24 | if (!posts || posts.length === 0) { |
| 25 | console.log('No blog posts found to process.'); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | console.log(`Found ${posts.length} blog posts to process.`); |
| 30 | |
| 31 | // Process each post |
| 32 | let successCount = 0; |
| 33 | let failureCount = 0; |
| 34 | |
| 35 | for (const post of posts) { |
| 36 | try { |
| 37 | if (!post.content) { |
| 38 | console.log(`Skipping post "${post.title}" (ID: ${post.id}) - no content`); |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | console.log(`Processing post "${post.title}" (ID: ${post.id})...`); |
| 43 | |
| 44 | // Process the markdown content |
| 45 | const processedContent = await processMarkdown(post.content); |
| 46 | |
| 47 | // Update the post with processed content |
| 48 | const { error: updateError } = await supabase |
| 49 | .from('posts') |
| 50 | .update({ content: processedContent }) |
| 51 | .eq('id', post.id); |
| 52 | |
| 53 | if (updateError) { |
| 54 | console.error(`Failed to update post "${post.title}" (ID: ${post.id}):`, updateError); |
| 55 | failureCount++; |
| 56 | } else { |
| 57 | console.log(`Successfully processed and updated post "${post.title}" (ID: ${post.id})`); |
| 58 | successCount++; |
| 59 | } |
| 60 | } catch (postError) { |
| 61 | console.error(`Error processing post "${post.title}" (ID: ${post.id}):`, postError); |
| 62 | failureCount++; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | console.log(` |
| 67 | Process complete: |
| 68 | - Successfully processed ${successCount} blog posts |
| 69 | - Failed to process ${failureCount} blog posts |
| 70 | `); |
| 71 | } catch (error) { |
| 72 | console.error('Error processing blog posts:', error); |
| 73 | process.exit(1); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Run the script |
| 78 | main() |
| 79 | .then(() => { |
| 80 | console.log('Script completed successfully.'); |
| 81 | process.exit(0); |
| 82 | }) |
| 83 | .catch((error) => { |
| 84 | console.error('Script failed with error:', error); |
| 85 | process.exit(1); |
| 86 | }); |