export const dynamic = "force-dynamic"; import { NextResponse } from 'next/server'; import fs from 'fs'; import path from 'path'; import matter from 'gray-matter'; import { marked } from 'marked'; import DOMPurify from 'dompurify'; import { JSDOM } from 'jsdom'; const postsDirectory = path.join(process.cwd(), 'posts'); const pinnedPath = path.join(postsDirectory, 'pinned.json'); let pinnedSlugs: string[] = []; if (fs.existsSync(pinnedPath)) { try { pinnedSlugs = JSON.parse(fs.readFileSync(pinnedPath, 'utf8')); } catch { pinnedSlugs = []; } } // Function to get file creation date function getFileCreationDate(filePath: string): Date { const stats = fs.statSync(filePath); return stats.birthtime ?? stats.mtime; } async function getPostByPath(filePath: string, relPath: string) { const fileContents = fs.readFileSync(filePath, 'utf8'); const { data, content } = matter(fileContents); const createdAt = getFileCreationDate(filePath); let processedContent = ''; try { marked.setOptions({ gfm: true, breaks: true }); const rawHtml = marked.parse(content); const window = new JSDOM('').window; const purify = DOMPurify(window); processedContent = purify.sanitize(rawHtml as string, { ALLOWED_TAGS: [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'a', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'em', 'strong', 'del', 'hr', 'br', 'img', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'div', 'span', 'iframe' ], ALLOWED_ATTR: [ 'class', 'id', 'style', 'href', 'target', 'rel', 'src', 'alt', 'title', 'width', 'height', 'frameborder', 'allowfullscreen' ], ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i }); } catch (err) { console.error(`Error processing markdown for ${relPath}:`, err); processedContent = `
`; } return { type: 'post', slug: relPath.replace(/\.md$/, ''), title: data.title, date: data.date, tags: data.tags || [], summary: data.summary, content: processedContent, createdAt: createdAt.toISOString(), pinned: pinnedSlugs.includes(relPath.replace(/\.md$/, '')), }; } async function readPostsDir(dir: string, relDir = ''): Promise