diff --git a/bookstax/Extensions/String+HTML.swift b/bookstax/Extensions/String+HTML.swift new file mode 100644 index 0000000..24ae774 --- /dev/null +++ b/bookstax/Extensions/String+HTML.swift @@ -0,0 +1,20 @@ +import Foundation + +extension String { + /// Strips HTML tags and decodes common HTML entities for plain-text display. + var strippingHTML: String { + // Use NSAttributedString to parse HTML — handles entities and nested tags correctly + guard let data = data(using: .utf8), + let attributed = try? NSAttributedString( + data: data, + options: [.documentType: NSAttributedString.DocumentType.html, + .characterEncoding: String.Encoding.utf8.rawValue], + documentAttributes: nil) + else { + // Fallback: remove tags with regex + return replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression) + .trimmingCharacters(in: .whitespacesAndNewlines) + } + return attributed.string.trimmingCharacters(in: .whitespacesAndNewlines) + } +}