This commit is contained in:
2026-02-26 15:14:13 +08:00
parent eb68553baf
commit 911c669d81
7 changed files with 278 additions and 22 deletions
+18 -3
View File
@@ -2,6 +2,7 @@
import { prisma } from '@/lib/prisma';
import type { Prisma } from '@prisma/client';
import { requireApiAuth } from '@/lib/api-auth';
import { cleanupAccidentalStandaloneInlineCode, markdownToImportHtml } from '@/lib/markdown-import';
type PageRef = { id: string; parentId: string | null };
@@ -119,11 +120,23 @@ export async function PUT(
updateData.title = trimmed || '无标题';
}
if (body.content !== undefined) {
if (typeof body.content !== 'string') {
if (body.content !== undefined || body.importMarkdown !== undefined) {
if (body.content !== undefined && typeof body.content !== 'string') {
return NextResponse.json({ error: 'Invalid content' }, { status: 400 });
}
updateData.content = body.content;
if (body.importMarkdown !== undefined && typeof body.importMarkdown !== 'string') {
return NextResponse.json({ error: 'Invalid importMarkdown' }, { status: 400 });
}
const source = typeof body.importMarkdown === 'string' ? markdownToImportHtml(body.importMarkdown) : (body.content as string);
updateData.content = cleanupAccidentalStandaloneInlineCode(source);
if (typeof body.importMarkdown === 'string' && process.env.NODE_ENV !== 'production') {
const lines = body.importMarkdown.replace(/\r\n?/g, '\n').split('\n');
const fenceLines = lines
.map((line: string, idx: number) => (line.trimStart().startsWith('```') || line.trimStart().startsWith('~~~') ? idx + 1 : -1))
.filter((n: number) => n > 0);
const preEnd = String(updateData.content || '').indexOf('</code></pre>');
console.info(`[import-md][PUT] id=${id} fences=${fenceLines.join(',') || 'none'} preEnd=${preEnd} len=${String(updateData.content || '').length}`);
}
}
if (body.icon !== undefined) {
@@ -213,3 +226,5 @@ export async function DELETE(
return NextResponse.json({ error: 'Error deleting page' }, { status: 500 });
}
}
+17 -1
View File
@@ -1,6 +1,7 @@
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { requireApiAuth } from '@/lib/api-auth';
import { cleanupAccidentalStandaloneInlineCode, markdownToImportHtml } from '@/lib/markdown-import';
const MAX_TITLE_LENGTH = 200;
const MAX_TAGS = 20;
@@ -64,7 +65,19 @@ export async function POST(request: Request) {
}
const type = body.type === 'folder' ? 'folder' : 'file';
const content = typeof body.content === 'string' ? body.content : '';
const importMarkdown = typeof body.importMarkdown === 'string' ? body.importMarkdown : null;
const rawContent = typeof body.content === 'string' ? body.content : '';
const content = cleanupAccidentalStandaloneInlineCode(
importMarkdown !== null ? markdownToImportHtml(importMarkdown) : rawContent
);
if (importMarkdown !== null && process.env.NODE_ENV !== 'production') {
const lines = importMarkdown.replace(/\r\n?/g, '\n').split('\n');
const fenceLines = lines
.map((line: string, idx: number) => (line.trimStart().startsWith('```') || line.trimStart().startsWith('~~~') ? idx + 1 : -1))
.filter((n: number) => n > 0);
const preEnd = content.indexOf('</code></pre>');
console.info(`[import-md][POST] title="${title}" fences=${fenceLines.join(',') || 'none'} preEnd=${preEnd} len=${content.length}`);
}
const parentId = typeof body.parentId === 'string' ? body.parentId : null;
const tags = normalizeTags(body.tags);
const icon = typeof body.icon === 'string' ? body.icon : null;
@@ -105,3 +118,6 @@ export async function POST(request: Request) {
return NextResponse.json({ error: 'Error creating page' }, { status: 500 });
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
import { NextRequest, NextResponse } from "next/server";
import JSZip from "jszip";
import { marked } from "marked";
import { prisma } from "@/lib/prisma";
import { requireApiAuth } from "@/lib/api-auth";
import { markdownToImportHtml } from "@/lib/markdown-import";
type ZipEntry = {
path: string;
@@ -68,7 +68,7 @@ async function parseMarkdownWithFrontmatter(filePath: string, content: string):
}
}
const htmlContent = await marked(markdownBody, { gfm: true, breaks: true });
const htmlContent = markdownToImportHtml(markdownBody);
return { title, order, tags, htmlContent };
}