type PageModule = {
  frontmatter: Record<string, unknown>;
  Content: unknown;
  rawContent?: () => string;
};

export type SitePage = PageModule & {
  title: string;
  slug: string;
};

const modules = import.meta.glob<PageModule>('/source/**/index.md', { eager: true });

export function getSitePages(): SitePage[] {
  return Object.entries(modules)
    .filter(([path]) => {
      if (path.includes('/_posts/') || path.includes('/_drafts/')) return false;
      return !['/source/categories/index.md', '/source/tags/index.md'].includes(path);
    })
    .map(([path, module]) => {
      const slug = path.replace('/source/', '').replace('/index.md', '');
      return {
        ...module,
        title: String(module.frontmatter.title ?? slug),
        slug,
      };
    });
}
