refactor: code refactor

This commit is contained in:
Andrea
2023-03-14 13:54:52 +01:00
parent 6d3a6c2155
commit 84ed5530ca
19 changed files with 1666 additions and 1023 deletions

View File

@@ -1,21 +0,0 @@
import { PrismaClient } from "@prisma/client";
export let db: PrismaClient;
declare global {
var __db: PrismaClient | undefined;
}
// this is needed because in development we don't want to restart
// the server with every change, but we want to make sure we don't
// create a new connection to the DB with every change either.
if (process.env.NODE_ENV === "production") {
db = new PrismaClient();
db.$connect();
} else {
if (!global.__db) {
global.__db = new PrismaClient();
global.__db.$connect();
}
db = global.__db;
}

View File

@@ -1,12 +1,17 @@
import { remarkCodeHike } from "@code-hike/mdx";
import { readFile } from "fs/promises";
import { readdir, readFile } from "fs/promises";
import { bundleMDX } from "mdx-bundler";
import codeHikeTheme from "shiki/themes/one-dark-pro.json";
export const getMdxFile = async (fileName: string) => {
const source = await readFile(`posts/${fileName}`);
return bundleMDX({
source: source.toString(),
type FrontMatter = {
title: string;
description: string;
publishedAt: string;
};
export const getMdxFile = async (file: string) => {
return bundleMDX<FrontMatter>({
source: (await readFile(`./posts/${file}.mdx`)).toString(),
mdxOptions() {
return {
remarkPlugins: [
@@ -24,3 +29,41 @@ export const getMdxFile = async (fileName: string) => {
},
});
};
export const findPosts = async () => {
const files = await readdir("posts");
const posts: (FrontMatter & {
filename: string;
})[] = [];
for (const file of files.filter((file) => file.endsWith(".mdx"))) {
const { frontmatter } = await bundleMDX({
source: (await readFile(`./posts/${file}`)).toString(),
mdxOptions() {
return {
remarkPlugins: [
[
remarkCodeHike,
{
theme: codeHikeTheme,
lineNumbers: true,
showCopyButton: true,
autoImport: true,
},
],
],
};
},
});
posts.push({
filename: file.replace(".mdx", ""),
description: frontmatter.description,
title: frontmatter.title,
publishedAt: frontmatter.publishedAt,
});
}
return posts.sort((a, b) =>
new Date(a.publishedAt) > new Date(b.publishedAt) ? -1 : 1
);
};