refactor: try another way from previous commit

This commit is contained in:
Andrea 2023-10-21 19:44:26 +02:00
parent e5287c4618
commit e030f94953
No known key found for this signature in database
GPG Key ID: 4594610B9C8F91C5

View File

@ -1,7 +1,6 @@
import { remarkCodeHike } from "@code-hike/mdx"; import { remarkCodeHike } from "@code-hike/mdx";
import { readdir, readFile } from "fs/promises"; import { readdir, readFile } from "fs/promises";
import { bundleMDX } from "mdx-bundler"; import { bundleMDX } from "mdx-bundler";
import path from "path";
import codeHikeTheme from "shiki/themes/one-dark-pro.json"; import codeHikeTheme from "shiki/themes/one-dark-pro.json";
type FrontMatter = { type FrontMatter = {
@ -12,9 +11,7 @@ type FrontMatter = {
export const getMdxFile = async (file: string) => { export const getMdxFile = async (file: string) => {
return bundleMDX<FrontMatter>({ return bundleMDX<FrontMatter>({
source: ( source: (await readFile(`${__dirname}/../posts/${file}.mdx`)).toString(),
await readFile(path.join(process.cwd(), `./posts/${file}.mdx`))
).toString(),
mdxOptions(options) { mdxOptions(options) {
return { return {
rehypePlugins: [...(options.rehypePlugins ?? [])], rehypePlugins: [...(options.rehypePlugins ?? [])],
@ -36,15 +33,13 @@ export const getMdxFile = async (file: string) => {
}; };
export const findPosts = async () => { export const findPosts = async () => {
const files = await readdir(path.join(process.cwd(), "./posts")); const files = await readdir(`${__dirname}/../posts`);
const posts: (FrontMatter & { const posts: (FrontMatter & {
filename: string; filename: string;
})[] = []; })[] = [];
for (const file of files.filter((file) => file.endsWith(".mdx"))) { for (const file of files.filter((file) => file.endsWith(".mdx"))) {
const { frontmatter } = await bundleMDX<FrontMatter>({ const { frontmatter } = await bundleMDX<FrontMatter>({
source: ( source: (await readFile(`${__dirname}/../posts/${file}`)).toString(),
await readFile(path.join(process.cwd(), `./posts/${file}`))
).toString(),
mdxOptions() { mdxOptions() {
return { return {
remarkPlugins: [ remarkPlugins: [