computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
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
112 lines
2.6 KiB
2 years ago
|
import type { MarkdownFileType } from './file';
|
||
|
|
||
|
export interface BestPracticeFrontmatter {
|
||
|
jsonUrl: string;
|
||
|
pdfUrl: string;
|
||
|
order: number;
|
||
2 years ago
|
briefTitle: string;
|
||
|
briefDescription: string;
|
||
2 years ago
|
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,
|
||
9 months ago
|
},
|
||
2 years ago
|
);
|
||
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,
|
||
9 months ago
|
},
|
||
2 years ago
|
);
|
||
2 years ago
|
|
||
|
const bestPracticeFiles = Object.values(bestPracticeFilesMap);
|
||
|
const bestPracticeItems = bestPracticeFiles.map((bestPracticeFile) => ({
|
||
|
...bestPracticeFile,
|
||
|
id: bestPracticePathToId(bestPracticeFile.file),
|
||
|
}));
|
||
|
|
||
2 years ago
|
return bestPracticeItems.sort(
|
||
9 months ago
|
(a, b) => a.frontmatter.order - b.frontmatter.order,
|
||
2 years ago
|
);
|
||
2 years ago
|
}
|
||
9 months 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),
|
||
|
};
|
||
|
}
|