37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
// Generate _redirects file for pretty URLs
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const contentDir = path.join(__dirname, '../src/content/blog');
|
|
|
|
const redirects = [
|
|
'# redirects for pretty URLs - generated automatically',
|
|
''
|
|
];
|
|
|
|
// Read all blog post directories
|
|
const entries = fs.readdirSync(contentDir, { withFileTypes: true });
|
|
for (const entry of entries) {
|
|
if (entry.isDirectory()) {
|
|
const slug = entry.name;
|
|
const postDir = path.join(contentDir, slug);
|
|
const hasPostFile = ['index.md', 'index.mdx'].some((file) => {
|
|
const postPath = path.join(postDir, file);
|
|
return fs.existsSync(postPath) && fs.statSync(postPath).isFile();
|
|
});
|
|
|
|
if (!hasPostFile) {
|
|
continue;
|
|
}
|
|
|
|
redirects.push(`/${slug} /blog/${slug} 200`);
|
|
}
|
|
}
|
|
|
|
// Write _redirects file
|
|
const outputPath = path.join(__dirname, '../public/_redirects');
|
|
fs.writeFileSync(outputPath, redirects.join('\n') + '\n');
|
|
|
|
console.log(`Generated ${redirects.length - 2} redirects`);
|