feat(mindspace): 公开页分享组件、workspace 路径与 Plaza 发布增强

- 新增 public share widget 与 workspace relative path 解析
- 增强 publications/chat-plaza 发布链路与缩略图 demo
- 补充 schema、cover 检查脚本与回归测试

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-07-05 23:14:41 +08:00
parent 69be1e5293
commit e2ad3bf62b
39 changed files with 2184 additions and 256 deletions
+221 -9
View File
@@ -1,3 +1,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { normalizeWorkspaceRelativePath } from './mindspace-pages.mjs';
export function slugFromPageTitle(title, pageId) {
const ascii = String(title ?? '')
.normalize('NFKC')
@@ -126,9 +130,21 @@ export async function ensureChatPageForPlaza({
});
}
export async function ensurePagePublicationForPlaza({ userId, page, mindSpacePublications }) {
export async function ensurePagePublicationForPlaza({
userId,
page,
mindSpacePublications,
refreshIfOnline = true,
}) {
const existing = await mindSpacePublications.getCurrent(userId, page.id);
if (existing?.id) return existing;
if (existing?.id) {
if (refreshIfOnline && page.pageType === 'html' && mindSpacePublications.refreshOnlinePublicationHtml) {
return (
(await mindSpacePublications.refreshOnlinePublicationHtml(userId, page.id)) ?? existing
);
}
return existing;
}
const preferredSlug = slugFromPageTitle(page.title, page.id);
return mindSpacePublications.publish(userId, page.id, {
@@ -139,6 +155,80 @@ export async function ensurePagePublicationForPlaza({ userId, page, mindSpacePub
});
}
function throwAlreadyPublished(postId) {
throw Object.assign(new Error('该内容已发布到广场'), {
code: 'ALREADY_PUBLISHED',
details: { post_id: postId },
});
}
async function assertWorkspacePathNotAlreadyOnPlaza({ userId, relativePath, mindSpacePages }) {
if (!mindSpacePages?.findPlazaContextByWorkspacePath) return null;
const context = await mindSpacePages.findPlazaContextByWorkspacePath(userId, relativePath);
if (
context?.postId &&
['published', 'pending_review'].includes(String(context.postStatus ?? ''))
) {
throwAlreadyPublished(context.postId);
}
return context;
}
async function ensureWorkspaceHtmlPageForPlaza({
userId,
relativePath,
content,
title,
mindSpacePages,
}) {
const normalized = normalizePublicHtmlRelativePath(relativePath);
let page = await mindSpacePages.findPageByRelativePath(userId, normalized);
const pageInput = {
title: title ?? page?.title ?? 'MindSpace 页面',
content,
contentFormat: 'html',
pageType: 'html',
categoryCode: 'draft',
};
if (page) {
return mindSpacePages.updatePage(userId, page.id, {
...pageInput,
expectedVersion: page.versionNo,
});
}
return mindSpacePages.createFromChat(userId, pageInput, {
snapshot: {
content_mode: 'static_html',
relative_path: normalized,
},
});
}
async function publishPageToPlaza({ userId, page, mindSpacePublications, plazaPosts }) {
const publication = await ensurePagePublicationForPlaza({
userId,
page,
mindSpacePublications,
refreshIfOnline: true,
});
const { post } = await ensurePlazaPostForPublication({
userId,
publication,
plazaPosts,
});
return {
pageId: page.id,
publicationId: publication.id,
publicUrl: publication.publicUrl ?? publication.public_url ?? null,
post: {
id: post.id,
status: post.status,
},
};
}
export async function ensurePlazaPostForPublication({
userId,
publication,
@@ -173,6 +263,7 @@ export async function quickPlazaFromChat({
throw Object.assign(new Error('Plaza 或 MindSpace 未启用'), { code: 'plaza_unavailable' });
}
const { analysis } = bundle;
const page = await ensureChatPageForPlaza({
userId: user.id,
bundle,
@@ -182,25 +273,146 @@ export async function quickPlazaFromChat({
skipThumbnail: true,
});
const publication = await ensurePagePublicationForPlaza({
if (analysis.contentMode === 'static_html' && analysis.relativePath) {
await assertWorkspacePathNotAlreadyOnPlaza({
userId: user.id,
relativePath: analysis.relativePath,
mindSpacePages,
});
}
return publishPageToPlaza({
userId: user.id,
page,
mindSpacePublications,
});
const { post } = await ensurePlazaPostForPublication({
userId: user.id,
publication,
plazaPosts,
});
}
function titleFromPublicHtml(content, relativePath) {
const match = String(content ?? '').match(/<title[^>]*>([^<]+)<\/title>/i);
const fromTitle = match?.[1]?.replace(/\s+/g, ' ').trim();
if (fromTitle) return fromTitle;
const basename = path.basename(String(relativePath ?? ''), '.html');
return basename || 'MindSpace 页面';
}
function normalizePublicHtmlRelativePath(relativePath) {
const normalized = normalizeWorkspaceRelativePath(relativePath);
if (!normalized || !normalized.startsWith('public/') || !normalized.toLowerCase().endsWith('.html')) {
throw Object.assign(new Error('仅支持 workspace 公开 HTML 页面'), { code: 'invalid_public_html_path' });
}
return normalized;
}
export async function getQuickPlazaFromPublicHtmlStatus({
user,
relativePath,
mindSpacePages,
mindSpacePublications,
plazaPosts,
}) {
if (!mindSpacePages || !mindSpacePublications || !plazaPosts) {
throw Object.assign(new Error('Plaza 或 MindSpace 未启用'), { code: 'plaza_unavailable' });
}
const normalized = normalizePublicHtmlRelativePath(relativePath);
const context = await mindSpacePages.findPlazaContextByWorkspacePath?.(user.id, normalized);
if (
context?.postId &&
['published', 'pending_review'].includes(String(context.postStatus ?? ''))
) {
return {
published: true,
pageId: context.pageId,
publicationId: context.publicationId,
post: {
id: context.postId,
status: context.postStatus,
},
};
}
const page = context?.pageId
? { id: context.pageId }
: await mindSpacePages.findPageByRelativePath(user.id, normalized);
if (!page) {
return { published: false };
}
const publication =
context?.publicationId != null
? { id: context.publicationId }
: await mindSpacePublications.getCurrent(user.id, page.id);
if (!publication?.id) {
return { published: false, pageId: page.id };
}
const post = await plazaPosts.findPostByPublicationId(publication.id, { userId: user.id });
if (!post || !['published', 'pending_review'].includes(post.status)) {
return {
published: false,
pageId: page.id,
publicationId: publication.id,
};
}
return {
published: true,
pageId: page.id,
publicationId: publication.id,
publicUrl: publication.publicUrl ?? publication.public_url ?? null,
post: {
id: post.id,
status: post.status,
},
};
}
export async function quickPlazaFromPublicHtml({
user,
relativePath,
mindSpacePages,
mindSpacePublications,
plazaPosts,
publishDir,
}) {
if (!mindSpacePages || !mindSpacePublications || !plazaPosts) {
throw Object.assign(new Error('Plaza 或 MindSpace 未启用'), { code: 'plaza_unavailable' });
}
const normalized = normalizePublicHtmlRelativePath(relativePath);
await assertWorkspacePathNotAlreadyOnPlaza({
userId: user.id,
relativePath: normalized,
mindSpacePages,
});
const resolvedRoot = path.resolve(publishDir);
const filePath = path.resolve(publishDir, normalized);
if (filePath !== resolvedRoot && !filePath.startsWith(`${resolvedRoot}${path.sep}`)) {
throw Object.assign(new Error('路径越界'), { code: 'invalid_public_html_path' });
}
let content;
try {
content = await fs.readFile(filePath, 'utf8');
} catch {
throw Object.assign(new Error('页面文件不存在'), { code: 'static_page_not_found' });
}
const page = await ensureWorkspaceHtmlPageForPlaza({
userId: user.id,
relativePath: normalized,
content,
title: titleFromPublicHtml(content, normalized),
mindSpacePages,
});
return publishPageToPlaza({
userId: user.id,
page,
mindSpacePublications,
plazaPosts,
});
}