From d4783d74386552579d4b39b6216252590ba90af0 Mon Sep 17 00:00:00 2001 From: fish Date: Sun, 12 Jul 2026 22:49:05 +0800 Subject: [PATCH] =?UTF-8?q?Note=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E6=9C=AB=E5=B0=BE=E7=A9=BA=E7=99=BD=E5=B9=B6=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E6=A0=87=E9=A2=98=E5=90=8E=E4=B8=A4=E8=A1=8C=E9=97=B4?= =?UTF-8?q?=E8=B7=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/FormatNoteViewController.swift | 30 ++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) 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() {