import Foundation /// Where some Markdown is written, so a picture it names can be found: inside /// its repository as the pages resolve one, and from the server when it's one /// a pull, issue or comment attaches. struct MarkdownPlace: Hashable, Sendable { let repo: RepoRef /// The branch a file was read at; none for what's written about the repository. var ref: String? /// The pull, issue or comment that says it. var subject: String? static let site = "https://knotless.dev" /// Where to look for the picture `dest` names, best first. func pictures(_ dest: String) -> [URL] { let scheme = dest.split(separator: ":", maxSplits: 1).first.map(String.init) if let scheme, dest.contains(":"), !scheme.contains(where: { "/#?".contains($0) }) { guard ["http", "https"].contains(scheme.lowercased()), let url = URL(string: dest) else { return [] } return (attached(url).map { [$0] } ?? []) + [url] } if dest.hasPrefix("#") { return [] } let cut = dest.firstIndex { "?#".contains($0) } ?? dest.endIndex var parts: [Substring] = [] for part in dest[.. URL? { guard url.path.hasSuffix("/xrpc/com.atproto.sync.getBlob"), let subject, let asked = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems, let did = asked.first(where: { $0.name == "did" })?.value, let cid = asked.first(where: { $0.name == "cid" })?.value, subject.hasPrefix("at://\(did)/"), var parts = URLComponents(string: "\(Self.site)/xrpc/dev.knotless.getAttachment") else { return nil } parts.queryItems = [.init(name: "repo", value: repo.uri), .init(name: "uri", value: subject), .init(name: "cid", value: cid)] return parts.url } }