<commit_before>import { promises as fs } from 'fs';
import path from 'path';

import matter from 'gray-matter';
import { compareDesc } from 'date-fns';
import renderToString from 'next-mdx-remote/render-to-string';

import { postFilePaths, POSTS_PATH } from '~/utils/mdx';
import type { Post } from '~/components/post-card';
import { components } from '~/components/layouts/post';

async function getPost(
  slug: string
): Promise<{
  filePath: string;
  data: Post;
  content: string;
}> {
  const filePath = slug.endsWith('.mdx') ? slug : `${slug}.mdx`;

  const source = await fs.readFile(path.join(POSTS_PATH, filePath), 'utf-8');
  const { content, data } = matter(source);

  return {
    content,
    data: data as Post,
    filePath: filePath.replace(/\.mdx?$/, ''),
  };
}

async function getPosts() {
  const postPromises = postFilePaths.map(filePath => getPost(filePath));

  const posts = await Promise.all(postPromises);

  return posts.sort((a, b) =>
    compareDesc(new Date(a.data.date), new Date(b.data.date))
  );
}<commit_msg>chore: prefix filePath with '/'<commit_after>import { promises as fs } from 'fs';
import path from 'path';

import matter from 'gray-matter';
import { compareDesc } from 'date-fns';
import renderToString from 'next-mdx-remote/render-to-string';

import { postFilePaths, POSTS_PATH } from '~/utils/mdx';
import type { Post } from '~/components/post-card';
import { components } from '~/components/layouts/post';

async function getPost(
  slug: string
): Promise<{
  filePath: string;
  data: Post;
  content: string;
}> {
  const fileName = slug.endsWith('.mdx') ? slug : `${slug}.mdx`;
  const filePath = fileName.startsWith('/') ? fileName : `/${fileName}`;

  const source = await fs.readFile(path.join(POSTS_PATH, filePath), 'utf-8');
  const { content, data } = matter(source);

  return {
    content,
    data: data as Post,
    filePath: filePath.replace(/\.mdx?$/, ''),
  };
}

async function getPosts() {
  const postPromises = postFilePaths.map(filePath => getPost(filePath));

  const posts = await Promise.all(postPromises);

  return posts.sort((a, b) =>
    compareDesc(new Date(a.data.date), new Date(b.data.date))
  );
}
{'fraction_non_alphanumeric': 0.16124599175446633, 'fraction_numerical': 0.0009161704076958314, 'mean_word_length': 4.163120567375887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3897840', 'n_tokens_mistral': 746, 'n_tokens_neox': 731, 'n_words': 187}