Roadmap to becoming a developer in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.6 KiB

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<BestPracticeFrontmatter> & {
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() {
2 years ago
const bestPracticeFiles = await import.meta.glob<BestPracticeFileType>(
'/src/data/best-practices/*/*.md',
{
eager: true,
},
2 years ago
);
return Object.keys(bestPracticeFiles).map(bestPracticePathToId);
}
/**
* Gets all the best practice files
*
* @returns Promisified BestPracticeFileType[]
*/
export async function getAllBestPractices(): Promise<BestPracticeFileType[]> {
2 years ago
const bestPracticeFilesMap = await import.meta.glob<BestPracticeFileType>(
'/src/data/best-practices/*/*.md',
{
eager: true,
},
2 years ago
);
const bestPracticeFiles = Object.values(bestPracticeFilesMap);
const bestPracticeItems = bestPracticeFiles.map((bestPracticeFile) => ({
...bestPracticeFile,
id: bestPracticePathToId(bestPracticeFile.file),
}));
2 years ago
return bestPracticeItems.sort(
(a, b) => a.frontmatter.order - b.frontmatter.order,
2 years ago
);
}
/**
* Gets the best practice file by ID
*
* @param id - Best practice file ID
* @returns BestPracticeFileType
*/
export async function getBestPracticeById(
id: string,
): Promise<BestPracticeFileType | null> {
const bestPracticeFilesMap = import.meta.glob<BestPracticeFileType>(
'/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),
};
}