Newer
Older
hello-programmer-world / src / constants / sections.ts
@h.sakamoto h.sakamoto on 6 Feb 1 KB git
import type { MDXInstance } from "astro";

export type PageItem = {
  url: string;
  file: string;
  title: string;
};

export type Section = {
  title: string;
  key: string;
  items: PageItem[];
  description?: string;
};

// "html",
// "js",
// "cli",
// "php",
// "sql",
// "git",
// "tips",
// "reference",
// "final-work",

const sections: Section[] = [
  {
    key: "html",
    title: "HTML",
    items: [],
    description: "",
  },
  {
    key: "js",
    title: "JavaScript",
    items: [],
    description: "自分のページに振る舞い方を教えよう。",
  },
  {
    key: "cli",
    title: "コマンドライン",
    items: [],
    description: "文字だけの世界で、パソコンと会話しよう。",
  },
  {
    key: "php",
    title: "PHP",
    items: [],
    description: "",
  },
  {
    key: "sql",
    title: "SQL",
    items: [],
    description: "",
  },
  {
    key: "git",
    title: "Git",
    items: [],
    description: "",
  },
  {
    key: "tips",
    title: "Tips",
    items: [],
  },
  {
    key: "reference",
    title: "参考資料",
    items: [],
  },
  {
    key: "final-work",
    title: "最終課題",
    items: [],
  },
];

type Article = {
  title?: string;
};

async function getSectionPages(sectionPath: string): Promise<PageItem[]> {
  const allPages = import.meta.glob<MDXInstance<Article>>(`../pages/**/*.mdx`);
  const filteredPages = Object.entries(allPages).filter(([path, _]) =>
    path.includes(`/pages/${sectionPath}/`),
  );

  const files = await Promise.all(
    filteredPages.map(async ([filePath, resolver]) => {
      const module = await resolver();

      const page: PageItem = {
        file: filePath,
        title: module.title || "",
        url: module.url || "",
      };

      return page;
    }),
  );

  return files;
}

sections.forEach(async (section) => {
  const pages = await getSectionPages(section.key);
  section.items = pages.sort();
});

export { sections };