Note格式化去除末尾空白并确保标题后两行间距
This commit is contained in:
@@ -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() {
|
||||||
|
|||||||
Reference in New Issue
Block a user