65 lines
1.5 KiB
TypeScript
65 lines
1.5 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import matter from 'gray-matter';
|
|
|
|
type Training = {
|
|
metadata: {
|
|
id: string;
|
|
name: string;
|
|
slug: string;
|
|
days: number;
|
|
weight: number;
|
|
draft?: boolean;
|
|
logoURL?: string;
|
|
svgIconURL?: string;
|
|
repositoryURL?: string;
|
|
priceOpen: number;
|
|
priceCorporate: number;
|
|
};
|
|
|
|
content: string;
|
|
};
|
|
|
|
export type { Training };
|
|
|
|
const root = process.cwd();
|
|
|
|
export function getTrainingFiles() {
|
|
return fs.readdirSync(path.join(root, 'content', 'training'), 'utf-8');
|
|
}
|
|
|
|
export function getTrainingBySlug(slug: string) {
|
|
const source = fs.readFileSync(path.join(root, 'content', 'training', `${slug}.md`), 'utf8');
|
|
|
|
const { data, content } = matter(source);
|
|
|
|
return {
|
|
metadata: data,
|
|
content: content,
|
|
};
|
|
}
|
|
|
|
export function getAllTrainingsWithMetadata(): Training[] {
|
|
const files = fs.readdirSync(path.join(root, 'content', 'training'))
|
|
|
|
const trainings = [] as Training[];
|
|
|
|
for (const fileName of files) {
|
|
const source = fs.readFileSync(path.join(root, 'content', 'training', fileName), 'utf8');
|
|
const { data: metadata, content: content } = matter(source);
|
|
trainings.push({metadata, content} as Training);
|
|
}
|
|
|
|
return trainings;
|
|
|
|
// return files.reduce((allTrainings, fileName) => {
|
|
// const source = fs.readFileSync(path.join(root, 'content', 'training', fileName), 'utf8');
|
|
// const training = matter(source);
|
|
|
|
// return [
|
|
// training,
|
|
// ...allTrainings,
|
|
// ]
|
|
// }, [])
|
|
}
|
|
|