// Copied from Equipo's EquipoKit (oficina/equipo 00d09aa7, // native/apple/Sources/EquipoKit/Markdown/MarkdownActions.swift). Base's changes are // marked "Base:"; keep the rest in step with Equipo's. import Foundation // Base: every link Base's Assistant may write is a web address; there are // no task links (Equipo's /tasks/ and /projects/ paths). enum MarkdownDestination: Equatable { case external(URL) } enum MarkdownActions { static func destination(_ target: String, origin: URL) -> MarkdownDestination? { guard let url = linkURL(target) else { return nil } return .external(url) } static func linkURL(_ target: String) -> URL? { guard ChatMarkdown.allowsLink(target) else { return nil } if let url = URL(string: target) { return url } // Foundation rejects a few host characters Go's URL parser allows. // Encode only that authority after validating the original target. guard let scheme = target.range(of: "://") else { return nil } let rest = target[scheme.upperBound...] let end = rest.firstIndex(where: { "/?#".contains($0) }) ?? target.endIndex let authority = target[scheme.upperBound.. String? { var stack = nodes while let node = stack.popLast() { if node.kind == "link", let target = node.target, linkURL(target) == url { return target } stack.append(contentsOf: node.children) } return nil } static func copyText(_ node: ChatMarkdownNode) -> String? { guard node.kind == "paste", node.copyEnabled == true else { return nil } let text = node.text ?? "" return text.hasSuffix("\n") ? String(text.dropLast()) : text } }