Fix crash when parsing invalid URLs

This commit is contained in:
Shadowfacts 2021-06-15 23:33:40 -04:00
parent dd0dcbde9c
commit 255e5d7ff4
1 changed files with 12 additions and 3 deletions

View File

@ -44,8 +44,6 @@ public struct GeminiParser {
// URL(string:relativeTo:) does not handle // meaning the same protocol as the base URL
urlString = baseURL.scheme! + ":" + urlString
}
// todo: if the URL initializer fails, should there be a .link line with a nil URL?
let url = URL(string: urlString, relativeTo: baseURL)!.absoluteURL
let text: String?
if textStart < line.endIndex {
@ -54,7 +52,18 @@ public struct GeminiParser {
text = nil
}
doc.lines.append(.link(url, text: text))
if let url = URL(string: urlString, relativeTo: baseURL)?.absoluteURL {
doc.lines.append(.link(url, text: text))
} else {
let str: String
if let text = text {
// todo: localize me?
str = "\(text): \(urlString)"
} else {
str = urlString
}
doc.lines.append(.text(str))
}
} else if line.starts(with: "#") {
let level: Document.HeadingLevel
if line.starts(with: "###") {