Skip to content

⬅️ Back to Table of Contents

📄 recent-blog-posts

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 4
📊 Variables & Constants 1
⚡ Async/Await Patterns 1

📚 Table of Contents

🛠️ File Location:

📂 packages/website/plugins/recent-blog-posts/index.ts

📦 Imports

Name Source
Plugin @docusaurus/types
matter gray-matter
readingTime reading-time
z zod

Variables & Constants

Name Type Kind Value Exported
storageFile "data/recent-blog-posts.json" const data/recent-blog-posts.json

Async/Await Patterns

Type Function Await Expressions Promise Chains
async-function blogPluginEnhanced fs.readdir('blog', { withFileTypes: true }), Promise.all( blogHandles.map(asy... Promise.all

Functions

blogPluginEnhanced(): Promise<Plugin>

Returns: Promise<Plugin>

Calls:

  • (await fs.readdir('blog', { withFileTypes: true })) .filter(handle => handle.isFile() && /.mdx?$/.test(handle.name)) .map(handle => ({ date: new Date(/\d+-\d+-\d+/.exec(handle.name)?.[0] || '1970-01-01'), handle, })) .sort((a, b) => b.date.getTime() - a.date.getTime()) .slice
  • Promise.all
  • blogHandles.map
  • fs.readFile
  • matterSchema.parse
  • matter (from gray-matter)
  • Math.round
  • readingTime (from reading-time)
  • fs.writeFile
  • JSON.stringify
Code
export default async function blogPluginEnhanced(): Promise<Plugin> {
  const blogHandles = (await fs.readdir('blog', { withFileTypes: true }))
    .filter(handle => handle.isFile() && /.mdx?$/.test(handle.name))
    .map(handle => ({
      date: new Date(/\d+-\d+-\d+/.exec(handle.name)?.[0] || '1970-01-01'),
      handle,
    }))
    .sort((a, b) => b.date.getTime() - a.date.getTime())
    .slice(0, 3);

  const blogPosts = await Promise.all(
    blogHandles.map(async ({ date, handle }) => {
      const content = await fs.readFile(`blog/${handle.name}`, 'utf-8');
      const data = matterSchema.parse(matter(content).data);

      return {
        date,
        description: data.description,
        readingTime: Math.round(readingTime(content).minutes),
        slug: data.slug,
        title: data.title,
      };
    }),
  );

  await fs.writeFile(storageFile, JSON.stringify(blogPosts, null, 2));

  return {
    name: 'recent-blog-posts',
  };
}

Generated by Syntax Scribe