QR-master/scripts/fix-metadata-dates.js

47 lines
1.9 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, '../src/lib/blog-data.ts');
let content = fs.readFileSync(filePath, 'utf-8');
// Fix the date formatting issue in metadata divs
// Replace "undefined NaN, NaN" with proper formatted dates from the post data
const postRegex = /slug:\s*"([^"]+)"[\s\S]*?date:\s*"([^"]+)"[\s\S]*?updatedAt:\s*"([^"]+)"[\s\S]*?<div class="post-metadata[^>]*>[\s\S]*?<strong>Published:<\/strong>\s*[^|]*\s*\|\s*<strong>Last updated:<\/strong>\s*undefined NaN, NaN/gm;
let match;
const replacements = [];
// First pass: collect all post slugs with their correct dates
const postDatesRegex = /slug:\s*"([^"]+)"[\s\S]*?date:\s*"([^"]+)"[\s\S]*?updatedAt:\s*"([^"]+)"/gm;
while ((match = postDatesRegex.exec(content)) !== null) {
const slug = match[1];
const publishDate = match[2]; // e.g., "February 16, 2026"
const updatedDate = match[3]; // e.g., "2026-01-26"
// Format the updated date
const [year, month, day] = updatedDate.split('-');
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const formattedUpdated = `${months[parseInt(month) - 1]} ${parseInt(day)}, ${year}`;
replacements.push({
slug,
publishDate,
updatedDate: formattedUpdated
});
}
// Now replace the broken metadata divs
replacements.forEach(({ slug, publishDate, updatedDate }) => {
const pattern = new RegExp(
`(<div class="post-metadata[^>]*>[\s\S]*?<strong>Published:<\/strong>\s*)${publishDate.replace(/[.*+?^${}()|[\]\\]/g, '\$&')}([\s\S]*?<strong>Last updated:<\/strong>\s*)undefined NaN, NaN`,
'gm'
);
content = content.replace(pattern, `$1${publishDate}$2${updatedDate}`);
});
fs.writeFileSync(filePath, content, 'utf-8');
console.log(`✅ Fixed date formatting in ${replacements.length} posts`);