feat(blogs): 新增多语言博客内容及图片资源

添加英文、日文和中文博客文章,包括1.mdx、2.mdx和3.mdx文件
新增博客相关图片资源到public/images目录
This commit is contained in:
砂糖
2025-12-09 17:34:49 +08:00
parent de67af263e
commit 1d23c01990
21 changed files with 348 additions and 184 deletions

View File

@@ -4,9 +4,11 @@ import { Locale, LOCALES } from "@/i18n/routing";
import { getPosts } from "@/lib/getBlogs";
import { constructMetadata } from "@/lib/metadata";
import { BlogPost } from "@/types/blog";
import fs from "fs/promises";
import { Metadata } from "next";
import { MDXRemote } from "next-mdx-remote-client/rsc";
import { notFound } from "next/navigation";
import path from "path";
type Params = Promise<{
locale: string;
@@ -17,14 +19,112 @@ type MetadataProps = {
params: Params;
};
async function getMDXContent(locale: string, section: string): Promise<BlogPost> {
const filePath = path.join(
process.cwd(),
"blogs",
locale,
`${section}.mdx`
);
try {
const content = await fs.readFile(filePath, "utf-8");
// 解析MDX文件的frontmatter和内容
const { frontmatter, content: postContent } = parseMDXContent(content);
// 构建BlogPost对象
const blogPost: BlogPost = {
locale,
title: frontmatter.title || '',
description: frontmatter.description,
image: frontmatter.image,
slug: frontmatter.slug || '',
tags: frontmatter.tags,
// 解析日期
date: frontmatter.date ? new Date(frontmatter.date) : new Date(),
// 设置visible默认值为published
visible: frontmatter.visible || 'published',
// 处理pin字段 - 如果有pin标记则为true否则为false
pin: frontmatter.pin === 'pin',
// 去除frontmatter后的内容
content: postContent.trim(),
// 所有frontmatter作为metadata
metadata: { ...frontmatter }
};
return blogPost;
} catch (error) {
console.error(`Error reading MDX file: ${error}`);
// 返回默认的BlogPost对象符合类型要求
return {
title: '',
slug: '',
date: new Date(),
content: '',
visible: 'published',
pin: false,
metadata: {},
locale
};
}
}
/**
* 解析MDX内容提取frontmatter和正文
*/
function parseMDXContent(content: string): {
frontmatter: Record<string, any>
content: string
} {
// 匹配frontmatter的正则表达式---开头和结尾)
const frontmatterRegex = /^---\s*[\r\n]([\s\S]*?)[\r\n]---\s*[\r\n]/;
const match = frontmatterRegex.exec(content);
let frontmatter: Record<string, any> = {};
let postContent = content;
if (match && match[1]) {
// 提取frontmatter部分并解析
const frontmatterStr = match[1];
postContent = content.slice(match[0].length);
// 解析frontmatter的每一行
const lines = frontmatterStr.split(/[\r\n]+/);
lines.forEach(line => {
// 跳过空行和注释
if (!line.trim() || line.trim().startsWith('#')) return;
// 分割键值对(支持值中有冒号的情况)
const colonIndex = line.indexOf(':');
if (colonIndex === -1) return;
const key = line.substring(0, colonIndex).trim();
let value = line.substring(colonIndex + 1).trim();
// 移除值的引号(如果有)
if ((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))) {
value = value.substring(1, value.length - 1);
}
frontmatter[key] = value;
});
}
return { frontmatter, content: postContent };
}
export async function generateMetadata({
params,
}: MetadataProps): Promise<Metadata> {
const { locale, slug } = await params;
let { posts }: { posts: BlogPost[] } = await getPosts(locale);
const post = posts.find((post) => post.slug === slug);
let post = await getMDXContent(locale, slug);
console.log(post, 'post');
console.log(post, posts);
if (!post) {
return constructMetadata({
title: "404",
@@ -38,9 +138,9 @@ export async function generateMetadata({
return constructMetadata({
page: "blog",
title: post.title,
description: post.description,
images: post.image ? [post.image] : [],
title: post.title || '',
description: post.description || '',
images: [],
locale: locale as Locale,
path: `/blog/${slug}`,
canonicalUrl: `/blog/${slug}`,
@@ -49,10 +149,9 @@ export async function generateMetadata({
export default async function BlogPage({ params }: { params: Params }) {
const { locale, slug } = await params;
let { posts }: { posts: BlogPost[] } = await getPosts(locale);
const post = posts.find((item) => item.slug === slug);
let post = await getMDXContent(locale, slug);
console.log(post);
if (!post) {
return notFound();
}