Branchwell

Glimmer / app · glimmertoday.ai / native / Glimmer / InkspringLink.swift

1import Foundation
2#if os(macOS)
3import AppKit
4#elseif os(iOS)
5import UIKit
6#endif
7
8/// Builds Inkspring's `inkspring://new-idea` link, which saves one open post idea.
9/// Contract: Inkspring `native-mac/README.md`, "Send an idea from another app" (Mac 1.0.89+, iPhone 1.0.51+).
10enum InkspringLink {
11 static let source = "glimmer"
12 static let probe = URL(string: "inkspring://new-idea")!
13 static let textLimit = 2_000
14 static let titleLimit = 140
15 static let shortTitleLimit = 60
16
17 /// Letters, digits and `-._~` stay as they are; everything else is percent-encoded,
18 /// so `+`, `&`, `=` and `#` in the quote can't change the meaning of the link.
19 private static let unreserved = CharacterSet(charactersIn:
20 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~")
21
22 static func newIdea(title: String, text: String) -> URL? {
23 let body = clip(text.trimmingCharacters(in: .whitespacesAndNewlines), to: textLimit)
24 guard !body.isEmpty else { return nil }
25 let heading = clip(oneLine(title), to: titleLimit)
26 var query = [("title", heading), ("text", body), ("source", source)]
27 if heading.isEmpty { query.removeFirst() }
28 let encoded = query.map { "\($0.0)=\(encode($0.1))" }.joined(separator: "&")
29 return URL(string: "inkspring://new-idea?\(encoded)")
30 }
31
32 /// Today's quote as an idea: a short title from the quote, and the quote with its attribution as the text.
33 static func newIdea(quote: String, author: String) -> URL? {
34 let words = quote.trimmingCharacters(in: .whitespacesAndNewlines)
35 let name = author.trimmingCharacters(in: .whitespacesAndNewlines)
36 let text = name.isEmpty ? "“\(words)”" : "“\(words)”\n— \(name)"
37 return newIdea(title: shortTitle(words), text: text)
38 }
39
40 /// The quote itself when it is short, otherwise its opening words ending in "…".
41 static func shortTitle(_ quote: String) -> String {
42 let line = oneLine(quote)
43 guard line.utf16.count > shortTitleLimit else { return line.isEmpty ? "Today’s Glimmer" : line }
44 var title = ""
45 for word in line.split(separator: " ") {
46 let next = title.isEmpty ? String(word) : title + " " + word
47 if next.utf16.count + 1 > shortTitleLimit { break }
48 title = next
49 }
50 if title.isEmpty { return clip(line, to: shortTitleLimit) }
51 return title.trimmingCharacters(in: .punctuationCharacters) + "…"
52 }
53
54 static func encode(_ value: String) -> String {
55 value.addingPercentEncoding(withAllowedCharacters: unreserved) ?? ""
56 }
57
58 /// Shortens to `limit` UTF-16 units ending in "…", never splitting a character.
59 static func clip(_ value: String, to limit: Int) -> String {
60 guard value.utf16.count > limit else { return value }
61 var result = ""
62 for character in value {
63 if result.utf16.count + character.utf16.count + 1 > limit { break }
64 result.append(character)
65 }
66 return result.trimmingCharacters(in: .whitespacesAndNewlines) + "…"
67 }
68
69 private static func oneLine(_ value: String) -> String {
70 value.components(separatedBy: .newlines)
71 .map { $0.trimmingCharacters(in: .whitespaces) }
72 .filter { !$0.isEmpty }
73 .joined(separator: " ")
74 }
75
76 /// True when a version of Inkspring that handles the link is installed.
77 @MainActor static var isInstalled: Bool {
78 #if os(macOS)
79 NSWorkspace.shared.urlForApplication(toOpen: probe) != nil
80 #elseif os(iOS)
81 UIApplication.shared.canOpenURL(probe)
82 #else
83 false
84 #endif
85 }
86
87 @MainActor static func open(_ url: URL) {
88 #if os(macOS)
89 NSWorkspace.shared.open(url)
90 #elseif os(iOS)
91 UIApplication.shared.open(url)
92 #endif
93 }
94}