Compose: Fix editing, now keep line breaks and link close #602

This commit is contained in:
Thomas Ricouard 2023-02-04 11:21:30 +01:00
parent 11b9346b0e
commit 86937e65fe
2 changed files with 12 additions and 2 deletions

View file

@ -29,7 +29,17 @@ public struct HTMLString: Decodable, Equatable, Hashable {
let document: Document = try SwiftSoup.parse(htmlValue)
handleNode(node: document)
asRawText = try document.text()
document.outputSettings(OutputSettings().prettyPrint(pretty: false))
try document.select("br").after("\n")
try document.select("p").after("\n\n")
let html = try document.html()
var text = try SwiftSoup.clean(html, "", Whitelist.none(), OutputSettings().prettyPrint(pretty: false)) ?? ""
// Remove the two last line break added after the last paragraph.
if text.hasSuffix("\n\n") {
_ = text.removeLast()
_ = text.removeLast()
}
asRawText = text
} catch {
asRawText = htmlValue
}

View file

@ -232,7 +232,7 @@ public class StatusEditorViewModel: ObservableObject {
selectedRange = .init(location: statusText.string.utf16.count, length: 0)
markedTextRange = nil
case let .edit(status):
var rawText = NSAttributedString(status.content.asSafeMarkdownAttributedString).string
var rawText = status.content.asRawText
for mention in status.mentions {
rawText = rawText.replacingOccurrences(of: "@\(mention.username)", with: "@\(mention.acct)")
}