diff --git a/GeminiFormat/GeminiParser.swift b/GeminiFormat/GeminiParser.swift index 029af47..9edf818 100644 --- a/GeminiFormat/GeminiParser.swift +++ b/GeminiFormat/GeminiParser.swift @@ -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-._~:/?#[]@!$&'()*+,;=%") +}