import { NextRequest, NextResponse } from 'next/server' import prisma from '@/lib/prisma' import { auth } from '@/auth' function extractBlockContent(html: string, blockId: string): string | null { const regex = new RegExp( `<(?:p|h[1-6]|blockquote)[^>]*data-id="${blockId}"[^>]*>([\\s\\S]*?)<\\/(?:p|h[1-6]|blockquote)>`, 'i' ) const match = regex.exec(html) if (!match) return null return match[1].replace(/<[^>]+>/g, '').trim() } // GET /api/blocks/[blockId]/status?sourceNoteId=xxx export async function GET( request: NextRequest, { params }: { params: Promise<{ blockId: string }> } ) { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { blockId } = await params const sourceNoteId = request.nextUrl.searchParams.get('sourceNoteId') if (!sourceNoteId) { return NextResponse.json({ error: 'sourceNoteId required' }, { status: 400 }) } const note = await prisma.note.findFirst({ where: { id: sourceNoteId, userId: session.user.id }, select: { id: true, title: true, content: true }, }) if (!note) { return NextResponse.json({ exists: false, content: '', sourceNoteTitle: '' }) } const content = extractBlockContent(note.content, blockId) if (content === null) { return NextResponse.json({ exists: false, content: '', sourceNoteTitle: note.title || '' }) } return NextResponse.json({ exists: true, content, sourceNoteTitle: note.title || 'Sans titre', }) }