oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2
3enum MarkdownDestination: Equatable {
4 case task(String)
5 case external(URL)
6}
7
8enum MarkdownActions {
9 static func destination(_ target: String, origin: URL) -> MarkdownDestination? {
10 guard let url = linkURL(target) else { return nil }
11 if target.hasPrefix("/tasks/") { return .task(String(target.dropFirst("/tasks/".count))) }
12 if target.hasPrefix("/projects/") { return .external(URL(string: target, relativeTo: origin)!.absoluteURL) }
13 return .external(url)
14 }
15 static func linkURL(_ target: String) -> URL? {
16 guard ChatMarkdown.allowsLink(target) else { return nil }
17 if let url = URL(string: target) { return url }
18 // Foundation rejects a few host characters Go's URL parser allows.
19 // Encode only that authority after validating the original target.
20 guard let scheme = target.range(of: "://") else { return nil }
21 let rest = target[scheme.upperBound...]
22 let end = rest.firstIndex(where: { "/?#".contains($0) }) ?? target.endIndex
23 let authority = target[scheme.upperBound..<end]
24 let safe = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~!$&'()*+,;=:%@")
25 guard let encoded = String(authority).addingPercentEncoding(withAllowedCharacters: safe) else { return nil }
26 return URL(string: String(target[..<scheme.upperBound]) + encoded + target[end...])
27 }
28 static func originalTarget(_ url: URL, in nodes: [ChatMarkdownNode]) -> String? {
29 var stack = nodes
30 while let node = stack.popLast() {
31 if node.kind == "link", let target = node.target, linkURL(target) == url { return target }
32 stack.append(contentsOf: node.children)
33 }
34 return nil
35 }
36 static func copyText(_ node: ChatMarkdownNode) -> String? {
37 guard node.kind == "paste", node.copyEnabled == true else { return nil }
38 let text = node.text ?? ""
39 return text.hasSuffix("\n") ? String(text.dropLast()) : text
40 }
41}
42