generate permalinks when parsing note

This commit is contained in:
2022-01-03 16:26:20 -05:00
parent 6d81939b8b
commit d2b71887b4
5 changed files with 16 additions and 111 deletions
+4 -3
View File
@@ -2,17 +2,18 @@ import fs from "fs";
import path from "path";
import matter from "gray-matter";
import { format, parseISO } from "date-fns";
import { NOTES_DIR } from "./config";
import * as config from "./config";
export const getNoteData = (file: string) => {
const slug = file.replace(/\.mdx$/, "");
const fullPath = path.join(process.cwd(), NOTES_DIR, `${slug}.mdx`);
const fullPath = path.join(process.cwd(), config.NOTES_DIR, `${slug}.mdx`);
const contents = fs.readFileSync(fullPath, "utf8");
const { data } = matter(contents);
return {
...data,
slug,
permalink: `${config.baseUrl}/notes/${slug}/`,
date: parseISO(data.date).toISOString(), // validate/normalize the date string provided from front matter
year: parseInt(format(parseISO(data.date), "yyyy")), // parse years here so it's easier to group them on list page
};
@@ -20,7 +21,7 @@ export const getNoteData = (file: string) => {
// all .mdx files in NOTES_DIR
export const getNoteFiles = () =>
fs.readdirSync(path.join(process.cwd(), NOTES_DIR)).filter((notePath) => /\.mdx$/.test(notePath));
fs.readdirSync(path.join(process.cwd(), config.NOTES_DIR)).filter((notePath) => /\.mdx$/.test(notePath));
export const getAllNotes = () =>
getNoteFiles()