auto-claude: subtask-1-1 - Add readingTime field to BlogPost interface and im
This commit is contained in:
@@ -10,6 +10,7 @@ export interface BlogPost {
|
||||
category?: string;
|
||||
tags?: string[];
|
||||
content?: string;
|
||||
readingTime?: number;
|
||||
}
|
||||
|
||||
const BLOG_POSTS_DIR = path.join(process.cwd(), 'blog-posts');
|
||||
@@ -70,6 +71,26 @@ function detectCategory(filename: string, content: string): string {
|
||||
return 'Technologie';
|
||||
}
|
||||
|
||||
function calculateReadingTime(content: string): number {
|
||||
// Remove markdown syntax for more accurate word count
|
||||
const text = content
|
||||
.replace(/```[\s\S]*?```/g, '') // Remove code blocks
|
||||
.replace(/`[^`]*`/g, '') // Remove inline code
|
||||
.replace(/!\[.*?\]\(.*?\)/g, '') // Remove images
|
||||
.replace(/\[.*?\]\(.*?\)/g, '') // Remove links
|
||||
.replace(/[#*_~]/g, '') // Remove markdown formatting
|
||||
.replace(/\s+/g, ' ') // Normalize whitespace
|
||||
.trim();
|
||||
|
||||
// Count words
|
||||
const words = text.split(/\s+/).filter(word => word.length > 0).length;
|
||||
|
||||
// Calculate reading time (average reading speed: 200 words per minute)
|
||||
const minutes = Math.ceil(words / 200);
|
||||
|
||||
return Math.max(1, minutes); // Minimum 1 minute
|
||||
}
|
||||
|
||||
function parseMarkdownPost(filename: string, content: string): BlogPost | null {
|
||||
try {
|
||||
// Extract title from first H1
|
||||
@@ -113,6 +134,9 @@ function parseMarkdownPost(filename: string, content: string): BlogPost | null {
|
||||
// Cover image path
|
||||
const coverImage = `/images/posts/${slug}/cover.jpg`;
|
||||
|
||||
// Calculate reading time
|
||||
const readingTime = calculateReadingTime(content);
|
||||
|
||||
return {
|
||||
slug,
|
||||
title,
|
||||
@@ -122,6 +146,7 @@ function parseMarkdownPost(filename: string, content: string): BlogPost | null {
|
||||
category,
|
||||
tags,
|
||||
content,
|
||||
readingTime,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Error parsing ${filename}:`, error);
|
||||
|
||||
Reference in New Issue
Block a user