From c1205036a239b955b66ccb56958cd1dcba0b1361 Mon Sep 17 00:00:00 2001 From: Gareth Simpson Date: Fri, 17 Feb 2023 05:31:24 +0000 Subject: [PATCH] This simplifies the parser. Having read the spec more closely as part of investigating bug 855, I was overcomplicating it. The server promises to send html so we should render it that way, not with heuristics. (#900) This puts in line breaks exclusively where there are
s and takes out other line breaks that are in the body text. *Doesn't* fix bug 855 --- .../Models/Sources/Models/Alias/HTMLString.swift | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/Packages/Models/Sources/Models/Alias/HTMLString.swift b/Packages/Models/Sources/Models/Alias/HTMLString.swift index c5e27d9a..d169fded 100644 --- a/Packages/Models/Sources/Models/Alias/HTMLString.swift +++ b/Packages/Models/Sources/Models/Alias/HTMLString.swift @@ -129,17 +129,7 @@ public struct HTMLString: Codable, Equatable, Hashable { } } else if node.nodeName() == "br" { if asMarkdown.count > 0 { // ignore first opening
- // some code to try and stop double carriage rerturns where they aren't required - // not perfect but effective in almost all cases - if !asMarkdown.hasSuffix("\n") && !asMarkdown.hasSuffix("\u{2028}") { - if let next = node.nextSibling() { - if next.nodeName() == "#text" && (next.description.hasPrefix("\n") || next.description.hasPrefix("\u{2028}")) { - // do nothing - } else { - asMarkdown += "\n" - } - } - } + asMarkdown += "\n" } } else if node.nodeName() == "a" { let href = try node.attr("href") @@ -168,8 +158,8 @@ public struct HTMLString: Codable, Equatable, Hashable { txt = main_regex.stringByReplacingMatches(in: txt, options: [], range: NSRange(location: 0, length: txt.count), withTemplate: "\\\\$1") txt = underscore_regex.stringByReplacingMatches(in: txt, options: [], range: NSRange(location: 0, length: txt.count), withTemplate: "\\\\$1") } - - asMarkdown += txt + // Strip newlines and line separators - they should be being sent as
s + asMarkdown += txt.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\u{2028}", with: "") } for n in node.getChildNodes() {