Advertisement
Guest User

index.js

a guest
Nov 20th, 2023
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { compileMDX } from 'next-mdx-remote/rsc'
  4. import CodeBlock from "../../app/elements/CodeBlock";
  5. import InfoPanel from "../../app/elements/InfoPanel";
  6.  
  7. const rootDirectory = path.join(process.cwd(), 'src/app', 'mdx-pages')
  8.  
  9. const components = {
  10.   // Define components here
  11.   CodeBlock,
  12.   InfoPanel,
  13. };
  14.  
  15.  
  16. export const getPostBySlug = async slug => {
  17.   var realSlug = slug.replace(/\.mdx$/, '')
  18.   const filePath = path.join(rootDirectory, `${realSlug}.mdx`)
  19.  
  20.   const fileContent = fs.readFileSync(filePath, { encoding: 'utf8' })
  21.  
  22.   const { frontmatter, content } = await compileMDX({
  23.     source: fileContent,
  24.     options: { parseFrontmatter: true },
  25.     components: {components}
  26.   })
  27.  
  28.   return { meta: { ...frontmatter, slug: realSlug }, content }
  29. }
  30.  
  31. export const getAllPostsMeta = async () => {
  32.   const files = fs.readdirSync(rootDirectory)
  33.  
  34.   let posts = []
  35.  
  36.   for (const file of files) {
  37.     const { meta } = await getPostBySlug(file)
  38.     posts.push(meta)
  39.   }
  40.  
  41.   return posts
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement