Branchwell

Inkspring / app · inkspring.ai / native-mac / Sources / InkspringSchedule / ContentPlan.swift

Raw 1.6 KB on 332c2458022ae8f6648ea05f3c733b528a595172
1import Foundation
2
3public enum ContentPlan {
4 public static let limit = 100_000
5 public static func validate(_ markdown: String) throws {
6 guard markdown.utf16.count <= limit, !markdown.contains("\0") else { throw ContentPlanError.invalid }
7 }
8 public static func decode(_ data: Data) throws -> String {
9 guard data.count <= limit * 4, let text = String(data: data, encoding: .utf8) else { throw ContentPlanError.invalid }
10 try validate(text)
11 return text
12 }
13 public static func filename(_ reference: String) -> String {
14 let clean = reference.uppercased().filter { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
15 return String((clean.isEmpty ? "CONTENT" : clean).prefix(80)) + "-plan.md"
16 }
17 public static func requested(in request: String) -> Bool {
18 request.range(of: #"(?i)\b(?:with|include|including|add)\s+(?:a\s+)?plan\b"#, options: .regularExpression) != nil
19 }
20 public static func starter(_ title: String) -> String {
21 "# \(title)\n\n## Goal\n\n\n## Steps\n\n- [ ] Develop the angle.\n- [ ] Write the draft.\n- [ ] Review and verify.\n"
22 }
23}
24
25public enum ContentPlanError: LocalizedError {
26 case invalid, conflict
27 public var errorDescription: String? {
28 switch self {
29 case .invalid: "Choose UTF-8 Markdown with up to 100,000 characters."
30 case .conflict: "The saved plan changed. Your draft is kept. Compare the latest saved plan before saving again."
31 }
32 }
33}
34
35public enum CompanionContentOperation: String, Codable, Sendable {
36 case savePlan, createDraft, createNewsletter, createProject, moveProject, setTitle
37}