init
This commit is contained in:
27
lib/workshop.ts
Normal file
27
lib/workshop.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { WorkShop } from '@/types/workShop';
|
||||
import fs from 'fs';
|
||||
import matter from 'gray-matter';
|
||||
import path from 'path';
|
||||
|
||||
// 获取指定语言的所有车间
|
||||
export async function getWorkShops(locale: string): Promise<WorkShop[]> {
|
||||
const contentPath = path.join(process.cwd(), `content/workshop/${locale}`);
|
||||
console.log(contentPath);
|
||||
const files = fs.readdirSync(contentPath);
|
||||
const workShops: WorkShop[] = [];
|
||||
for (const file of files.map((file) => file.replace('.mdx', ''))) {
|
||||
const contentPath = path.join(process.cwd(), `content/workshop/${locale}/${file}.mdx`);
|
||||
const fileContent = fs.readFileSync(contentPath, 'utf8');
|
||||
const { data: frontmatter, content } = matter(fileContent);
|
||||
workShops.push(frontmatter as WorkShop);
|
||||
}
|
||||
return workShops;
|
||||
}
|
||||
|
||||
// 获取特定车间的内容
|
||||
export async function getWorkShop(locale: string, title: string): Promise<WorkShop> {
|
||||
const contentPath = path.join(process.cwd(), `content/workshop/${locale}/${title}.mdx`);
|
||||
const fileContent = fs.readFileSync(contentPath, 'utf8');
|
||||
const { data: frontmatter, content } = matter(fileContent);
|
||||
return frontmatter as WorkShop;
|
||||
}
|
||||
Reference in New Issue
Block a user