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
+15 -12
View File
@@ -1,37 +1,40 @@
import { Feed } from "feed";
import { getAllNotes } from "./parse-notes";
import * as config from "./config";
export const buildFeed = (notes: any[]) => {
export const buildFeed = () => {
const baseURL = config.baseURL || "http://localhost:3000"; // necessary for local testing
const feed = new Feed({
id: `${config.baseURL}/`,
link: `${config.baseURL}/`,
id: `${baseURL}/`,
link: `${baseURL}/`,
title: config.siteName,
description: config.longDescription,
copyright: "https://creativecommons.org/licenses/by/4.0/",
updated: new Date(),
image: `${config.baseURL}/static/images/me.jpg`,
image: `${baseURL}/static/images/me.jpg`,
feedLinks: {
rss: `${config.baseURL}/feed.xml`,
atom: `${config.baseURL}/feed.atom`,
rss: `${baseURL}/feed.xml`,
atom: `${baseURL}/feed.atom`,
},
author: {
name: config.authorName,
link: config.baseURL,
link: baseURL,
email: "jake@jarv.is",
},
});
notes.forEach((note: { title: any; slug: any; description: any; image: any; date: string | number | Date }) => {
const notes = getAllNotes();
notes.forEach((note: any) => {
feed.addItem({
title: note.title,
link: `${config.baseURL}/notes/${note.slug}/`,
guid: `${config.baseURL}/notes/${note.slug}/`,
link: `${baseURL}/notes/${note.slug}/`,
guid: `${baseURL}/notes/${note.slug}/`,
description: note.description,
image: note.image ? `${config.baseURL}${note.image}` : "",
image: note.image ? `${baseURL}${note.image}` : "",
author: [
{
name: config.authorName,
link: config.baseURL,
link: baseURL,
},
],
date: new Date(note.date),
+3
View File
@@ -27,4 +27,7 @@ export const facebookAppId = "3357248167622283";
export const webmentionId = "jarv.is";
export const monetization = "$ilp.uphold.com/BJp6d2FrEB69";
// Next.js constants
export const NOTES_DIR = "notes";
// ...note / TODO: there is still a metric poop ton of this kind of info hard-coded.
+20 -29
View File
@@ -1,38 +1,29 @@
import fs from "fs";
import path from "path";
import matter from "gray-matter";
import { format, parseISO } from "date-fns";
import { NOTES_DIR } from "./config";
export const NOTES_PATH = path.join(process.cwd(), "notes");
export const getNoteData = (file: string) => {
const slug = file.replace(/\.mdx$/, "");
const fullPath = path.join(process.cwd(), NOTES_DIR, `${slug}.mdx`);
const contents = fs.readFileSync(fullPath, "utf8");
const { data } = matter(contents);
export const getNoteSlugs = () => fs.readdirSync(NOTES_PATH);
// Return all md(x) files in NOTES_PATH
export const notePaths = getNoteSlugs().filter((notePath) => /\.mdx?$/.test(notePath));
export const getNoteBySlug = (slug, fields = []) => {
const realSlug = slug.replace(/\.mdx$/, "");
const fullPath = path.join(NOTES_PATH, `${realSlug}.mdx`);
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data, content } = matter(fileContents);
const items = {};
// Ensure only the minimal needed data is exposed
fields.forEach((field) => {
if (field === "slug") {
items[field] = realSlug;
} else if (field === "content") {
items[field] = content;
} else if (typeof data[field] !== "undefined") {
items[field] = data[field];
}
});
return items;
return {
...data,
slug: slug,
date: parseISO(data.date).toISOString(), // validate/normalize the date string provided from front matter
year: parseInt(format(parseISO(data.date), "yyyy")),
};
};
export const getAllNotes = (fields = []) =>
getNoteSlugs()
.map((slug) => getNoteBySlug(slug, fields))
// all .mdx files in NOTES_DIR
export const getNoteFiles = () =>
fs.readdirSync(path.join(process.cwd(), NOTES_DIR)).filter((notePath) => /\.mdx$/.test(notePath));
export const getAllNotes = () =>
getNoteFiles()
.map((file) => getNoteData(file))
// sort notes by date in descending order
.sort((note1: any, note2: any) => (note1.date > note2.date ? -1 : 1));