Note格式化去除末尾空白并确保标题后两行间距

This commit is contained in:
2026-07-12 22:49:05 +08:00
parent 195483b3e1
commit d4783d7438
+25 -5
View File
@@ -230,13 +230,33 @@ class FormatNoteViewController: UIViewController {
content = content.replacingOccurrences(of: "#", with: "") content = content.replacingOccurrences(of: "#", with: "")
content = content.replacingOccurrences(of: "日星期", with: "日 星期") content = content.replacingOccurrences(of: "日星期", with: "日 星期")
// --- //
let paragraphs = content.components(separatedBy: "---") let allLines = removeEmptyLines(content).components(separatedBy: .newlines)
let formattedParagraphs = paragraphs.map { paragraph -> String in guard let title = allLines.first else { return content }
removeEmptyLines(paragraph)
// ---
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() { private func saveToFiles() {