Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions web/src/lib/space-archive/content-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
61 changes: 60 additions & 1 deletion web/src/lib/space-archive/export-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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("本地预检与坏包防御(零服务端写入)", () => {
Expand Down
1 change: 1 addition & 0 deletions web/src/lib/space-archive/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"en": "English"
},
"cancel": "Cancel",
"confirm": "Confirm",
"save": "Save",
"saving": "Saving…",
"retry": "Retry",
Expand Down
1 change: 1 addition & 0 deletions web/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"en": "English"
},
"cancel": "取消",
"confirm": "确定",
"save": "保存",
"saving": "保存中…",
"retry": "重试",
Expand Down