mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-17 18:45:32 -04:00
more caching and error handling
This commit is contained in:
@@ -19,38 +19,42 @@ export type FrontMatter = {
|
||||
};
|
||||
|
||||
// returns front matter and the **raw & uncompiled** markdown of a given slug
|
||||
export const getFrontMatter = async (slug: string): Promise<FrontMatter> => {
|
||||
const { frontmatter } = await import(`../../${POSTS_DIR}/${slug}/index.mdx`);
|
||||
export const getFrontMatter = cache(async (slug: string): Promise<FrontMatter | null> => {
|
||||
try {
|
||||
const { frontmatter } = await import(`../../${POSTS_DIR}/${slug}/index.mdx`);
|
||||
|
||||
// process markdown title to html...
|
||||
const htmlTitle = await unified()
|
||||
.use(remarkParse)
|
||||
.use(remarkSmartypants)
|
||||
.use(remarkHtml, {
|
||||
sanitize: {
|
||||
// allow *very* limited markdown to be used in post titles
|
||||
tagNames: ["code", "em", "strong"],
|
||||
},
|
||||
})
|
||||
.process(frontmatter.title)
|
||||
.then((result) => result.toString().trim());
|
||||
// process markdown title to html...
|
||||
const htmlTitle = await unified()
|
||||
.use(remarkParse)
|
||||
.use(remarkSmartypants)
|
||||
.use(remarkHtml, {
|
||||
sanitize: {
|
||||
// allow *very* limited markdown to be used in post titles
|
||||
tagNames: ["code", "em", "strong"],
|
||||
},
|
||||
})
|
||||
.process(frontmatter.title)
|
||||
.then((result) => result.toString().trim());
|
||||
|
||||
// ...and then (sketchily) remove said html for a plaintext version:
|
||||
// https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
|
||||
const title = decode(htmlTitle.replace(/<[^>]*>/g, ""));
|
||||
// ...and then (sketchily) remove said html for a plaintext version:
|
||||
// https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
|
||||
const title = decode(htmlTitle.replace(/<[^>]*>/g, ""));
|
||||
|
||||
// return both the parsed YAML front matter (with a few amendments) and the raw, unparsed markdown content
|
||||
return {
|
||||
...(frontmatter as Partial<FrontMatter>),
|
||||
// plain title without html or markdown syntax:
|
||||
title,
|
||||
// stylized title with limited html tags:
|
||||
htmlTitle,
|
||||
slug,
|
||||
date: new Date(frontmatter.date).toISOString(), // validate/normalize the date string provided from front matter
|
||||
permalink: `${BASE_URL}/${POSTS_DIR}/${slug}`,
|
||||
};
|
||||
};
|
||||
return {
|
||||
...(frontmatter as Partial<FrontMatter>),
|
||||
// plain title without html or markdown syntax:
|
||||
title,
|
||||
// stylized title with limited html tags:
|
||||
htmlTitle,
|
||||
slug,
|
||||
date: new Date(frontmatter.date).toISOString(), // validate/normalize the date string provided from front matter
|
||||
permalink: `${BASE_URL}/${POSTS_DIR}/${slug}`,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Failed to load front matter for post with slug "${slug}":`, error);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
// use filesystem to get a simple list of all post slugs
|
||||
export const getPostSlugs = cache(async (): Promise<string[]> => {
|
||||
@@ -70,7 +74,7 @@ export const getPostSlugs = cache(async (): Promise<string[]> => {
|
||||
export const getAllPosts = cache(async (): Promise<FrontMatter[]> => {
|
||||
// concurrently fetch the front matter of each post
|
||||
const slugs = await getPostSlugs();
|
||||
const posts = await Promise.all(slugs.map(getFrontMatter));
|
||||
const posts = (await Promise.all(slugs.map(getFrontMatter))) as FrontMatter[];
|
||||
|
||||
// sort the results reverse chronologically and return
|
||||
return posts.sort((post1, post2) => new Date(post1.date).getTime() - new Date(post2.date).getTime());
|
||||
|
Reference in New Issue
Block a user