Files
damus/damus/Util/URIParsing.swift
kernelkind 86e9ee16a0 ui: truncate visible media URL
The URL shown to the user before they click the 'Load Media' button can
take up a large portion of the screen, and doesn't offer any value to
the user.

This patch features a naive approach to truncate the URL to be a certain
number of characters if it is greater than the specified value. Right
now the maximum number of characters is set to 80.

Closes: https://github.com/damus-io/damus/issues/1950
Lightning-address: kernelkind@getalby.com
Signed-off-by: kernelkind <kernelkind@gmail.com>
Link: 20240130183525.50446-1-kernelkind@gmail.com
Signed-off-by: William Casarin <jb55@jb55.com>
2024-01-30 10:43:15 -08:00

46 lines
1.2 KiB
Swift

//
// URIParsing.swift
// damus
//
// Created by KernelKind on 1/13/24.
//
import Foundation
fileprivate let MAX_CHAR_URL = 80
private func remove_damus_uri_prefix(_ s: String) -> String {
var uri = s.replacingOccurrences(of: "https://damus.io/r/", with: "")
uri = uri.replacingOccurrences(of: "https://damus.io/", with: "")
uri = uri.replacingOccurrences(of: "/", with: "")
return uri
}
func remove_nostr_uri_prefix(_ s: String) -> String {
if s.starts(with: "https://damus.io/") {
return remove_damus_uri_prefix(s)
}
var uri = s
uri = uri.replacingOccurrences(of: "nostr://", with: "")
uri = uri.replacingOccurrences(of: "nostr:", with: "")
// Fix for non-latin characters resulting in second colon being encoded
uri = uri.replacingOccurrences(of: "damus:t%3A", with: "t:")
uri = uri.replacingOccurrences(of: "damus://", with: "")
uri = uri.replacingOccurrences(of: "damus:", with: "")
return uri
}
func abbreviateURL(_ url: URL) -> String {
let urlString = url.absoluteString
if urlString.count > MAX_CHAR_URL {
return String(urlString.prefix(MAX_CHAR_URL)) + "..."
}
return urlString
}