Add more markdown formatting to the text parser

This commit is contained in:
Thomas Ricouard 2024-10-05 12:51:32 +02:00
parent d4706bea31
commit 1dec2ab68a

View file

@ -109,7 +109,7 @@ public struct HTMLString: Codable, Equatable, Hashable, @unchecked Sendable {
try container.encode(links, forKey: .links) try container.encode(links, forKey: .links)
} }
private mutating func handleNode(node: SwiftSoup.Node) { private mutating func handleNode(node: SwiftSoup.Node, indent: Int? = 0) {
do { do {
if let className = try? node.attr("class") { if let className = try? node.attr("class") {
if className == "invisible" { if className == "invisible" {
@ -121,7 +121,7 @@ public struct HTMLString: Codable, Equatable, Hashable, @unchecked Sendable {
// descend into this one now and // descend into this one now and
// append the ellipsis // append the ellipsis
for nn in node.getChildNodes() { for nn in node.getChildNodes() {
handleNode(node: nn) handleNode(node: nn, indent: indent)
} }
asMarkdown += "" asMarkdown += ""
return return
@ -189,32 +189,45 @@ public struct HTMLString: Codable, Equatable, Hashable, @unchecked Sendable {
} }
// Strip newlines and line separators - they should be being sent as <br>s // Strip newlines and line separators - they should be being sent as <br>s
asMarkdown += txt.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\u{2028}", with: "") asMarkdown += txt.replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\u{2028}", with: "")
} else if node.nodeName() == "ul" { } else if node.nodeName() == "strong" || node.nodeName() == "b" {
// Unordered (bulleted) list asMarkdown += "**"
// SwiftUI's Text won't display these in an AttributedString, but we can at least improve the appearance
asMarkdown += "\n\n"
for nn in node.getChildNodes() { for nn in node.getChildNodes() {
asMarkdown += "- " handleNode(node: nn, indent: indent)
handleNode(node: nn) }
asMarkdown += "\n" asMarkdown += "**"
return
} else if node.nodeName() == "em" || node.nodeName() == "i" {
asMarkdown += "_"
for nn in node.getChildNodes() {
handleNode(node: nn, indent: indent)
}
asMarkdown += "_"
return
} else if node.nodeName() == "ul" || node.nodeName() == "ol" {
asMarkdown += "\n"
for nn in node.getChildNodes() {
handleNode(node: nn, indent: (indent ?? 0) + 1)
} }
return return
} else if node.nodeName() == "ol" { } else if node.nodeName() == "li" {
// Ordered (numbered) list asMarkdown += " "
// Same thing, won't display in a Text, but this is just an attempt to improve the appearance if let indent, indent > 1 {
asMarkdown += "\n\n" for _ in 0..<indent {
var curNumber = 1 asMarkdown += " "
for nn in node.getChildNodes() { }
asMarkdown += "\(curNumber). " asMarkdown += "- "
handleNode(node: nn) } else {
asMarkdown += "\n" asMarkdown += ""
curNumber += 1
} }
for nn in node.getChildNodes() {
handleNode(node: nn, indent: indent)
}
asMarkdown += "\n"
return return
} }
for n in node.getChildNodes() { for n in node.getChildNodes() {
handleNode(node: n) handleNode(node: n, indent: indent)
} }
} catch {} } catch {}
} }