mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-26 18:48:28 -04:00
further optimize heavier note parsing functions
This commit is contained in:
parent
cace19db4c
commit
c134ae76fd
@ -1,6 +1,8 @@
|
|||||||
import { cache } from "react";
|
import { cache } from "react";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import glob from "fast-glob";
|
import glob from "fast-glob";
|
||||||
|
import { unified } from "unified";
|
||||||
|
import { remarkParse, remarkSmartypants, remarkRehype, rehypeSanitize, rehypeStringify } from "./remark-rehype-plugins";
|
||||||
import { decode } from "html-entities";
|
import { decode } from "html-entities";
|
||||||
import { formatDate } from "./format-date";
|
import { formatDate } from "./format-date";
|
||||||
import { BASE_URL, POSTS_DIR } from "../config/constants";
|
import { BASE_URL, POSTS_DIR } from "../config/constants";
|
||||||
@ -19,21 +21,10 @@ export type FrontMatter = {
|
|||||||
|
|
||||||
// returns front matter and the **raw & uncompiled** markdown of a given slug
|
// returns front matter and the **raw & uncompiled** markdown of a given slug
|
||||||
export const getFrontMatter = async (slug: string): Promise<FrontMatter> => {
|
export const getFrontMatter = async (slug: string): Promise<FrontMatter> => {
|
||||||
if (!(await getPostSlugs()).includes(slug)) {
|
|
||||||
throw new Error(`No post found for slug: ${slug}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { frontmatter } = await import(`../../${POSTS_DIR}/${slug}/index.mdx`);
|
const { frontmatter } = await import(`../../${POSTS_DIR}/${slug}/index.mdx`);
|
||||||
|
|
||||||
const { unified } = await import("unified");
|
// create a reusable processor for titles
|
||||||
const { remarkParse, remarkSmartypants, remarkRehype, rehypeSanitize, rehypeStringify } = await import(
|
const titleProcessor = unified()
|
||||||
"./remark-rehype-plugins"
|
|
||||||
);
|
|
||||||
|
|
||||||
// allow *very* limited markdown to be used in post titles
|
|
||||||
const parseTitle = async (title: string, allowedTags: string[] = []): Promise<string> => {
|
|
||||||
const newTitle = (
|
|
||||||
await unified()
|
|
||||||
.use(remarkParse)
|
.use(remarkParse)
|
||||||
.use(remarkSmartypants, {
|
.use(remarkSmartypants, {
|
||||||
quotes: true,
|
quotes: true,
|
||||||
@ -42,19 +33,24 @@ export const getFrontMatter = async (slug: string): Promise<FrontMatter> => {
|
|||||||
ellipses: false,
|
ellipses: false,
|
||||||
})
|
})
|
||||||
.use(remarkRehype)
|
.use(remarkRehype)
|
||||||
.use(rehypeSanitize, { tagNames: allowedTags })
|
.use(rehypeStringify);
|
||||||
.use(rehypeStringify)
|
|
||||||
.process(title)
|
|
||||||
).toString();
|
|
||||||
|
|
||||||
// assume if we don't want any html titles then we don't want encoded html entities either
|
|
||||||
return allowedTags.length === 0 ? decode(newTitle) : newTitle;
|
|
||||||
};
|
|
||||||
|
|
||||||
// process title as both plain and stylized
|
// process title as both plain and stylized
|
||||||
const [title, htmlTitle] = await Promise.all([
|
const [title, htmlTitle] = await Promise.all([
|
||||||
parseTitle(frontmatter.title),
|
titleProcessor()
|
||||||
parseTitle(frontmatter.title, ["code", "em", "strong"]),
|
.use(rehypeSanitize, {
|
||||||
|
// strip all resulting html tags from the markdown title
|
||||||
|
tagNames: [],
|
||||||
|
})
|
||||||
|
.process(frontmatter.title)
|
||||||
|
.then((result) => decode(String(result))),
|
||||||
|
titleProcessor()
|
||||||
|
.use(rehypeSanitize, {
|
||||||
|
// allow *very* limited markdown to be used in post titles
|
||||||
|
tagNames: ["code", "em", "strong"],
|
||||||
|
})
|
||||||
|
.process(frontmatter.title)
|
||||||
|
.then((result) => String(result)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// return both the parsed YAML front matter (with a few amendments) and the raw, unparsed markdown content
|
// return both the parsed YAML front matter (with a few amendments) and the raw, unparsed markdown content
|
||||||
@ -73,7 +69,7 @@ export const getFrontMatter = async (slug: string): Promise<FrontMatter> => {
|
|||||||
// use filesystem to get a simple list of all post slugs
|
// use filesystem to get a simple list of all post slugs
|
||||||
export const getPostSlugs = cache(async (): Promise<string[]> => {
|
export const getPostSlugs = cache(async (): Promise<string[]> => {
|
||||||
// list all .mdx files in POSTS_DIR
|
// list all .mdx files in POSTS_DIR
|
||||||
const mdxFiles = await glob("**/*.mdx", {
|
const mdxFiles = await glob("*/index.mdx", {
|
||||||
cwd: path.join(process.cwd(), POSTS_DIR),
|
cwd: path.join(process.cwd(), POSTS_DIR),
|
||||||
dot: false,
|
dot: false,
|
||||||
});
|
});
|
||||||
@ -88,10 +84,8 @@ export const getPostSlugs = cache(async (): Promise<string[]> => {
|
|||||||
export const getAllPosts = cache(async (): Promise<FrontMatter[]> => {
|
export const getAllPosts = cache(async (): Promise<FrontMatter[]> => {
|
||||||
// concurrently fetch the front matter of each post
|
// concurrently fetch the front matter of each post
|
||||||
const slugs = await getPostSlugs();
|
const slugs = await getPostSlugs();
|
||||||
const data = await Promise.all(slugs.map(async (slug) => await getFrontMatter(slug)));
|
const posts = await Promise.all(slugs.map(getFrontMatter));
|
||||||
|
|
||||||
// sort the results by date
|
// sort the results reverse chronologically and return
|
||||||
data.sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
|
return posts.sort((post1, post2) => new Date(post1.date).getTime() - new Date(post2.date).getTime());
|
||||||
|
|
||||||
return data;
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user