mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-11-05 07:05:40 -05:00
refactor "notes" into "posts" (only on the backend)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Feed } from "feed";
|
||||
import { getAllNotes } from "./parse-notes";
|
||||
import { getAllPosts } from "./posts";
|
||||
import * as config from "../config";
|
||||
import { meJpg } from "../config/favicons";
|
||||
import type { GetServerSideProps } from "next";
|
||||
@@ -40,22 +40,22 @@ export const buildFeed = async (
|
||||
},
|
||||
});
|
||||
|
||||
// add notes separately using their frontmatter
|
||||
const notes = await getAllNotes();
|
||||
notes.forEach((note) => {
|
||||
// add posts separately using their frontmatter
|
||||
const posts = await getAllPosts();
|
||||
posts.forEach((post) => {
|
||||
feed.addItem({
|
||||
guid: note.permalink,
|
||||
link: note.permalink,
|
||||
title: note.title,
|
||||
description: note.description,
|
||||
image: note.image && `${baseUrl}${note.image}`,
|
||||
guid: post.permalink,
|
||||
link: post.permalink,
|
||||
title: post.title,
|
||||
description: post.description,
|
||||
image: post.image && `${baseUrl}${post.image}`,
|
||||
author: [
|
||||
{
|
||||
name: config.authorName,
|
||||
link: `${baseUrl}/`,
|
||||
},
|
||||
],
|
||||
date: new Date(note.date),
|
||||
date: new Date(post.date),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import { serialize } from "next-mdx-remote/serialize";
|
||||
import { getNoteData } from "./parse-notes";
|
||||
|
||||
import type { NoteWithSource } from "../../types";
|
||||
|
||||
// fully parses MDX into JS and returns *everything* about a note
|
||||
export const compileNote = async (slug: string): Promise<NoteWithSource> => {
|
||||
const { frontMatter, content } = await getNoteData(slug);
|
||||
|
||||
const { remarkGfm, remarkSmartypants, remarkUnwrapImages, rehypeSlug, rehypePrism } = await import(
|
||||
"./remark-rehype-plugins"
|
||||
);
|
||||
|
||||
const { compiledSource } = await serialize(content, {
|
||||
parseFrontmatter: false,
|
||||
mdxOptions: {
|
||||
remarkPlugins: [
|
||||
// @ts-ignore
|
||||
[remarkGfm, { singleTilde: false }],
|
||||
[
|
||||
// @ts-ignore
|
||||
remarkSmartypants,
|
||||
{
|
||||
quotes: true,
|
||||
dashes: "oldschool",
|
||||
backticks: false,
|
||||
ellipses: false,
|
||||
},
|
||||
],
|
||||
// @ts-ignore
|
||||
[remarkUnwrapImages],
|
||||
],
|
||||
rehypePlugins: [
|
||||
// @ts-ignore
|
||||
[rehypeSlug],
|
||||
// @ts-ignore
|
||||
[rehypePrism, { ignoreMissing: true }],
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
frontMatter,
|
||||
source: {
|
||||
compiledSource,
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,90 +0,0 @@
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
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 type { NoteFrontMatter } from "../../types";
|
||||
|
||||
export const getNoteSlugs = async (): Promise<string[]> => {
|
||||
// list all .mdx files in "/notes"
|
||||
const mdxFiles = await glob("*.mdx", {
|
||||
cwd: path.join(process.cwd(), "notes"),
|
||||
dot: false,
|
||||
});
|
||||
|
||||
// strip the .mdx extensions from filenames
|
||||
const slugs = mdxFiles.map((fileName) => fileName.replace(/\.mdx$/, ""));
|
||||
|
||||
return slugs;
|
||||
};
|
||||
|
||||
// returns front matter and/or *raw* markdown contents of a given slug
|
||||
export const getNoteData = async (
|
||||
slug: string
|
||||
): Promise<{
|
||||
frontMatter: NoteFrontMatter;
|
||||
content: string;
|
||||
}> => {
|
||||
const fullPath = path.join(process.cwd(), "notes", `${slug}.mdx`);
|
||||
const rawContent = await fs.readFile(fullPath, "utf8");
|
||||
const { data, content } = matter(rawContent);
|
||||
|
||||
const { unified } = await import("unified");
|
||||
const { remarkParse, remarkSmartypants, remarkRehype, rehypeSanitize, rehypeStringify } = await import(
|
||||
"./remark-rehype-plugins"
|
||||
);
|
||||
|
||||
// allow *very* limited markdown to be used in post titles
|
||||
const parseTitle = async (title: string, allowedTags: string[] = []): Promise<string> => {
|
||||
return String(
|
||||
await unified()
|
||||
.use(remarkParse)
|
||||
.use(remarkSmartypants, {
|
||||
quotes: true,
|
||||
dashes: "oldschool",
|
||||
backticks: false,
|
||||
ellipses: false,
|
||||
})
|
||||
.use(remarkRehype)
|
||||
.use(rehypeSanitize, { tagNames: allowedTags })
|
||||
.use(rehypeStringify)
|
||||
.process(title)
|
||||
);
|
||||
};
|
||||
|
||||
// process title as both plain and stylized
|
||||
const [title, htmlTitle] = await Promise.all([
|
||||
parseTitle(data.title),
|
||||
parseTitle(data.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<NoteFrontMatter>),
|
||||
// zero markdown title:
|
||||
title,
|
||||
htmlTitle,
|
||||
slug,
|
||||
permalink: `${process.env.NEXT_PUBLIC_BASE_URL || ""}/notes/${slug}/`,
|
||||
date: formatDate(data.date), // validate/normalize the date string provided from front matter
|
||||
},
|
||||
content,
|
||||
};
|
||||
};
|
||||
|
||||
// returns the parsed front matter of ALL notes, sorted reverse chronologically
|
||||
export const getAllNotes = pMemoize(async (): Promise<NoteFrontMatter[]> => {
|
||||
const slugs = await getNoteSlugs();
|
||||
|
||||
// for each slug, query its front matter
|
||||
const data = await pMap(slugs, async (slug) => (await getNoteData(slug)).frontMatter);
|
||||
|
||||
// sort the results by date
|
||||
data.sort((note1, note2) => (note1.date > note2.date ? -1 : 1));
|
||||
|
||||
return data;
|
||||
});
|
||||
135
lib/helpers/posts.ts
Normal file
135
lib/helpers/posts.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import path from "path";
|
||||
import fs from "fs/promises";
|
||||
import { serialize } from "next-mdx-remote/serialize";
|
||||
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 type { PostFrontMatter, PostWithSource } from "../../types";
|
||||
|
||||
// path to directory with .mdx files, relative to project root
|
||||
export const POSTS_DIR = "./notes";
|
||||
|
||||
// returns front matter and the **raw & uncompiled** markdown of a given slug
|
||||
export const getPostData = async (
|
||||
slug: string
|
||||
): Promise<{
|
||||
frontMatter: PostFrontMatter;
|
||||
markdown: string;
|
||||
}> => {
|
||||
const fullPath = path.join(process.cwd(), POSTS_DIR, `${slug}.mdx`);
|
||||
const rawContent = await fs.readFile(fullPath, "utf8");
|
||||
const { data, content } = matter(rawContent);
|
||||
|
||||
const { unified } = await import("unified");
|
||||
const { remarkParse, remarkSmartypants, remarkRehype, rehypeSanitize, rehypeStringify } = await import(
|
||||
"./remark-rehype-plugins"
|
||||
);
|
||||
|
||||
// allow *very* limited markdown to be used in post titles
|
||||
const parseTitle = async (title: string, allowedTags: string[] = []): Promise<string> => {
|
||||
return String(
|
||||
await unified()
|
||||
.use(remarkParse)
|
||||
.use(remarkSmartypants, {
|
||||
quotes: true,
|
||||
dashes: "oldschool",
|
||||
backticks: false,
|
||||
ellipses: false,
|
||||
})
|
||||
.use(remarkRehype)
|
||||
.use(rehypeSanitize, { tagNames: allowedTags })
|
||||
.use(rehypeStringify)
|
||||
.process(title)
|
||||
);
|
||||
};
|
||||
|
||||
// process title as both plain and stylized
|
||||
const [title, htmlTitle] = await Promise.all([
|
||||
parseTitle(data.title),
|
||||
parseTitle(data.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<PostFrontMatter>),
|
||||
// zero markdown title:
|
||||
title,
|
||||
htmlTitle,
|
||||
slug,
|
||||
permalink: `${process.env.NEXT_PUBLIC_BASE_URL || ""}/${POSTS_DIR}/${slug}/`,
|
||||
date: formatDate(data.date), // validate/normalize the date string provided from front matter
|
||||
},
|
||||
markdown: content,
|
||||
};
|
||||
};
|
||||
|
||||
// fully parses MDX into JS and returns *everything* about a post
|
||||
export const compilePost = async (slug: string): Promise<PostWithSource> => {
|
||||
const { frontMatter, markdown } = await getPostData(slug);
|
||||
|
||||
const { remarkGfm, remarkSmartypants, remarkUnwrapImages, rehypeSlug, rehypePrism } = await import(
|
||||
"./remark-rehype-plugins"
|
||||
);
|
||||
|
||||
const { compiledSource } = await serialize(markdown, {
|
||||
parseFrontmatter: false,
|
||||
mdxOptions: {
|
||||
remarkPlugins: [
|
||||
// @ts-ignore
|
||||
[remarkGfm, { singleTilde: false }],
|
||||
[
|
||||
// @ts-ignore
|
||||
remarkSmartypants,
|
||||
{
|
||||
quotes: true,
|
||||
dashes: "oldschool",
|
||||
backticks: false,
|
||||
ellipses: false,
|
||||
},
|
||||
],
|
||||
// @ts-ignore
|
||||
[remarkUnwrapImages],
|
||||
],
|
||||
rehypePlugins: [
|
||||
// @ts-ignore
|
||||
[rehypeSlug],
|
||||
// @ts-ignore
|
||||
[rehypePrism, { ignoreMissing: true }],
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
frontMatter,
|
||||
source: {
|
||||
compiledSource,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const getPostSlugs = pMemoize(async (): Promise<string[]> => {
|
||||
// list all .mdx files in POSTS_DIR
|
||||
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$/, ""));
|
||||
|
||||
return slugs;
|
||||
});
|
||||
|
||||
// returns the parsed front matter of ALL posts, sorted reverse chronologically
|
||||
export const getAllPosts = pMemoize(async (): Promise<PostFrontMatter[]> => {
|
||||
// for each post, query its front matter
|
||||
const data = await pMap(await getPostSlugs(), async (slug) => (await getPostData(slug)).frontMatter);
|
||||
|
||||
// sort the results by date
|
||||
data.sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
|
||||
|
||||
return data;
|
||||
});
|
||||
Reference in New Issue
Block a user