1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-17 11:35:32 -04: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

@@ -1,13 +0,0 @@
// Next.js constants (not needed in frontend)
// directory containing .mdx files relative to project root
export const NOTES_DIR = "notes";
// normalize the timestamp saved when building/deploying (see next.config.js) and fall back to right now
export const RELEASE_DATE = new Date(process.env.RELEASE_DATE || Date.now()).toISOString();
// detect if running locally via `next dev` (phase is checked in next.config.js)
export const IS_DEV_SERVER = process.env.IS_DEV_SERVER === "true";
// attempt to normalize the various environment flags
export const BUILD_ENV = process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.NODE_ENV;

View File

@@ -6,7 +6,6 @@ module.exports = {
siteDomain: "jarv.is",
siteLocale: "en-US",
timeZone: "America/New_York", // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
baseUrl: process.env.BASE_URL || "", // see next.config.js
onionDomain: "http://jarvis2i2vp4j4tbxjogsnqdemnte5xhzyi7hziiyzxwge3hzmh57zad.onion",
shortDescription: "Front-End Web Developer in Boston, MA",
longDescription:

View File

@@ -17,7 +17,7 @@ export const defaultSeo: DefaultSeoProps = {
type: "website",
images: [
{
url: `${config.baseUrl}${meJpg.src}`,
url: `${process.env.BASE_URL}${meJpg.src}`,
alt: `${config.siteName} ${config.shortDescription}`,
},
],
@@ -103,9 +103,9 @@ export const defaultSeo: DefaultSeoProps = {
export const socialProfileJsonLd: SocialProfileJsonLdProps = {
type: "Person",
name: config.authorName,
url: `${config.baseUrl}/`,
url: `${process.env.BASE_URL}/`,
sameAs: [
`${config.baseUrl}/`,
`${process.env.BASE_URL}/`,
`https://github.com/${config.authorSocial?.github}`,
`https://keybase.io/${config.authorSocial?.keybase}`,
`https://twitter.com/${config.authorSocial?.twitter}`,
@@ -122,5 +122,5 @@ export const socialProfileJsonLd: SocialProfileJsonLdProps = {
export const articleJsonLd: Pick<ArticleJsonLdProps, "authorName" | "publisherName" | "publisherLogo"> = {
authorName: [config.authorName],
publisherName: config.siteName,
publisherLogo: `${config.baseUrl}${meJpg.src}`,
publisherLogo: `${process.env.BASE_URL}${meJpg.src}`,
};

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,