49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import matter from 'gray-matter';
|
|
|
|
export interface PageFrontmatter {
|
|
title: string;
|
|
excerpt: string;
|
|
featuredImage: string | null;
|
|
locale: string;
|
|
}
|
|
|
|
export interface PageMdx {
|
|
slug: string;
|
|
frontmatter: PageFrontmatter;
|
|
content: string;
|
|
}
|
|
|
|
export async function getPageBySlug(slug: string, locale: string): Promise<PageMdx | null> {
|
|
const pagesDir = path.join(process.cwd(), 'data', 'pages', locale);
|
|
const filePath = path.join(pagesDir, `${slug}.mdx`);
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
return null;
|
|
}
|
|
|
|
const fileContent = fs.readFileSync(filePath, 'utf8');
|
|
const { data, content } = matter(fileContent);
|
|
|
|
return {
|
|
slug,
|
|
frontmatter: data as PageFrontmatter,
|
|
content,
|
|
};
|
|
}
|
|
|
|
export async function getAllPages(locale: string): Promise<PageMdx[]> {
|
|
const pagesDir = path.join(process.cwd(), 'data', 'pages', locale);
|
|
if (!fs.existsSync(pagesDir)) return [];
|
|
|
|
const files = fs.readdirSync(pagesDir);
|
|
const pages = await Promise.all(
|
|
files
|
|
.filter(file => file.endsWith('.mdx'))
|
|
.map(file => getPageBySlug(file.replace(/\.mdx$/, ''), locale))
|
|
);
|
|
|
|
return pages.filter((p): p is PageMdx => p !== null);
|
|
}
|