🌈 style: added styles for markdown
This commit is contained in:
56
lib/posts.ts
56
lib/posts.ts
@@ -3,6 +3,12 @@ import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { remark } from 'remark';
|
||||
import html from 'remark-html';
|
||||
import { unified } from 'unified';
|
||||
import remarkParse from 'remark-parse';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import remarkRehype from 'remark-rehype';
|
||||
import rehypeRaw from 'rehype-raw';
|
||||
import rehypeStringify from 'rehype-stringify';
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'posts');
|
||||
|
||||
@@ -46,21 +52,39 @@ export function getSortedPostsData(): Omit<PostData, 'contentHtml'>[] {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getPostData(slug: string): Promise<PostData> {
|
||||
const fullPath = path.join(postsDirectory, `${slug}.md`);
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
||||
const matterResult = matter(fileContents);
|
||||
const processedContent = await remark()
|
||||
.use(html)
|
||||
.process(matterResult.content);
|
||||
const contentHtml = processedContent.toString();
|
||||
export async function getPostData(slug: string): Promise<PostData | null> {
|
||||
try {
|
||||
const fullPath = path.join(postsDirectory, `${slug}.md`);
|
||||
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
console.error(`Файл не найден: ${fullPath}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
||||
const matterResult = matter(fileContents);
|
||||
|
||||
// Используем unified pipeline для более полной поддержки Markdown
|
||||
const processedContent = await unified()
|
||||
.use(remarkParse) // парсим Markdown
|
||||
.use(remarkGfm) // добавляем поддержку GFM (таблицы, задачи и т.д.)
|
||||
.use(remarkRehype, { allowDangerousHtml: true }) // конвертируем в HTML
|
||||
.use(rehypeRaw) // разрешаем raw HTML
|
||||
.use(rehypeStringify) // сериализуем в строку
|
||||
.process(matterResult.content || '');
|
||||
|
||||
const contentHtml = processedContent.toString();
|
||||
|
||||
return {
|
||||
slug,
|
||||
contentHtml,
|
||||
title: matterResult.data.title,
|
||||
date: matterResult.data.date,
|
||||
tags: matterResult.data.tags || [],
|
||||
description: matterResult.data.description || '',
|
||||
};
|
||||
return {
|
||||
slug,
|
||||
contentHtml,
|
||||
title: matterResult.data.title || 'Без названия',
|
||||
date: matterResult.data.date || new Date().toISOString().split('T')[0],
|
||||
tags: matterResult.data.tags || [],
|
||||
description: matterResult.data.description || '',
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Ошибка при загрузке поста ${slug}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user