import type { MarkdownFileType } from './file'; export interface BestPracticeFrontmatter { jsonUrl: string; pdfUrl: string; order: number; briefTitle: string; briefDescription: string; title: string; description: string; isNew: boolean; isUpcoming: boolean; dimensions?: { width: number; height: number; }; seo: { title: string; description: string; keywords: string[]; }; schema?: { headline: string; description: string; datePublished: string; dateModified: string; imageUrl: string; }; } export type BestPracticeFileType = MarkdownFileType & { id: string; }; function bestPracticePathToId(filePath: string): string { const fileName = filePath.split('/').pop() || ''; return fileName.replace('.md', ''); } /** * Gets the IDs of all the best practices available on the website * * @returns string[] Array of best practices file IDs */ export async function getBestPracticeIds() { const bestPracticeFiles = await import.meta.glob( '/src/data/best-practices/*/*.md', { eager: true, }, ); return Object.keys(bestPracticeFiles).map(bestPracticePathToId); } /** * Gets all the best practice files * * @returns Promisified BestPracticeFileType[] */ export async function getAllBestPractices(): Promise { const bestPracticeFilesMap = await import.meta.glob( '/src/data/best-practices/*/*.md', { eager: true, }, ); const bestPracticeFiles = Object.values(bestPracticeFilesMap); const bestPracticeItems = bestPracticeFiles.map((bestPracticeFile) => ({ ...bestPracticeFile, id: bestPracticePathToId(bestPracticeFile.file), })); return bestPracticeItems.sort( (a, b) => a.frontmatter.order - b.frontmatter.order, ); } /** * Gets the best practice file by ID * * @param id - Best practice file ID * @returns BestPracticeFileType */ export async function getBestPracticeById( id: string, ): Promise { const bestPracticeFilesMap = import.meta.glob( '/src/data/best-practices/*/*.md', { eager: true, }, ); const bestPracticeFiles = Object.values(bestPracticeFilesMap); const bestPracticeFile = bestPracticeFiles.find( (bestPracticeFile) => bestPracticePathToId(bestPracticeFile.file) === id, ); if (!bestPracticeFile) { throw new Error(`Best practice with ID ${id} not found`); } return { ...bestPracticeFile, id: bestPracticePathToId(bestPracticeFile.file), }; }