import { createServiceClient } from '../src/lib/supabase'; /** * This script reverts HTML content back to markdown for all blog posts * It removes any HTML processing that was done previously */ async function main() { console.log('Starting to revert HTML content back to markdown 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 skippedCount = 0; let failureCount = 0; for (const post of posts) { try { if (!post.content) { console.log(`Skipping post "${post.title}" (ID: ${post.id}) - no content`); skippedCount++; continue; } console.log(`Processing post "${post.title}" (ID: ${post.id})...`); // Check if content appears to be HTML const hasHtmlTags = /<\/?[a-z][\s\S]*>/i.test(post.content); if (!hasHtmlTags) { console.log(`Post "${post.title}" (ID: ${post.id}) appears to already be in markdown format, skipping.`); skippedCount++; continue; } // Simple HTML to Markdown conversions // This is a basic implementation and works for simple cases let markdownContent = post.content // Replace
/, '```$1\n')
.replace(/<\/code><\/pre><\/div>/g, '\n```')
// Replace with **
.replace(/(.*?)<\/strong>/g, '**$1**')
.replace(/(.*?)<\/b>/g, '**$1**')
// Replace with *
.replace(/(.*?)<\/em>/g, '*$1*')
.replace(/(.*?)<\/i>/g, '*$1*')
// Replace - with # - ######
.replace(/(.*?)<\/h1>/g, '# $1')
.replace(/(.*?)<\/h2>/g, '## $1')
.replace(/(.*?)<\/h3>/g, '### $1')
.replace(/(.*?)<\/h4>/g, '#### $1')
.replace(/(.*?)<\/h5>/g, '##### $1')
.replace(/(.*?)<\/h6>/g, '###### $1')
// Replace with [text](url)
.replace(/(.*?)<\/a>/g, '[$2]($1)')
// Replace with >
.replace(/(.*?)<\/blockquote>/g, '> $1')
// Replace and -
.replace(/
([\s\S]*?)<\/ul>/g, (match: string, content: string) => {
return content.replace(/- ([\s\S]*?)<\/li>/g, '- $1\n');
})
// Replace
and -
.replace(/
([\s\S]*?)<\/ol>/g, (match: string, content: string) => {
let counter = 1;
return content.replace(/- ([\s\S]*?)<\/li>/g, () => {
return `${counter++}. $1\n`;
});
})
// Replace
with line breaks
.replace(/
([\s\S]*?)<\/p>/g, '$1\n\n')
// Replace
with line breaks
.replace(/
/g, '\n')
// Replace with `
.replace(/(.*?)<\/code>/g, '`$1`')
// Remove any other HTML tags
.replace(/<\/?[^>]+(>|$)/g, '')
// Fix extra line breaks
.replace(/\n{3,}/g, '\n\n')
.trim();
// Update the post with markdown content
const { error: updateError } = await supabase
.from('posts')
.update({ content: markdownContent })
.eq('id', post.id);
if (updateError) {
console.error(`Failed to update post "${post.title}" (ID: ${post.id}):`, updateError);
failureCount++;
} else {
console.log(`Successfully reverted content to markdown for 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 reverted ${successCount} blog posts to markdown
- Skipped ${skippedCount} blog posts (already in markdown or no content)
- Failed to process ${failureCount} blog posts
`);
} catch (error) {
console.error('Error reverting blog posts to markdown:', 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);
});