From 57ae52c090bd1aea74438ba04fa6d3bef9f15fe4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 30 Aug 2021 21:03:18 -0400 Subject: [PATCH] Fix link lines with unescaped characters in the link not being parsed --- GeminiFormat/GeminiParser.swift | 9 +++++++++ 1 file changed, 9 insertions(+) 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-._~:/?#[]@!$&'()*+,;=%") +}