Add utils folder
This commit is contained in:
25
src/utils/getAllPosts.js
Normal file
25
src/utils/getAllPosts.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { getNormalizedPost } from "~/utils/getNormalizedPost";
|
||||||
|
|
||||||
|
const load = async function () {
|
||||||
|
const posts = import.meta.glob("../data/posts/**/*.{md,mdx}", {
|
||||||
|
eager: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const normalizedPosts = Object.keys(posts).map(async (key) => {
|
||||||
|
const post = await posts[key];
|
||||||
|
return await getNormalizedPost(post);
|
||||||
|
});
|
||||||
|
|
||||||
|
const results = (await Promise.all(normalizedPosts)).sort(
|
||||||
|
(a, b) => new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf()
|
||||||
|
);
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
let _posts;
|
||||||
|
|
||||||
|
export const getAllPosts = async () => {
|
||||||
|
_posts = _posts || load();
|
||||||
|
|
||||||
|
return await _posts;
|
||||||
|
};
|
17
src/utils/getNormalizedPost.js
Normal file
17
src/utils/getNormalizedPost.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import getReadingTime from "reading-time";
|
||||||
|
|
||||||
|
export const getNormalizedPost = async (post) => {
|
||||||
|
const { frontmatter, compiledContent, rawContent, file } = post;
|
||||||
|
|
||||||
|
return {
|
||||||
|
pubDate: frontmatter.pubDate,
|
||||||
|
title: frontmatter.title,
|
||||||
|
description: frontmatter.description,
|
||||||
|
excerpt: frontmatter.excerpt,
|
||||||
|
body: compiledContent(),
|
||||||
|
image: frontmatter.image,
|
||||||
|
authors: frontmatter.authors,
|
||||||
|
slug: file.split("/").pop().split(".").shift(),
|
||||||
|
readingTime: Math.ceil(getReadingTime(rawContent()).minutes),
|
||||||
|
};
|
||||||
|
};
|
Reference in New Issue
Block a user