mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-17 18:45:32 -04:00
refactor notes directory and front matter
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
import glob from "fast-glob";
|
||||
import pMap from "p-map";
|
||||
import pMemoize from "p-memoize";
|
||||
import matter from "gray-matter";
|
||||
import { formatDate } from "./format-date";
|
||||
import config from "../config";
|
||||
|
||||
@@ -23,21 +21,14 @@ export type FrontMatter = {
|
||||
};
|
||||
|
||||
// returns front matter and the **raw & uncompiled** markdown of a given slug
|
||||
export const getPostData = async (
|
||||
slug: string
|
||||
): Promise<{
|
||||
frontMatter: FrontMatter;
|
||||
markdown: string;
|
||||
}> => {
|
||||
export const getFrontMatter = async (slug: string): Promise<FrontMatter> => {
|
||||
const { frontmatter } = await import(`../../${POSTS_DIR}/${slug}/index.mdx`);
|
||||
|
||||
const { unified } = await import("unified");
|
||||
const { remarkParse, remarkSmartypants, remarkRehype, rehypeSanitize, rehypeStringify } = await import(
|
||||
"./remark-rehype-plugins"
|
||||
);
|
||||
|
||||
const fullPath = path.join(process.cwd(), POSTS_DIR, `${slug}.mdx`);
|
||||
const rawContent = await fs.readFile(fullPath, "utf8");
|
||||
const { data, content } = matter(rawContent);
|
||||
|
||||
// allow *very* limited markdown to be used in post titles
|
||||
const parseTitle = async (title: string, allowedTags: string[] = []): Promise<string> => {
|
||||
return String(
|
||||
@@ -58,35 +49,32 @@ export const getPostData = async (
|
||||
|
||||
// process title as both plain and stylized
|
||||
const [title, htmlTitle] = await Promise.all([
|
||||
parseTitle(data.title),
|
||||
parseTitle(data.title, ["code", "em", "strong"]),
|
||||
parseTitle(frontmatter.title),
|
||||
parseTitle(frontmatter.title, ["code", "em", "strong"]),
|
||||
]);
|
||||
|
||||
// return both the parsed YAML front matter (with a few amendments) and the raw, unparsed markdown content
|
||||
return {
|
||||
frontMatter: {
|
||||
...(data as Partial<FrontMatter>),
|
||||
// zero markdown title:
|
||||
title,
|
||||
htmlTitle,
|
||||
slug,
|
||||
date: formatDate(data.date), // validate/normalize the date string provided from front matter
|
||||
permalink: `${config.baseUrl}/${POSTS_DIR}/${slug}/`,
|
||||
image: data.image ? `${config.baseUrl}${data.image}` : undefined,
|
||||
},
|
||||
markdown: content,
|
||||
...(frontmatter as Partial<FrontMatter>),
|
||||
// zero markdown title:
|
||||
title,
|
||||
htmlTitle,
|
||||
slug,
|
||||
date: formatDate(frontmatter.date), // validate/normalize the date string provided from front matter
|
||||
permalink: `${config.baseUrl}/${POSTS_DIR}/${slug}/`,
|
||||
image: frontmatter.image ? `${config.baseUrl}${frontmatter.image}` : undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export const getPostSlugs = pMemoize(async (): Promise<string[]> => {
|
||||
// list all .mdx files in POSTS_DIR
|
||||
const mdxFiles = await glob("*.mdx", {
|
||||
const mdxFiles = await glob("**/*.mdx", {
|
||||
cwd: path.join(process.cwd(), POSTS_DIR),
|
||||
dot: false,
|
||||
});
|
||||
|
||||
// strip the .mdx extensions from filenames
|
||||
const slugs = mdxFiles.map((fileName) => fileName.replace(/\.mdx$/, ""));
|
||||
const slugs = mdxFiles.map((fileName) => fileName.replace(/\/index\.mdx$/, ""));
|
||||
|
||||
return slugs;
|
||||
});
|
||||
@@ -94,7 +82,7 @@ export const getPostSlugs = pMemoize(async (): Promise<string[]> => {
|
||||
// returns the parsed front matter of ALL posts, sorted reverse chronologically
|
||||
export const getAllPosts = pMemoize(async (): Promise<FrontMatter[]> => {
|
||||
// for each post, query its front matter
|
||||
const data = await pMap(await getPostSlugs(), async (slug) => (await getPostData(slug)).frontMatter);
|
||||
const data = await pMap(await getPostSlugs(), async (slug) => await getFrontMatter(slug));
|
||||
|
||||
// sort the results by date
|
||||
data.sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
|
||||
|
Reference in New Issue
Block a user