Skip to content

Instantly share code, notes, and snippets.

@kjmczk
Created April 30, 2021 08:15
Show Gist options
  • Save kjmczk/215478c9c0ac39baaee3220989ab0e10 to your computer and use it in GitHub Desktop.
Save kjmczk/215478c9c0ac39baaee3220989ab0e10 to your computer and use it in GitHub Desktop.
utils/mdxUtils.ts - MDX Blog Simple - Medium
// utils/mdxUtils.ts
import fs from 'fs';
import { join } from 'path';
import matter from 'gray-matter';
type Items = {
[key: string]: string;
};
type Post = {
data: {
[key: string]: string;
};
content: string;
};
const POSTS_PATH = join(process.cwd(), '_posts');
function getPostFilePaths(): string[] {
return (
fs
.readdirSync(POSTS_PATH)
// Only include md(x) files
.filter((path) => /\.mdx?$/.test(path))
);
}
export function getPost(slug: string): Post {
const fullPath = join(POSTS_PATH, `${slug}.mdx`);
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data, content } = matter(fileContents);
return { data, content };
}
export function getPostItems(filePath: string, fields: string[] = []): Items {
const slug = filePath.replace(/\.mdx?$/, '');
const { data, content } = getPost(slug);
const items: Items = {};
// Ensure only the minimal needed data is exposed
fields.forEach((field) => {
if (field === 'slug') {
items[field] = slug;
}
if (field === 'content') {
items[field] = content;
}
if (data[field]) {
items[field] = data[field];
}
});
return items;
}
export function getAllPosts(fields: string[] = []): Items[] {
const filePaths = getPostFilePaths();
const posts = filePaths
.map((filePath) => getPostItems(filePath, fields))
// sort posts by date in descending order
.sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
return posts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment