damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

URIParsing.swift (1208B)


      1 //
      2 //  URIParsing.swift
      3 //  damus
      4 //
      5 //  Created by KernelKind on 1/13/24.
      6 //
      7 
      8 import Foundation
      9 
     10 fileprivate let MAX_CHAR_URL = 80
     11 
     12 private func remove_damus_uri_prefix(_ s: String) -> String {
     13     var uri = s.replacingOccurrences(of: "https://damus.io/r/", with: "")
     14     uri = uri.replacingOccurrences(of: "https://damus.io/", with: "")
     15     uri = uri.replacingOccurrences(of: "/", with: "")
     16     
     17     return uri
     18 }
     19 
     20 func remove_nostr_uri_prefix(_ s: String) -> String {
     21     if s.starts(with: "https://damus.io/") {
     22         return remove_damus_uri_prefix(s)
     23     }
     24 
     25     var uri = s
     26     uri = uri.replacingOccurrences(of: "nostr://", with: "")
     27     uri = uri.replacingOccurrences(of: "nostr:", with: "")
     28 
     29     // Fix for non-latin characters resulting in second colon being encoded
     30     uri = uri.replacingOccurrences(of: "damus:t%3A", with: "t:")
     31     
     32     uri = uri.replacingOccurrences(of: "damus://", with: "")
     33     uri = uri.replacingOccurrences(of: "damus:", with: "")
     34     
     35     return uri
     36 }
     37 
     38 func abbreviateURL(_ url: URL) -> String {
     39     let urlString = url.absoluteString
     40     
     41     if urlString.count > MAX_CHAR_URL {
     42         return String(urlString.prefix(MAX_CHAR_URL)) + "..."
     43     }
     44     return urlString
     45 }