Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 18s
Build & Deploy / 🧪 QA (push) Successful in 59s
Build & Deploy / 🏗️ Build (push) Failing after 1m19s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import matter from 'gray-matter';
|
|
|
|
export interface ReferenceFrontmatter {
|
|
title: string;
|
|
client: string;
|
|
date: string;
|
|
dateString?: string;
|
|
featuredImage?: string | null;
|
|
location: string;
|
|
category?: string | string[];
|
|
}
|
|
|
|
export interface ReferenceData {
|
|
slug: string;
|
|
frontmatter: ReferenceFrontmatter;
|
|
content: string;
|
|
}
|
|
|
|
export async function getReferenceBySlug(slug: string, locale: string): Promise<ReferenceData | null> {
|
|
const localeDir = path.join(process.cwd(), 'content', locale, 'referenzen');
|
|
const filePath = path.join(localeDir, `${slug}.mdx`);
|
|
|
|
if (!fs.existsSync(filePath)) return null;
|
|
|
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
const { content, data } = matter(source);
|
|
|
|
return {
|
|
slug,
|
|
frontmatter: data as ReferenceFrontmatter,
|
|
content,
|
|
};
|
|
}
|
|
|
|
export async function getAllReferences(locale: string): Promise<ReferenceData[]> {
|
|
const localeDir = path.join(process.cwd(), 'content', locale, 'referenzen');
|
|
if (!fs.existsSync(localeDir)) return [];
|
|
|
|
const files = fs.readdirSync(localeDir).filter(f => f.endsWith('.mdx'));
|
|
|
|
const references = await Promise.all(
|
|
files.map(async f => {
|
|
const slug = f.replace('.mdx', '');
|
|
return await getReferenceBySlug(slug, locale);
|
|
})
|
|
);
|
|
|
|
return references
|
|
.filter((p): p is ReferenceData => p !== null)
|
|
.sort((a, b) => new Date(b.frontmatter.date).getTime() - new Date(a.frontmatter.date).getTime());
|
|
}
|