AI Resource Pro / website / scripts / revert-html-to-markdown.ts
| 1 | import { createServiceClient } from '../src/lib/supabase'; |
| 2 | |
| 3 | /** |
| 4 | * This script reverts HTML content back to markdown for all blog posts |
| 5 | * It removes any HTML processing that was done previously |
| 6 | */ |
| 7 | async function main() { |
| 8 | console.log('Starting to revert HTML content back to markdown for all blog posts...'); |
| 9 | |
| 10 | try { |
| 11 | // Initialize Supabase client |
| 12 | const supabase = createServiceClient(); |
| 13 | |
| 14 | // Fetch all blog posts |
| 15 | const { data: posts, error } = await supabase |
| 16 | .from('posts') |
| 17 | .select('id, title, slug, content'); |
| 18 | |
| 19 | if (error) { |
| 20 | throw error; |
| 21 | } |
| 22 | |
| 23 | if (!posts || posts.length === 0) { |
| 24 | console.log('No blog posts found to process.'); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | console.log(`Found ${posts.length} blog posts to process.`); |
| 29 | |
| 30 | // Process each post |
| 31 | let successCount = 0; |
| 32 | let skippedCount = 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 | skippedCount++; |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | console.log(`Processing post "${post.title}" (ID: ${post.id})...`); |
| 44 | |
| 45 | // Check if content appears to be HTML |
| 46 | const hasHtmlTags = /<\/?[a-z][\s\S]*>/i.test(post.content); |
| 47 | |
| 48 | if (!hasHtmlTags) { |
| 49 | console.log(`Post "${post.title}" (ID: ${post.id}) appears to already be in markdown format, skipping.`); |
| 50 | skippedCount++; |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | // Simple HTML to Markdown conversions |
| 55 | // This is a basic implementation and works for simple cases |
| 56 | let markdownContent = post.content |
| 57 | // Replace <div class="syntax-highlight"...> with ``` |
| 58 | .replace(/<div class="syntax-highlight" data-language="(\w+)"><pre class="language-\w+"><code class="language-\w+">/, '```$1\n') |
| 59 | .replace(/<\/code><\/pre><\/div>/g, '\n```') |
| 60 | |
| 61 | // Replace <strong> with ** |
| 62 | .replace(/<strong>(.*?)<\/strong>/g, '**$1**') |
| 63 | .replace(/<b>(.*?)<\/b>/g, '**$1**') |
| 64 | |
| 65 | // Replace <em> with * |
| 66 | .replace(/<em>(.*?)<\/em>/g, '*$1*') |
| 67 | .replace(/<i>(.*?)<\/i>/g, '*$1*') |
| 68 | |
| 69 | // Replace <h1> - <h6> with # - ###### |
| 70 | .replace(/<h1>(.*?)<\/h1>/g, '# $1') |
| 71 | .replace(/<h2>(.*?)<\/h2>/g, '## $1') |
| 72 | .replace(/<h3>(.*?)<\/h3>/g, '### $1') |
| 73 | .replace(/<h4>(.*?)<\/h4>/g, '#### $1') |
| 74 | .replace(/<h5>(.*?)<\/h5>/g, '##### $1') |
| 75 | .replace(/<h6>(.*?)<\/h6>/g, '###### $1') |
| 76 | |
| 77 | // Replace <a> with [text](url) |
| 78 | .replace(/<a href="(.*?)".*?>(.*?)<\/a>/g, '[$2]($1)') |
| 79 | |
| 80 | // Replace <blockquote> with > |
| 81 | .replace(/<blockquote>(.*?)<\/blockquote>/g, '> $1') |
| 82 | |
| 83 | // Replace <ul> and <li> |
| 84 | .replace(/<ul>([\s\S]*?)<\/ul>/g, (match: string, content: string) => { |
| 85 | return content.replace(/<li>([\s\S]*?)<\/li>/g, '- $1\n'); |
| 86 | }) |
| 87 | |
| 88 | // Replace <ol> and <li> |
| 89 | .replace(/<ol>([\s\S]*?)<\/ol>/g, (match: string, content: string) => { |
| 90 | let counter = 1; |
| 91 | return content.replace(/<li>([\s\S]*?)<\/li>/g, () => { |
| 92 | return `${counter++}. $1\n`; |
| 93 | }); |
| 94 | }) |
| 95 | |
| 96 | // Replace <p> with line breaks |
| 97 | .replace(/<p>([\s\S]*?)<\/p>/g, '$1\n\n') |
| 98 | |
| 99 | // Replace <br> with line breaks |
| 100 | .replace(/<br\s*\/?>/g, '\n') |
| 101 | |
| 102 | // Replace <code> with ` |
| 103 | .replace(/<code>(.*?)<\/code>/g, '`$1`') |
| 104 | |
| 105 | // Remove any other HTML tags |
| 106 | .replace(/<\/?[^>]+(>|$)/g, '') |
| 107 | |
| 108 | // Fix extra line breaks |
| 109 | .replace(/\n{3,}/g, '\n\n') |
| 110 | .trim(); |
| 111 | |
| 112 | // Update the post with markdown content |
| 113 | const { error: updateError } = await supabase |
| 114 | .from('posts') |
| 115 | .update({ content: markdownContent }) |
| 116 | .eq('id', post.id); |
| 117 | |
| 118 | if (updateError) { |
| 119 | console.error(`Failed to update post "${post.title}" (ID: ${post.id}):`, updateError); |
| 120 | failureCount++; |
| 121 | } else { |
| 122 | console.log(`Successfully reverted content to markdown for post "${post.title}" (ID: ${post.id})`); |
| 123 | successCount++; |
| 124 | } |
| 125 | } catch (postError) { |
| 126 | console.error(`Error processing post "${post.title}" (ID: ${post.id}):`, postError); |
| 127 | failureCount++; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | console.log(` |
| 132 | Process complete: |
| 133 | - Successfully reverted ${successCount} blog posts to markdown |
| 134 | - Skipped ${skippedCount} blog posts (already in markdown or no content) |
| 135 | - Failed to process ${failureCount} blog posts |
| 136 | `); |
| 137 | } catch (error) { |
| 138 | console.error('Error reverting blog posts to markdown:', error); |
| 139 | process.exit(1); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Run the script |
| 144 | main() |
| 145 | .then(() => { |
| 146 | console.log('Script completed successfully.'); |
| 147 | process.exit(0); |
| 148 | }) |
| 149 | .catch((error) => { |
| 150 | console.error('Script failed with error:', error); |
| 151 | process.exit(1); |
| 152 | }); |