mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-25 17:51:01 +00:00
Escape markdown markup characters on their way into HTMLToMarkdown to prevent them being rendered as styled text after they come out. (#329)
This commit is contained in:
parent
d0854d0107
commit
ffe1a1dba0
1 changed files with 10 additions and 1 deletions
|
@ -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]
|
||||
|
@ -18,6 +18,15 @@ public struct HTMLString: Decodable, Equatable, Hashable {
|
|||
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)
|
||||
.toMarkdown()
|
||||
|
|
Loading…
Reference in a new issue