import Foundation
import UIKit

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)
    }
}
