From d5c45dc8ba206de0cd0477f26fec2ecc34d19f3d Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sun, 19 Feb 2023 09:20:09 -0800 Subject: [PATCH] Fix rare markdown crash --- damus/Util/Markdown.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/damus/Util/Markdown.swift b/damus/Util/Markdown.swift index 28139d68..2f1722d5 100644 --- a/damus/Util/Markdown.swift +++ b/damus/Util/Markdown.swift @@ -8,8 +8,8 @@ import Foundation public struct Markdown { - private let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) - + private var detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) + /// Ensure the specified URL has a scheme by prepending "https://" if it's absent. static func withScheme(_ url: any StringProtocol) -> any StringProtocol { return url.contains("://") ? url : "https://" + url @@ -31,6 +31,9 @@ public struct Markdown { /// Process the input text and add markdown for any embedded URLs. public func process(_ input: String) -> AttributedString { + guard let detector else { + return AttributedString(stringLiteral: input) + } let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) var output = input // Start with the last match, because replacing the first would invalidate all subsequent indices