1
0
Fork 0

feat: add static content utils, with gray-matter metadata

This commit is contained in:
Vojtěch Mareš 2023-06-25 15:59:34 +02:00
parent 410ce498c8
commit ce8bb3e8f8
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
2 changed files with 61 additions and 1 deletions

59
lib/content/training.ts Normal file
View file

@ -0,0 +1,59 @@
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
type Training = {
metadata: {
id: string;
name: string;
slug: string;
description: string;
days: number;
weight: number;
draft?: boolean;
logoURL?: string;
repositoryURL?: string;
priceOpen: number;
priceCorporate: number;
};
content: string;
};
export type { Training };
const root = process.cwd();
export async function getTrainingFiles() {
return fs.readdirSync(path.join(root, 'content', 'training'), 'utf-8');
}
export async function getTrainingBySlug(slug: string, fields: string[] = []) {
const source = fs.readFileSync(path.join(root, 'content', 'training', `${slug}.md`), 'utf8');
const { data, content } = matter(source);
return {
metadata: data,
content: content,
};
}
export async function getAllTrainingsWithMetadata(): Promise<Training[]> {
const files = fs.readdirSync(path.join(root, 'content', 'training'))
// @ts-ignore
return files.reduce((allTrainings, fileName) => {
const source = fs.readFileSync(path.join(root, 'content', 'training', fileName), 'utf8');
const { data, content } = matter(source);
return [
{
metadata: data,
content: content,
},
...allTrainings,
]
}, [])
}

View file

@ -18,7 +18,8 @@
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
"~/*": ["./src/*"],
"~/lib/*": ["./lib/*"]
}
},
"include": [