1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 15:16:40 -04:00

simplify note parsing logic

This commit is contained in:
2022-01-02 23:05:51 -05:00
parent ca614e1a1a
commit 49fb053649
15 changed files with 164 additions and 154 deletions

View File

@ -1,14 +1,12 @@
import { getAllNotes } from "../lib/parse-notes";
import { buildFeed } from "../lib/build-feed";
import type { GetServerSideProps } from "next";
const AtomPage = () => null;
export const getServerSideProps: GetServerSideProps = async (context) => {
const notes = getAllNotes(["title", "date", "image", "slug", "description"]);
const feed = buildFeed(notes);
const feed = buildFeed();
const { res } = context;
res.setHeader("content-type", "application/atom+xml");
res.write(feed.atom1());
res.end();

View File

@ -1,14 +1,12 @@
import { getAllNotes } from "../lib/parse-notes";
import { buildFeed } from "../lib/build-feed";
import type { GetServerSideProps } from "next";
const RssPage = () => null;
export const getServerSideProps: GetServerSideProps = async (context) => {
const notes = getAllNotes(["title", "date", "image", "slug", "description"]);
const feed = buildFeed(notes);
const feed = buildFeed();
const { res } = context;
res.setHeader("content-type", "application/rss+xml");
res.write(feed.rss2());
res.end();

View File

@ -8,8 +8,8 @@ import Layout from "../../components/Layout";
import Container from "../../components/Container";
import Content from "../../components/Content";
import Meta from "../../components/notes/Meta";
import { notePaths, NOTES_PATH } from "../../lib/parse-notes";
import mdxComponents from "../../components/mdxComponents";
import { getNoteFiles } from "../../lib/parse-notes";
import * as config from "../../lib/config";
import type { GetStaticProps, GetStaticPaths } from "next";
@ -59,7 +59,9 @@ const Note = ({ source, frontMatter, slug }) => (
<Container>
<Meta {...frontMatter} slug={slug} />
<Content>
<MDXRemote {...source} components={mdxComponents} />
<div className="markdown">
<MDXRemote {...source} components={mdxComponents} />
</div>
</Content>
</Container>
</Layout>
@ -67,7 +69,7 @@ const Note = ({ source, frontMatter, slug }) => (
);
export const getStaticProps: GetStaticProps = async ({ params }) => {
const filePath = path.join(NOTES_PATH, `${params.slug}.mdx`);
const filePath = path.join(process.cwd(), config.NOTES_DIR, `${params.slug}.mdx`);
const source = fs.readFileSync(filePath);
const { content, data } = matter(source);
@ -98,7 +100,7 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
};
export const getStaticPaths: GetStaticPaths = async () => {
const paths = notePaths
const paths = getNoteFiles()
// Remove file extensions for page paths
.map((notePath) => notePath.replace(/\.mdx?$/, ""))
// Map the path into the static paths object required by Next.js

View File

@ -1,28 +1,23 @@
import { format, parseISO } from "date-fns";
import groupBy from "lodash.groupby";
import Layout from "../../components/Layout";
import Container from "../../components/Container";
import List from "../../components/notes/List";
import { getAllNotes } from "../../lib/parse-notes";
import type { GetStaticProps } from "next";
const Notes = ({ notesByYear }) => (
const Notes = ({ notes }) => (
<Layout>
<Container title="Notes" description="Recent posts by Jake Jarvis.">
<List notesByYear={notesByYear} />
<List notes={notes} />
</Container>
</Layout>
);
export const getStaticProps: GetStaticProps = async () => {
const allNotes = getAllNotes(["date", "slug", "title"]);
// parse year of each note
allNotes.map((note: any) => (note.year = parseInt(format(parseISO(note.date), "yyyy"))));
const notes = getAllNotes();
return {
props: {
notesByYear: groupBy(allNotes, "year"),
notes,
},
};
};