SPB Git

Repository Public

Text.swift 14.2 KB 311 lines swift

1// SP Notes Core2// File: Sources/SPNotesDocument/Text.swift3// Description: Text utilities equal to the server's (text.ts): plain text, word count, preview, page and attachment4//              references, task counts; plus the small JavaScript-semantics helpers the ports rely on.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910/// Number of task items of a document and how many are checked.11public struct TaskCounts: Sendable, Hashable {12    public var total: Int13    public var checked: Int1415    public init(total: Int, checked: Int) {16        self.total = total17        self.checked = checked18    }19}2021/// JavaScript semantics needed to reproduce the server's string output exactly.22enum JS {23    /// `value ?? fallback`: nil for both `undefined` and `null`.24    static func nullish(_ v: JSONValue?) -> JSONValue? {25        guard let v, !v.isNull else { return nil }26        return v27    }2829    /// String conversion as in a template literal.30    static func string(_ v: JSONValue?) -> String {31        guard let v else { return "undefined" }32        switch v {33        case .null: return "null"34        case .bool(let b): return b ? "true" : "false"35        case .int(let i): return String(i)36        case .double(let d): return d.isNaN ? "NaN" : d.isInfinite ? (d < 0 ? "-Infinity" : "Infinity") : numberToString(d)37        case .string(let s): return s38        case .array(let a): return a.map { $0.isNull ? "" : string($0) }.joined(separator: ",")39        case .object: return "[object Object]"40        }41    }4243    /// ECMAScript `Number::toString` of a finite number: the shortest digits that round-trip, written without an44    /// exponent from 1e-6 up to (but excluding) 1e21, and as `d.ddde±x` beyond (`1e-7`, `1e+21`, never `1e-07`).45    static func numberToString(_ d: Double) -> String {46        if d == 0 { return "0" }47        if d < 0 { return "-" + numberToString(-d) }48        // Swift's description gives the shortest round-trip digits; only the layout differs from ECMAScript's.49        let parts = "\(d)".lowercased().split(separator: "e", maxSplits: 1)50        let mantissa = parts[0].split(separator: ".", maxSplits: 1, omittingEmptySubsequences: false)51        let whole = String(mantissa[0]), fraction = mantissa.count > 1 ? String(mantissa[1]) : ""52        var digits = whole + fraction53        // `n`: the value is 0.<digits> × 10^n.54        var n = whole.count + (parts.count > 1 ? Int(parts[1]) ?? 0 : 0)55        let leading = digits.prefix { $0 == "0" }.count56        digits.removeFirst(leading)57        n -= leading58        while digits.count > 1, digits.last == "0" { digits.removeLast() }59        let k = digits.count60        if k <= n, n <= 21 { return digits + String(repeating: "0", count: n - k) }61        if 0 < n, n <= 21 { return digits.prefix(n) + "." + digits.dropFirst(n) }62        if -6 < n, n <= 0 { return "0." + String(repeating: "0", count: -n) + digits }63        let exponent = n - 164        let sign = exponent < 0 ? "-" : "+"65        return (k == 1 ? digits : digits.prefix(1) + "." + digits.dropFirst()) + "e" + sign + String(abs(exponent))66    }6768    static func truthy(_ v: JSONValue?) -> Bool {69        guard let v else { return false }70        switch v {71        case .null: return false72        case .bool(let b): return b73        case .int(let i): return i != 074        case .double(let d): return d != 0 && !d.isNaN75        case .string(let s): return !s.isEmpty76        case .array, .object: return true77        }78    }7980    /// `Number(value)` for the cases the exports meet; NaN when not numeric.81    static func number(_ v: JSONValue?) -> Double {82        guard let v else { return .nan }83        switch v {84        case .null: return 085        case .bool(let b): return b ? 1 : 086        case .int(let i): return Double(i)87        case .double(let d): return d88        case .string(let s):89            let t = trim(s)90            return t.isEmpty ? 0 : (Double(t) ?? .nan)91        case .array, .object: return .nan92        }93    }9495    /// White space and line terminators of ECMAScript (`String.prototype.trim`).96    static func isSpace(_ u: Unicode.Scalar) -> Bool {97        switch u.value {98        case 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x20, 0xA0, 0x1680, 0x2000...0x200A, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000, 0xFEFF: return true99        default: return false100        }101    }102103    static func trimStart(_ s: String) -> String {104        let u = s.unicodeScalars105        guard let start = u.firstIndex(where: { !isSpace($0) }) else { return "" }106        return String(u[start...])107    }108109    static func trim(_ s: String) -> String {110        let u = s.unicodeScalars111        guard let start = u.firstIndex(where: { !isSpace($0) }), let end = u.lastIndex(where: { !isSpace($0) }) else { return "" }112        return String(u[start...end])113    }114115    /// `s.replace(/\n{3,}/g, "\n\n")`.116    static func collapseNewlines(_ s: String) -> String {117        var out = String.UnicodeScalarView()118        var run = 0119        for u in s.unicodeScalars {120            if u == "\n" {121                run += 1122                if run <= 2 { out.append(u) }123            } else {124                run = 0125                out.append(u)126            }127        }128        return String(out)129    }130131    /// `s.split("\n")` (a CR LF pair is not one separator, unlike Swift's Character view).132    static func splitLines(_ s: String) -> [String] {133        var out: [String] = []134        var cur = String.UnicodeScalarView()135        for u in s.unicodeScalars {136            if u == "\n" { out.append(String(cur)); cur = String.UnicodeScalarView() } else { cur.append(u) }137        }138        out.append(String(cur))139        return out140    }141142    static func replacing(_ s: String, _ target: Unicode.Scalar, with replacement: String) -> String {143        var out = String.UnicodeScalarView()144        for u in s.unicodeScalars { if u == target { out.append(contentsOf: replacement.unicodeScalars) } else { out.append(u) } }145        return String(out)146    }147148    /// The 36-character id following `prefix` when it only holds `[0-9a-f-]` (`/^<prefix>([0-9a-f-]{36})/`).149    static func uuidAfter(prefix: String, in s: String) -> String? {150        let u = Array(s.unicodeScalars), p = Array(prefix.unicodeScalars)151        guard u.count >= p.count + 36, Array(u[0..<p.count]) == p else { return nil }152        let id = u[p.count..<p.count + 36]153        guard id.allSatisfy({ ("0"..."9").contains($0) || ("a"..."f").contains($0) || $0 == "-" }) else { return nil }154        var v = String.UnicodeScalarView()155        v.append(contentsOf: id)156        return String(v)157    }158}159160enum TextTools {161    /// Same set as `BLOCKS` in text.ts.162    static let blocks: Set<String> = Set(["paragraph", "heading", "blockquote", "codeBlock", "bulletList", "orderedList", "listItem", "taskList",163                                          "taskItem", "table", "tableRow", "callout", "details", "detailsSummary", "detailsContent", "aiBlock",164                                          "blockMath", "horizontalRule", "image", "fileBlock", "youtube", "footnote"]).union(NextNodes.blockTypes)165166    static func nodeText(_ n: PMNode) -> String {167        if let t = n.text, !t.isEmpty { return t }168        if let next = NextNodes.text(n) { return next }   // 3.0 nodes: leaf atoms whose text lives in attrs169        if n.type == "hardBreak" { return "\n" }170        if n.type == "mention" { return "@" + (JS.nullish(n.attrs?["label"]).map(JS.string) ?? "") }171        if n.type == "math" || n.type == "inlineMath" || n.type == "blockMath" { return JS.nullish(n.attrs?["latex"]).map(JS.string) ?? "" }172        let kids = n.content ?? []173        return kids.map(nodeText).joined(separator: kids.contains { blocks.contains($0.type) } ? "\n" : "")174    }175176    static func docText(_ doc: PMNode) -> String { JS.trim(JS.collapseNewlines(nodeText(doc))) }177178    static func wordCount(_ s: String) -> Int {179        guard let re = try? NSRegularExpression(pattern: "[\\p{L}\\p{N}]+(?:['’-][\\p{L}\\p{N}]+)*") else { return 0 }180        return re.numberOfMatches(in: s, range: NSRange(location: 0, length: (s as NSString).length))181    }182183    static func preview(_ plain: String, maxLength: Int) -> String {184        guard maxLength > 0 else { return "" }185        for raw in JS.splitLines(plain) {186            var words: [String] = []187            var cur = String.UnicodeScalarView()188            for u in raw.unicodeScalars {189                if JS.isSpace(u) { if !cur.isEmpty { words.append(String(cur)); cur = String.UnicodeScalarView() } } else { cur.append(u) }190            }191            if !cur.isEmpty { words.append(String(cur)) }192            if words.isEmpty { continue }193            let line = words.joined(separator: " ")194            if line.count <= maxLength { return line }195            return JS.trim(String(line.prefix(maxLength - 1))) + "…"196        }197        return ""198    }199200    static func walk(_ n: PMNode, _ visit: (PMNode) -> Void) {201        visit(n)202        for c in n.content ?? [] { walk(c, visit) }203    }204205    static func pageReferences(_ doc: PMNode) -> [String] {206        var ids: [String] = []207        var seen = Set<String>()208        func add(_ id: String) { if seen.insert(id).inserted { ids.append(id) } }209        walk(doc) { n in210            if n.type == "mention", case .string(let id)? = n.attrs?["id"], !id.isEmpty, n.attrs?["kind"] != .string("user") { add(id) }211            if let ref = NextNodes.pageRef(n) { add(ref.pageId) }212            for m in n.allMarks where m.type == "link" {213                if case .string(let href)? = m.attrs?["href"], let id = JS.uuidAfter(prefix: "/p/", in: href) { add(id) }214            }215        }216        return ids217    }218219    static func attachmentReferences(_ doc: PMNode) -> [String] {220        var ids: [String] = []221        var seen = Set<String>()222        func add(_ id: String) { if seen.insert(id).inserted { ids.append(id) } }223        walk(doc) { n in224            for id in NextNodes.attachmentIds(n) { add(id) }   // 3.0 nodes reference their files by id in attrs225            guard n.type == "image" || n.type == "fileBlock" else { return }226            if case .string(let src)? = n.attrs?["src"], let id = JS.uuidAfter(prefix: "/api/files/", in: src) { add(id) }227            if n.type == "fileBlock", case .string(let id)? = n.attrs?["attachmentId"], !id.isEmpty { add(id) }228        }229        return ids230    }231232    static func taskItems(_ doc: PMNode) -> TaskCounts {233        var counts = TaskCounts(total: 0, checked: 0)234        walk(doc) { n in235            guard n.type == "taskItem" else { return }236            counts.total += 1237            if JS.truthy(n.attrs?["checked"]) { counts.checked += 1 }238        }239        return counts240    }241242    /// Port of the server's `docTaskItems`.243    static func taskDetails(_ doc: PMNode) -> [TaskItemDetail] {244        var out: [TaskItemDetail] = []245        walk(doc) { n in246            guard n.type == "taskItem" else { return }247            let a = n.attrs ?? [:]248            let first = (n.content ?? []).first { !Schema.lists.contains($0.type) }249            var words: [String] = []250            var cur = String.UnicodeScalarView()251            for u in (first.map(nodeText) ?? "").unicodeScalars {252                if JS.isSpace(u) { if !cur.isEmpty { words.append(String(cur)); cur = String.UnicodeScalarView() } } else { cur.append(u) }253            }254            if !cur.isEmpty { words.append(String(cur)) }255            let text = String(decoding: Array(words.joined(separator: " ").utf16.prefix(500)), as: UTF16.self)   // `.slice(0, 500)` counts UTF-16 units256            let priority = a["priority"]?.stringValue.flatMap { ["low", "medium", "high"].contains($0) ? TaskPriority(rawValue: $0) : nil }257            out.append(TaskItemDetail(blockId: BlockID.usable(a["id"]), taskId: NextNodes.id(a["taskId"]), checked: JS.truthy(a["checked"]), text: text,258                                      due: a["due"]?.stringValue.flatMap { TaskItemDetail.isDate($0) ? $0 : nil }, priority: priority))259        }260        return out261    }262263    static func citations(_ doc: PMNode) -> [String] {264        var ids: [String] = []265        var seen = Set<String>()266        walk(doc) { n in if let id = NextNodes.refId(n), seen.insert(id).inserted { ids.append(id) } }267        return ids268    }269}270271extension PMNode {272    /// The marks as the server reads them: `marks`, or — when a mark carries unknown keys and the array is therefore273    /// kept raw in `extra` — their type and attrs read from there.274    var allMarks: [PMMark] {275        if let marks { return marks }276        guard case .array(let raw)? = extra["marks"] else { return [] }277        return raw.compactMap { m in278            guard case .string(let type)? = m["type"] else { return nil }279            return PMMark(type: type, attrs: m["attrs"]?.objectValue)280        }281    }282283    /// Text of this node as the server's `nodeText` computes it.284    public var plainText: String { TextTools.nodeText(self) }285}286287extension NoteDocument {288    /// Ids of the pages this note refers to (mentions that are not user mentions, links to `/p/<uuid>`, and since 3.0289    /// the targets of `blockRef` and `canvasLink` nodes), in document290    /// order without duplicates; equal to the server's `extractPageRefs`.291    public func pageReferences() -> [String] { TextTools.pageReferences(pmNode()) }292293    /// Ids of the attachments this note uses: image / file `src` of the form `/api/files/<uuid>`, file294    /// `attachmentId`, and since 3.0 the files of `mermaid` (render), `drawing` (ink, render, PencilKit data), `pdf`295    /// and `audio` nodes; tables included, in document order without duplicates.296    public func attachmentReferences() -> [String] { TextTools.attachmentReferences(pmNode()) }297298    /// Number of task items and of checked ones.299    public func taskItems() -> TaskCounts { TextTools.taskItems(pmNode()) }300301    /// Every task item in document order with its 3.0 attributes (block id, linked task, due date, priority) and302    /// the text of its first block; equal to the server's `docTaskItems`.303    public func taskItemDetails() -> [TaskItemDetail] { TextTools.taskDetails(pmNode()) }304305    /// Ids of the research references cited by `citation` nodes, in document order without duplicates.306    public func citedReferences() -> [String] { TextTools.citations(pmNode()) }307308    /// The server's word count of an arbitrary string.309    public static func wordCount(of text: String) -> Int { TextTools.wordCount(text) }310}311
Files main
Menu

Theme