diff --git a/Note/FormatNoteViewController.swift b/Note/FormatNoteViewController.swift index fd87d46..4e111c8 100644 --- a/Note/FormatNoteViewController.swift +++ b/Note/FormatNoteViewController.swift @@ -230,13 +230,33 @@ class FormatNoteViewController: UIViewController { content = content.replacingOccurrences(of: "#", with: "") content = content.replacingOccurrences(of: "日星期", with: "日 星期") - // 按 --- 分割段落,移除每个段落内部的空行,段落之间保留两个空行 - let paragraphs = content.components(separatedBy: "---") - let formattedParagraphs = paragraphs.map { paragraph -> String in - removeEmptyLines(paragraph) + // 第一行固定为标题 + let allLines = removeEmptyLines(content).components(separatedBy: .newlines) + guard let title = allLines.first else { return content } + + // 剩余内容按 --- 分段 + let bodyLines = Array(allLines.dropFirst()) + var groups: [[String]] = [] + var currentGroup: [String] = [] + for line in bodyLines { + if line == "---" { + if !currentGroup.isEmpty { + groups.append(currentGroup) + currentGroup = [] + } + } else { + currentGroup.append(line) + } + } + if !currentGroup.isEmpty { + groups.append(currentGroup) } - return formattedParagraphs.joined(separator: "\n\n\n") + var result = title + for group in groups { + result += "\n\n\n" + group.joined(separator: "\n") + } + return result.trimmingCharacters(in: .whitespacesAndNewlines) } private func saveToFiles() {