1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-11-05 08:45:41 -05:00

attempt to make edge functions a tad bit lighter

This commit is contained in:
2023-07-06 10:37:51 -04:00
parent 2f44d8d227
commit b13c8259b3
17 changed files with 121 additions and 146 deletions

View File

@@ -2,7 +2,6 @@ import { Feed } from "feed";
import { getAllNotes } from "./parse-notes";
import * as config from "../config";
import { meJpg } from "../config/favicons";
import { RELEASE_DATE } from "../config/constants";
import type { GetServerSideProps } from "next";
export type GetServerSideFeedProps = GetServerSideProps<Record<string, never>>;
@@ -22,20 +21,20 @@ export const buildFeed = async (
// https://github.com/jpmonette/feed#example
const feed = new Feed({
id: `${config.baseUrl}/`,
link: `${config.baseUrl}/`,
id: `${process.env.BASE_URL}/`,
link: `${process.env.BASE_URL}/`,
title: config.siteName,
description: config.longDescription,
copyright: config.licenseUrl,
updated: new Date(RELEASE_DATE),
image: `${config.baseUrl}${meJpg.src}`,
updated: new Date(process.env.RELEASE_DATE || Date.now()),
image: `${process.env.BASE_URL}${meJpg.src}`,
feedLinks: {
rss: `${config.baseUrl}/feed.xml`,
atom: `${config.baseUrl}/feed.atom`,
rss: `${process.env.BASE_URL}/feed.xml`,
atom: `${process.env.BASE_URL}/feed.atom`,
},
author: {
name: config.authorName,
link: `${config.baseUrl}/`,
link: `${process.env.BASE_URL}/`,
email: config.authorEmail,
},
});
@@ -48,11 +47,11 @@ export const buildFeed = async (
link: note.permalink,
title: note.title,
description: note.description,
image: note.image && `${config.baseUrl}${note.image}`,
image: note.image && `${process.env.BASE_URL}${note.image}`,
author: [
{
name: config.authorName,
link: `${config.baseUrl}/`,
link: `${process.env.BASE_URL}/`,
},
],
date: new Date(note.date),

View File

@@ -1,7 +1,6 @@
import { serialize } from "next-mdx-remote/serialize";
import { minify } from "uglify-js";
import { getNoteData } from "./parse-notes";
import { IS_DEV_SERVER } from "../config/constants";
// remark/rehype markdown plugins
import remarkGfm from "remark-gfm";
@@ -38,14 +37,15 @@ export const compileNote = async (slug: string): Promise<NoteWithSource> => {
// TODO: next-mdx-remote v4 doesn't (yet?) minify compiled JSX output, see:
// https://github.com/hashicorp/next-mdx-remote/pull/211#issuecomment-1013658514
// ...so for now, let's do it manually (and conservatively) with uglify-js when building for production.
const compiledSource = IS_DEV_SERVER
? source.compiledSource
: minify(source.compiledSource, {
toplevel: true,
parse: {
bare_returns: true,
},
}).code;
const compiledSource =
process.env.NODE_ENV === "production"
? minify(source.compiledSource, {
toplevel: true,
parse: {
bare_returns: true,
},
}).code
: source.compiledSource;
return {
frontMatter,

View File

@@ -5,19 +5,20 @@ import pMap from "p-map";
import pMemoize from "p-memoize";
import matter from "gray-matter";
import removeMarkdown from "remove-markdown";
import { marked } from "marked";
// @ts-ignore
import { markedSmartypants } from "marked-smartypants";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import remarkSmartypants from "remark-smartypants";
import { formatDate } from "./format-date";
import { baseUrl } from "../config";
import { NOTES_DIR } from "../config/constants";
import type { NoteFrontMatter } from "../../types";
import rehypeSanitize from "rehype-sanitize";
export const getNoteSlugs = async (): Promise<string[]> => {
// list all .mdx files in NOTES_DIR
// list all .mdx files in "/notes"
const mdxFiles = await glob("*.mdx", {
cwd: path.join(process.cwd(), NOTES_DIR),
cwd: path.join(process.cwd(), "notes"),
dot: false,
});
@@ -34,13 +35,25 @@ export const getNoteData = async (
frontMatter: NoteFrontMatter;
content: string;
}> => {
const fullPath = path.join(process.cwd(), NOTES_DIR, `${slug}.mdx`);
const fullPath = path.join(process.cwd(), "notes", `${slug}.mdx`);
const rawContent = await fs.readFile(fullPath, "utf8");
const { data, content } = matter(rawContent);
// attach marked extensions:
// https://marked.js.org/using_advanced#extensions
marked.use(markedSmartypants());
// allow *very* limited markdown to be used in post titles
const htmlTitle = String(
await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeSanitize, { tagNames: ["code", "em", "strong"] })
.use(remarkSmartypants, {
quotes: true,
dashes: "oldschool",
backticks: false,
ellipses: false,
})
.use(rehypeStringify, { allowDangerousHtml: true })
.process(data.title)
);
// return both the parsed YAML front matter (with a few amendments) and the raw, unparsed markdown content
return {
@@ -48,15 +61,9 @@ export const getNoteData = async (
...(data as Partial<NoteFrontMatter>),
// zero markdown title:
title: removeMarkdown(data.title),
// allow markdown formatting to appear in post titles in some places (rarely used):
htmlTitle: marked.parseInline(data.title, {
silent: true,
// these are deprecated and throw very noisy warnings but are still defaults, make it make sense...
mangle: false,
headerIds: false,
}),
htmlTitle,
slug,
permalink: `${baseUrl}/${NOTES_DIR}/${slug}/`,
permalink: `${process.env.BASE_URL}/notes/${slug}/`,
date: formatDate(data.date), // validate/normalize the date string provided from front matter
},
content,