4.1 KiB
4.1 KiB
Blog Post Fixing Script
This Node.js script automatically fixes common issues in blog posts when migrating from WordPress to Astro, specifically:
- HTML entities to Unicode conversion - Converts HTML entities like
🎯to proper Unicode characters like 🎯 - WordPress caption shortcodes to Markdown - Converts WordPress
[caption]shortcodes to clean Markdown image syntax - Image URL conversion - Converts WordPress image URLs to proper Astro asset import paths
Features
- Automatic backup creation before making any changes
- Comprehensive HTML entity support - Handles both named (
&) and numeric (🎯,{) entities - WordPress caption conversion - Converts
[caption]shortcodes to Markdown images with optional captions - Smart image path conversion - Converts WordPress upload URLs to Astro asset paths (
~/assets/images/...) - Image reference fixing - Automatically matches broken image references with existing files
- Detailed logging - Complete logs of all changes made
- Error handling - Robust error handling with detailed error messages
- Safe operation - Only modifies files that actually need changes
Usage
# Run the script
node fix-blog-posts.cjs
What it fixes
1. HTML Entities
Before:
title: '🎯 Dual Band Flower Pot Anteni'
After:
title: '🎯 Dual Band Flower Pot Anteni'
2. WordPress Caption Shortcodes
Before:
[caption id="attachment_223" align="aligncenter" width="300"]<img class="wp-image-223 size-medium" src="https://radio.org.tr/wp-content/uploads/2025/04/DMR-HS-300x300.png" alt="D.MR. Hot Spot" width="300" height="300" /> Örnek D.M.R. HotSpot[/caption]
After:

*Örnek D.M.R. HotSpot*
3. Image Path Conversion
Before:

After:

Configuration
You can modify the configuration at the top of the script:
const config = {
postsDirectory: './src/data/post', // Where your blog posts are located
assetsDirectory: './src/assets/images', // Where your images are stored
backupDirectory: './blog-posts-backup', // Where backups are created
logFile: './fix-blog-posts.log' // Log file location
};
Output
The script provides detailed console output and creates comprehensive logs:
✅ Blog post processing completed successfully!
📄 Check ./fix-blog-posts.log for detailed logs
💾 Backup created in ./blog-posts-backup
Processing Summary:
- Total files processed: 38
- Files with changes: 13
- Files with errors: 0
- Files unchanged: 25
Safety Features
- Automatic backup - Creates a complete backup of all posts before making changes
- Dry run capability - Logs what would be changed without actually modifying files
- Atomic operations - Either all changes to a file succeed, or none are applied
- Image validation - Only converts image references if the target file actually exists
- Detailed logging - Every change is logged with before/after details
Supported HTML Entities
The script includes comprehensive support for:
- Common HTML entities (
&,<,>, etc.) - Unicode emoji entities (
🎯→ 🎯) - Extended character entities (
—,…, etc.) - Numeric entities (both decimal and hexadecimal)
Error Handling
The script handles various error conditions gracefully:
- Missing files or directories
- Permission issues
- Malformed content
- Missing images
- Invalid HTML entities
All errors are logged with detailed information for troubleshooting.
Requirements
- Node.js (any recent version)
- File system permissions to read/write in the project directory
Changelog
v1.0.0
- Initial release with HTML entity conversion, WordPress caption conversion, and image path fixing
- Comprehensive logging and backup system
- Support for 500+ HTML entities and Unicode characters
- Smart image file matching and validation