From ffe1a1dba0fc1e54352ad481c11e64a1eb2a66c5 Mon Sep 17 00:00:00 2001 From: Gareth Simpson Date: Tue, 24 Jan 2023 05:38:38 +0000 Subject: [PATCH] Escape markdown markup characters on their way into HTMLToMarkdown to prevent them being rendered as styled text after they come out. (#329) --- Packages/Models/Sources/Models/Alias/HTMLString.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Packages/Models/Sources/Models/Alias/HTMLString.swift b/Packages/Models/Sources/Models/Alias/HTMLString.swift index 54dc33ba..7534130e 100644 --- a/Packages/Models/Sources/Models/Alias/HTMLString.swift +++ b/Packages/Models/Sources/Models/Alias/HTMLString.swift @@ -4,7 +4,7 @@ import SwiftSoup import SwiftUI public struct HTMLString: Decodable, Equatable, Hashable { - public let htmlValue: String + public var htmlValue: String public let asMarkdown: String public let asRawText: String public let statusesURLs: [URL] @@ -17,6 +17,15 @@ public struct HTMLString: Decodable, Equatable, Hashable { } catch { htmlValue = "" } + + // https://daringfireball.net/projects/markdown/syntax + // HTML2Markdown only auto escapes * on the way out + // so we pre-escape all other escapable sequences on + // the way in here. + + if let regex = try? NSRegularExpression(pattern: "([\\\\_(){}\\[\\]\\.\\+\\-#!])", options: .caseInsensitive) { + htmlValue = regex.stringByReplacingMatches(in: htmlValue, options: [], range: NSRange(location: 0, length: htmlValue.count), withTemplate: "\\\\$1") + } do { asMarkdown = try HTMLParser().parse(html: htmlValue)