支持外部 Markdown 文件预览与格式化分发

This commit is contained in:
2026-07-13 22:17:12 +08:00
parent df7e74b10e
commit 99b014190d
6 changed files with 306 additions and 119 deletions
+10 -96
View File
@@ -20,6 +20,7 @@ class FormatNoteViewController: UIViewController {
private var rightBarButtonMode: RightBarButtonMode = .paste
private var isCloudFormatted = false
private var isNoteFormatted = false
var initialText: String?
private enum RightBarButtonMode {
case paste
@@ -32,6 +33,13 @@ class FormatNoteViewController: UIViewController {
navigationItem.largeTitleDisplayMode = .never
view.backgroundColor = .systemGroupedBackground
setupUI()
if let initialText = initialText {
textView.text = initialText
isNoteFormatted = true
noteButton.setTitle("复制并打开备忘录", for: .normal)
rightBarButtonMode = .reset
updateRightBarButtonItem()
}
setupKeyboardObservers()
}
@@ -214,7 +222,7 @@ class FormatNoteViewController: UIViewController {
if isCloudFormatted {
saveToFiles()
} else {
textView.text = formatForCloud(textView.text)
textView.text = TextFormatter.formatForCloud(textView.text)
isCloudFormatted = true
iCloudButton.setTitle("保存到 iCloud", for: .normal)
rightBarButtonMode = .reset
@@ -230,7 +238,7 @@ class FormatNoteViewController: UIViewController {
if isNoteFormatted {
copyAndOpenNotes()
} else {
textView.text = formatForNote(textView.text)
textView.text = TextFormatter.formatForNote(textView.text)
isNoteFormatted = true
noteButton.setTitle("复制并打开备忘录", for: .normal)
rightBarButtonMode = .reset
@@ -245,86 +253,6 @@ class FormatNoteViewController: UIViewController {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
private func formatForCloud(_ text: String) -> String {
var content = text
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: "\u{00A0}", with: "")
content = content.replacingOccurrences(of: "# ", with: "")
content = content.replacingOccurrences(of: "#", with: "")
content = content.replacingOccurrences(of: "", with: "")
content = content.replacingOccurrences(of: "!", with: "")
content = content.replacingOccurrences(of: ";", with: "")
content = content.replacingOccurrences(of: "", with: "")
content = content.replacingOccurrences(of: ".", with: "")
content = content.replacingOccurrences(of: "", with: "(")
content = content.replacingOccurrences(of: "", with: ")")
content = content.replacingOccurrences(of: "?", with: "")
content = content.replacingOccurrences(of: ",", with: "")
content = content.replacingOccurrences(of: ":", with: "")
let numberReplacements: [(String, String)] = [
("20、", "二十、"), ("19、", "十九、"), ("18、", "十八、"), ("17、", "十七、"),
("16、", "十六、"), ("15、", "十五、"), ("14、", "十四、"), ("13、", "十三、"),
("12、", "十二、"), ("11、", "十一、"), ("10、", "十、"),
("9、", "九、"), ("8、", "八、"), ("7、", "七、"), ("6、", "六、"),
("5、", "五、"), ("4、", "四、"), ("3、", "三、"), ("2、", "二、"), ("1、", "一、")
]
for (old, new) in numberReplacements {
content = content.replacingOccurrences(of: old, with: new)
}
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "", with: "\n")
content = removeEmptyLines(content)
content = content.replacingOccurrences(of: "\\s*", with: "\n\n", options: .regularExpression)
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "", with: "\n")
content = content.replacingOccurrences(of: "\n\n", with: "")
content = content.replacingOccurrences(of: ":一、", with: "\n\n一、")
content = content.replacingOccurrences(of: " ", with: "")
content = content.replacingOccurrences(of: "---", with: "---\n")
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
content = "# " + content
content = insertEmptyLineAfterFirstLine(content)
return content
}
private func formatForNote(_ text: String) -> String {
var content = text
content = content.replacingOccurrences(of: "# ", with: "")
content = content.replacingOccurrences(of: "#", with: "")
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
//
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)
}
var result = title
for group in groups {
result += "\n\n\n" + group.joined(separator: "\n")
}
return result.trimmingCharacters(in: .whitespacesAndNewlines)
}
private func saveToFiles() {
let fileName = generateFileName()
let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
@@ -351,20 +279,6 @@ class FormatNoteViewController: UIViewController {
}
return cleanName + ".md"
}
private func removeEmptyLines(_ text: String) -> String {
let lines = text.components(separatedBy: .newlines)
let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
return nonEmptyLines.joined(separator: "\n")
}
private func insertEmptyLineAfterFirstLine(_ text: String) -> String {
var lines = text.components(separatedBy: .newlines)
if !lines.isEmpty {
lines.insert("", at: 1)
}
return lines.joined(separator: "\n")
}
}
extension FormatNoteViewController: UIDocumentPickerDelegate {