diff --git a/web/src/lib/space-archive/content-parser.ts b/web/src/lib/space-archive/content-parser.ts index 90ac0c8..303a62e 100644 --- a/web/src/lib/space-archive/content-parser.ts +++ b/web/src/lib/space-archive/content-parser.ts @@ -274,5 +274,20 @@ export function rewriteMarkdownForExport( if (!trimmedBody) { return `${titleHeader}\n` } + // 正文首个一级标题与页面标题同文时,不再重复追加首行标题,避免与正文 h1 重复 + if (firstLevelOneHeading(trimmedBody) === pageTitle.trim()) { + return `${trimmedBody}\n` + } return `${titleHeader}\n\n${trimmedBody}\n` } + +/** 返回 Markdown 正文首个一级标题文本;无一级标题返回 null。 */ +function firstLevelOneHeading(markdown: string): string | null { + for (const line of markdown.split("\n")) { + const match = /^\s*#(?:\s+(.*?))?\s*$/.exec(line) + if (!match) continue + const text = (match[1] ?? "").replace(/\s+#+\s*$/, "").trim() + return text + } + return null +} diff --git a/web/src/lib/space-archive/export-import.test.ts b/web/src/lib/space-archive/export-import.test.ts index 4edb362..b78844d 100644 --- a/web/src/lib/space-archive/export-import.test.ts +++ b/web/src/lib/space-archive/export-import.test.ts @@ -16,7 +16,7 @@ import { rewriteContentJson, rewriteMarkdownForExport } from "./content-parser" -import { generateAllPagePaths, relativePosixPath } from "./path" +import { cleanFilename, generateAllPagePaths, relativePosixPath } from "./path" import type { ManifestFile } from "./types" vi.mock("@/lib/api", async (importOriginal) => { @@ -98,6 +98,31 @@ describe("空间导出/导入核心引擎", () => { ).toBe("1-next.md") }) + it("cleanFilename 折叠连续空白为连字符,导出包内文件名不含空格", () => { + expect(cleanFilename("发布 说明")).toBe("发布-说明") + expect(cleanFilename("a b c")).toBe("a-b-c") + expect(cleanFilename(" 开头结尾 ")).toBe("开头结尾") + expect(cleanFilename("a / b: c")).toBe("a-b-c") + }) + + it("generateAllPagePaths 对含空格标题生成不含空格的 md/json 路径", () => { + const roots: PageNode[] = [ + { + id: 1, + parentId: null, + title: "版本 1.0 发布 说明", + sortOrder: 0, + updatedAt: "2026-11-28", + children: [] + } + ] + const map = generateAllPagePaths(roots) + const info = map.get(1)! + expect(info.mdPath).not.toContain(" ") + expect(info.jsonPath).not.toContain(" ") + expect(info.mdPath).toMatch(/^pages\/0-版本-1.0-发布-说明-/) + }) + it("校验与解析站内文件 key,拦截非法形状与站外外链", () => { // 合法 UPLOAD_KEY 形状 expect( @@ -306,6 +331,40 @@ describe("空间导出/导入核心引擎", () => { expect(rewritten).toContain("[架构总览](../0-arch.md)") expect(rewritten).toContain("[外部文档](https://external.com)") }) + + it("正文首个 h1 与页面标题同文时不再重复追加首行标题", () => { + const sameH1 = rewriteMarkdownForExport( + "当前子页", + "# 当前子页\n\n正文内容", + "pages/1-guide/0-sub.md", + new Map(), + new Map() + ) + expect(sameH1).toBe("# 当前子页\n\n正文内容\n") + expect(sameH1.split("\n").filter((l) => l.startsWith("# "))).toHaveLength( + 1 + ) + }) + + it("正文无 h1 或首 h1 异文时仍补首行标题", () => { + const noH1 = rewriteMarkdownForExport( + "当前子页", + "正文内容", + "pages/1-guide/0-sub.md", + new Map(), + new Map() + ) + expect(noH1.startsWith("# 当前子页\n\n正文内容")).toBe(true) + + const diffH1 = rewriteMarkdownForExport( + "当前子页", + "# 其他标题\n\n正文内容", + "pages/1-guide/0-sub.md", + new Map(), + new Map() + ) + expect(diffH1.startsWith("# 当前子页\n\n# 其他标题")).toBe(true) + }) }) describe("本地预检与坏包防御(零服务端写入)", () => { diff --git a/web/src/lib/space-archive/path.ts b/web/src/lib/space-archive/path.ts index 722a6fd..26a9346 100644 --- a/web/src/lib/space-archive/path.ts +++ b/web/src/lib/space-archive/path.ts @@ -11,6 +11,7 @@ export function stripControlChars(value: string): string { export function cleanFilename(title: string, fallback = "untitled"): string { const stripped = stripControlChars(title) .replace(ILLEGAL_FILENAME_CHARS, "-") + .replace(/\s+/g, "-") .replace(/-{2,}/g, "-") .replace(/^[-.\s]+|[-.\s]+$/g, "") return stripped || fallback diff --git a/web/src/locales/en.json b/web/src/locales/en.json index 2496d01..a415e3a 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -14,6 +14,7 @@ "en": "English" }, "cancel": "Cancel", + "confirm": "Confirm", "save": "Save", "saving": "Saving…", "retry": "Retry", diff --git a/web/src/locales/zh.json b/web/src/locales/zh.json index 2bef897..83912b7 100644 --- a/web/src/locales/zh.json +++ b/web/src/locales/zh.json @@ -14,6 +14,7 @@ "en": "English" }, "cancel": "取消", + "confirm": "确定", "save": "保存", "saving": "保存中…", "retry": "重试",