feat: preview server startup directly

This commit is contained in:
2026-02-16 21:28:16 +01:00
parent 5d0791566e
commit 54a8ba5ceb
4 changed files with 216 additions and 2 deletions

View File

@@ -8,6 +8,8 @@ import { getProjectEngine } from './ProjectEngine';
interface ActiveProjectContext {
projectId: string;
dataDir?: string;
projectName?: string;
projectDescription?: string;
}
interface PostEngineContract {
@@ -57,6 +59,30 @@ function clampMaxPostsPerPage(value: unknown): number {
return normalized;
}
function resolvePageTitle(metadata: ProjectMetadata | null, fallbackProjectName?: string, fallbackProjectDescription?: string): string {
const candidate = metadata?.description?.trim();
if (candidate) {
return candidate;
}
const metadataName = metadata?.name?.trim();
if (metadataName) {
return metadataName;
}
const descriptionFallback = fallbackProjectDescription?.trim();
if (descriptionFallback) {
return descriptionFallback;
}
const fallback = fallbackProjectName?.trim();
if (fallback) {
return fallback;
}
return 'Blog Preview';
}
function escapeHtml(value: string): string {
return value
.replace(/&/g, '&')
@@ -162,7 +188,12 @@ export class PreviewServer {
const activeProject = await projectEngine.getActiveProject();
const projectId = activeProject?.id ?? 'default';
const dataDir = projectEngine.getDataDir(projectId, activeProject?.dataPath);
return { projectId, dataDir };
return {
projectId,
dataDir,
projectName: activeProject?.name,
projectDescription: activeProject?.description ?? undefined,
};
});
}
@@ -267,7 +298,7 @@ export class PreviewServer {
return;
}
this.respond(res, 200, getPageHtml(result, 'Blog Preview'));
this.respond(res, 200, getPageHtml(result, resolvePageTitle(metadata, context.projectName, context.projectDescription)));
} catch (error) {
console.error('[PreviewServer] Request failed:', error);
this.respond(res, 500, 'Internal Server Error');