Skip to content

Commit 7886ec1

Browse files
committed
fix(blog): posts constructor no longer loads posts
1 parent 511c82b commit 7886ec1

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/utils/posts.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ export class Posts {
1111
private tagCount: Map<string, number> = new Map<string, number>();
1212
private tagCategories: string[] = [];
1313

14-
constructor() {
15-
this.loadPosts();
16-
}
14+
constructor() { }
1715

1816
private loadPosts(): void {
17+
if (this.posts.length > 0) {
18+
return;
19+
}
20+
1921
const fileNames = fs.readdirSync(postsDirectory);
2022
const uniqueTagCategories = new Set<string>();
2123
const tagCount = new Map<string, number>();
@@ -68,13 +70,15 @@ export class Posts {
6870
* Returns all posts ordered by date.
6971
*/
7072
public getPosts(): PostData[] {
73+
this.loadPosts();
7174
return this.posts.sort((a, b) => b.date.localeCompare(a.date));
7275
}
7376

7477
/**
7578
* Returns the posts with the tag(s) specified, sorted by date
7679
*/
7780
public getPostsByTag(activeFilters: string[]): PostData[] {
81+
this.loadPosts();
7882
return this.posts
7983
.filter((post) =>
8084
activeFilters.every((filter) => post.tags.includes(filter)),
@@ -92,13 +96,15 @@ export class Posts {
9296
}
9397

9498
public getTagCategories(): FilterCategory[] {
99+
this.loadPosts();
95100
return this.tagCategories.map((category) => ({
96101
title: toTitleCase(category),
97102
tags: this.getTagsByCategory(category),
98103
}));
99104
}
100105

101106
public getNeighborPosts(slug: string): NeighborPosts {
107+
this.loadPosts();
102108
const postIdx = this.posts.findIndex((post) => post.slug === slug);
103109

104110
const nextPost = this.posts.at(postIdx + 1);
@@ -110,10 +116,12 @@ export class Posts {
110116
}
111117

112118
public getPost(slug: string): PostData | undefined {
119+
this.loadPosts();
113120
return this.posts.find((post) => post.slug === slug);
114121
}
115122

116123
public getNumPosts(): number {
124+
this.loadPosts();
117125
return this.posts.length;
118126
}
119127
}

0 commit comments

Comments
 (0)