This commit is contained in:
2025-06-22 14:30:37 +02:00
parent 15afa15794
commit 309b5a47df
7 changed files with 306 additions and 42 deletions

View File

@@ -47,15 +47,29 @@ export async function POST(request: Request) {
}
}
export async function GET(request: Request) {
// Return the current pinned.json object
try {
const pinnedPath = path.join(postsDirectory, 'pinned.json');
let pinnedData = { pinned: [], folderEmojis: {} };
if (fs.existsSync(pinnedPath)) {
pinnedData = JSON.parse(fs.readFileSync(pinnedPath, 'utf8'));
}
return NextResponse.json(pinnedData);
} catch (error) {
return NextResponse.json({ error: 'Error reading pinned.json' }, { status: 500 });
}
}
export async function PATCH(request: Request) {
try {
const body = await request.json();
const { pinned } = body; // expects an array of slugs
if (!Array.isArray(pinned)) {
return NextResponse.json({ error: 'Invalid pinned data' }, { status: 400 });
const { pinned, folderEmojis } = body; // expects pinned (array) and folderEmojis (object)
if (!Array.isArray(pinned) || typeof folderEmojis !== 'object') {
return NextResponse.json({ error: 'Invalid pinned or folderEmojis data' }, { status: 400 });
}
const pinnedPath = path.join(postsDirectory, 'pinned.json');
fs.writeFileSync(pinnedPath, JSON.stringify(pinned, null, 2), 'utf8');
fs.writeFileSync(pinnedPath, JSON.stringify({ pinned, folderEmojis }, null, 2), 'utf8');
return NextResponse.json({ success: true });
} catch (error) {
console.error('Error updating pinned.json:', error);

View File

@@ -13,12 +13,15 @@ import { getPostsDirectory } from '@/lib/postsDirectory';
const postsDirectory = getPostsDirectory();
const pinnedPath = path.join(postsDirectory, 'pinned.json');
let pinnedSlugs: string[] = [];
let pinnedData: { pinned: string[]; folderEmojis: Record<string, string> } = { pinned: [], folderEmojis: {} };
if (fs.existsSync(pinnedPath)) {
try {
pinnedSlugs = JSON.parse(fs.readFileSync(pinnedPath, 'utf8'));
const raw = fs.readFileSync(pinnedPath, 'utf8');
pinnedData = JSON.parse(raw);
if (!pinnedData.pinned) pinnedData.pinned = [];
if (!pinnedData.folderEmojis) pinnedData.folderEmojis = {};
} catch {
pinnedSlugs = [];
pinnedData = { pinned: [], folderEmojis: {} };
}
}
@@ -102,7 +105,7 @@ async function getPostByPath(filePath: string, relPath: string) {
summary: data.summary,
content: processedContent,
createdAt: createdAt.toISOString(),
pinned: pinnedSlugs.includes(relPath.replace(/\.md$/, '')),
pinned: pinnedData.pinned.includes(relPath.replace(/\.md$/, '')),
};
}
@@ -117,7 +120,8 @@ async function readPostsDir(dir: string, relDir = ''): Promise<any[]> {
if (entry.isDirectory()) {
const children = await readPostsDir(fullPath, relPath);
folders.push({ type: 'folder', name: entry.name, path: relPath, children });
const emoji = pinnedData.folderEmojis[relPath] || '📁';
folders.push({ type: 'folder', name: entry.name, path: relPath, emoji, children });
} else if (entry.isFile() && entry.name.endsWith('.md')) {
posts.push(await getPostByPath(fullPath, relPath));
}