Next.js Discord

Discord Forum

MD Files in App Dir

Answered
Arinji posted this in #help-forum
Open in Discord
Avatar
ArinjiOP
ok so i have an array of objects in the DocMap file whichh maps a url to a markdown file

export const MINECRAFTDOCS = [
  {
    link: "minecraft-server-management-the-basics-ig1n62",
    docFile: "../DocFiles/minecraft-custom-ip.md",
  },
];

And i am trying to use the markdown file in the page.tsx.
import { MINECRAFTDOCS } from "@/DOCMAPS/minecraft";
import { notFound } from "next/navigation";

async function GetMDFilename(link: string) {
  const filename = MINECRAFTDOCS.find((doc) => doc.link === link);
  if (!filename) notFound();
  const file = await fetch(`${filename.docFile}`);
  return { filename, file };
}

export default async function Page({ params }: { params: { link: string } }) {
  const { filename, file } = await GetMDFilename(params.link);

  console.log(filename, file);

  return (
    <main className=" min-h-[100svh] w-full flex flex-col items-center justify-center bg-shades-darkgrey"></main>
  );
}


But i get a fetch error, how do i do this lol
I cant put it in the public folder btw cause we dont want it so people can just get the md file
Image
Answered by Ray
try this
import fs from "fs/promises";
import path from "path";

const file = await fs.readFile(path.join(process.cwd(), 'src/Docfiles/minecraft-custom-ip.md'));
View full answer

2 Replies

Avatar
Ray
try this
import fs from "fs/promises";
import path from "path";

const file = await fs.readFile(path.join(process.cwd(), 'src/Docfiles/minecraft-custom-ip.md'));
Answer
Avatar
ArinjiOP
works, thanks :D