Skip to content

Commit 0319e3e

Browse files
committed
show errors for get post
1 parent 71dc57a commit 0319e3e

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

utils/getPostMetadata.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,26 @@ const getPostMetadata = (getDrafts: boolean = false): PostMetadata[] => {
1111

1212
const posts: PostMetadata[] = markdownPosts.map(
1313
(fileName): Partial<PostMetadata> => {
14-
const fileContents = fs.readFileSync(`${folder}${fileName}`, 'utf8');
15-
const matterResult = matter(fileContents);
16-
17-
return {
18-
title: matterResult.data.title,
19-
date: matterResult.data.date,
20-
tags: matterResult.data.tags,
21-
wordcount: (matterResult.content.match(/\b\w+\b/gu) || []).length,
22-
slug: fileName.replace('.md', ''),
23-
};
14+
try {
15+
const fileContents = fs.readFileSync(`${folder}${fileName}`, 'utf8');
16+
const matterResult = matter(fileContents);
17+
18+
return {
19+
title: matterResult.data.title,
20+
date: matterResult.data.date,
21+
tags: matterResult.data.tags,
22+
wordcount: (matterResult.content.match(/\b\w+\b/gu) || []).length,
23+
slug: fileName.replace('.md', ''),
24+
};
25+
} catch (error) {
26+
console.error(`Error parsing frontmatter in file: ${fileName}`);
27+
console.error(error);
28+
// Return a minimal object to prevent the entire function from failing
29+
return {
30+
title: `Error in ${fileName}`,
31+
slug: fileName.replace('.md', ''),
32+
};
33+
}
2434
}
2535
) as PostMetadata[];
2636

0 commit comments

Comments
 (0)