// blog-post-data.js
// Data layer for blog post management using Vite's dynamic imports and front-matter parsing
import fm from 'front-matter';

// Vite glob import for dynamic markdown file loading
const modules = import.meta.glob('./BlogPosts/*.md', { query: '?raw', import: 'default' });

// Load all blog posts with front-matter parsing
export async function loadPosts() {
    const posts = [];

    // Process each markdown file dynamically
    for (const path in modules) {
        const fileContent = await modules[path]();
        const { attributes, body } = fm(fileContent);

        posts.push({
            ...attributes, // Front-matter attributes (title, date, etc.)
            content: body, // Markdown content
            path,
        });
    }

    return posts;
}

// Find specific post by slug for editor functionality
export async function loadPostBySlug(slug) {
    const posts = await loadPosts();
    return posts.find(post => post.slug === slug);
}
