Branchwell

Jotley / app · jotley.ai / Shared / Services / InkspringIdeaLink.swift

1import Foundation
2#if os(macOS)
3import AppKit
4#else
5import UIKit
6#endif
7
8/// Sends a note to Inkspring as a post idea through Inkspring's
9/// `inkspring://new-idea?title=&text=&source=` link. The menu item only
10/// appears when Inkspring is installed on this device.
11enum InkspringIdeaLink {
12 static let scheme = "inkspring"
13 static let source = "jotley"
14 /// Inkspring keeps 2,000 UTF-16 units of the text; trimming here keeps the link short.
15 static let maxTextLength = 2_000
16 static let maxTitleLength = 140
17
18 /// The idea text: the summary when the note has one, otherwise the transcript.
19 static func ideaText(summary: String?, transcript: String?) -> String? {
20 let summaryText = summary.map(plainText) ?? ""
21 let body = Note.isMeaningfulSummaryText(summary) && !summaryText.isEmpty
22 ? summaryText
23 : (transcript ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
24 guard !body.isEmpty else { return nil }
25 return truncated(body, to: maxTextLength)
26 }
27
28 static func url(title: String?, text: String) -> URL? {
29 let trimmedText = text.trimmingCharacters(in: .whitespacesAndNewlines)
30 guard !trimmedText.isEmpty else { return nil }
31 var items = [URLQueryItem]()
32 let trimmedTitle = (title ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
33 if !trimmedTitle.isEmpty { items.append(URLQueryItem(name: "title", value: truncated(trimmedTitle, to: maxTitleLength))) }
34 items.append(URLQueryItem(name: "text", value: trimmedText))
35 items.append(URLQueryItem(name: "source", value: source))
36 var components = URLComponents()
37 components.scheme = scheme
38 components.host = "new-idea"
39 components.queryItems = items
40 // URLComponents leaves "+" alone, and Inkspring reads "+" as a space.
41 components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
42 return components.url
43 }
44
45 /// Strips the **bold** and `#` heading markers Jotley's summaries use.
46 static func plainText(_ markdown: String) -> String {
47 markdown
48 .replacingOccurrences(of: "**", with: "")
49 .replacingOccurrences(of: #"(?m)^#{1,6}[ \t]+"#, with: "", options: .regularExpression)
50 .trimmingCharacters(in: .whitespacesAndNewlines)
51 }
52
53 private static func truncated(_ text: String, to limit: Int) -> String {
54 guard text.utf16.count > limit else { return text }
55 var result = ""
56 for character in text {
57 if result.utf16.count + character.utf16.count > limit - 1 { break }
58 result.append(character)
59 }
60 return result.trimmingCharacters(in: .whitespacesAndNewlines) + "…"
61 }
62
63 private static var probeURL: URL { URL(string: "\(scheme)://new-idea")! }
64
65 @MainActor static var isInstalled: Bool {
66 #if os(macOS)
67 NSWorkspace.shared.urlForApplication(toOpen: probeURL) != nil
68 #else
69 UIApplication.shared.canOpenURL(probeURL)
70 #endif
71 }
72
73 /// Opens Inkspring with the note as a new post idea. Returns false when there is nothing to send.
74 @MainActor @discardableResult
75 static func send(title: String?, summary: String?, transcript: String?) -> Bool {
76 guard let text = ideaText(summary: summary, transcript: transcript),
77 let link = url(title: title, text: text) else { return false }
78 #if os(macOS)
79 NSWorkspace.shared.open(link)
80 #else
81 UIApplication.shared.open(link)
82 #endif
83 return true
84 }
85}