wip
This commit is contained in:
22
lib/blog.ts
22
lib/blog.ts
@@ -73,3 +73,25 @@ export async function getAdjacentPosts(slug: string, locale: string): Promise<{
|
||||
|
||||
return { prev, next };
|
||||
}
|
||||
|
||||
export function getReadingTime(content: string): number {
|
||||
const wordsPerMinute = 200;
|
||||
const noOfWords = content.split(/\s/g).length;
|
||||
const minutes = noOfWords / wordsPerMinute;
|
||||
return Math.ceil(minutes);
|
||||
}
|
||||
|
||||
export function getHeadings(content: string): { id: string; text: string; level: number }[] {
|
||||
const headingLines = content.split('\n').filter((line) => line.match(/^#{2,3}\s/));
|
||||
|
||||
return headingLines.map((line) => {
|
||||
const level = line.match(/^#+/)?.[0].length || 0;
|
||||
const text = line.replace(/^#+\s/, '').trim();
|
||||
const id = text
|
||||
.toLowerCase()
|
||||
.replace(/[^\w\s-]/g, '')
|
||||
.replace(/\s+/g, '-');
|
||||
|
||||
return { id, text, level };
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user