File size: 1,013 Bytes
e7a2183 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const blogPostsFile = path.join(__dirname, "../src/content/blogPosts.ts");
const text = fs.readFileSync(blogPostsFile, "utf8");
const slugRe = /slug:\s*"([^"]+)"/g;
const slugs = [];
let m;
while ((m = slugRe.exec(text))) {
slugs.push({ slug: m[1], idx: m.index });
}
const posts = [];
for (let i = 0; i < slugs.length; i++) {
const start = slugs[i].idx;
const end = slugs[i + 1]?.idx ?? text.length;
const block = text.slice(start, end);
const heroM = block.match(/heroImage:\s*"([^"]+)"/);
if (heroM) continue;
const slug = slugs[i].slug;
const titleM = block.match(/title:\s*"([^"]+)"/);
const descM = block.match(/description:\s*\n\s*"([^"]+)"/);
posts.push({
slug,
title: titleM?.[1] ?? slug,
description: descM?.[1] ?? "",
heroImage: `/blog/blog-cover-${slug}.png`,
});
}
console.log(JSON.stringify(posts, null, 2));
|