Fix link lines with unescaped characters in the link not being parsed

This commit is contained in:
Shadowfacts 2021-08-30 21:03:18 -04:00
parent 66f318f0e7
commit 57ae52c090
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 9 additions and 0 deletions

View File

@ -54,6 +54,11 @@ public struct GeminiParser {
if let url = URL(string: urlString, relativeTo: baseURL)?.absoluteURL {
doc.lines.append(.link(url, text: text))
} else if let escaped = urlString.addingPercentEncoding(withAllowedCharacters: .URLAllowed),
let url = URL(string: escaped, relativeTo: baseURL)?.absoluteURL {
// if URL initialization fails because there are unescaped chars in the doc, escape everything and try again.
// I'm not certain, but it feels unsafe to always do this escaping
doc.lines.append(.link(url, text: text))
} else {
let str: String
if let text = text {
@ -113,3 +118,7 @@ fileprivate extension String {
return index
}
}
private extension CharacterSet {
static let URLAllowed = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%")
}