Add blog utilities and tag pages
Introduce formatBlogDate, slugifyTag and getTagHref in src/lib/blog.ts and a reading time helper getReadingTime in src/lib/readingTime.ts. Update blog index and post pages to display ISO dates, reading times and link tags to /blog/tag/<slug>. Add a tag listing page at src/pages/blog/tag/[tag].astro with styles and static paths generation
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
export function formatBlogDate(date: Date) {
|
||||
return date.toISOString().split("T")[0];
|
||||
}
|
||||
|
||||
export function slugifyTag(tag: string) {
|
||||
return tag
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/&/g, " and ")
|
||||
.replace(/[^a-z0-9\s-]/g, "")
|
||||
.replace(/\s+/g, "-")
|
||||
.replace(/-+/g, "-");
|
||||
}
|
||||
|
||||
export function getTagHref(tag: string) {
|
||||
return `/blog/tag/${slugifyTag(tag)}`;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
export function getReadingTime(content: string) {
|
||||
const normalized = content
|
||||
.replace(/```[\s\S]*?```/g, " ")
|
||||
.replace(/`[^`]*`/g, " ")
|
||||
.replace(/!\[[^\]]*\]\([^)]+\)/g, " ")
|
||||
.replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
|
||||
.replace(/<[^>]+>/g, " ")
|
||||
.replace(/[#>*_~-]/g, " ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
|
||||
const wordCount = normalized ? normalized.split(" ").length : 0;
|
||||
const minutes = Math.max(1, Math.ceil(wordCount / 200));
|
||||
|
||||
return {
|
||||
minutes,
|
||||
text: `${minutes} min read`,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user