Sources/OrgIndex/RoamProperties.swift

4d8e517de5aef10e915fb482735feba5435a4a21
orgstar/Sources/OrgIndex/RoamProperties.swift

86 lines · 3463 bytes

 1import Foundation
 2
 3/// A node's `ROAM_ALIASES`, `ROAM_REFS` and `ROAM_EXCLUDE` as written, read as org-roam 2.3's
 4/// `org-roam-db-update-file` reads them.
 5public struct RoamProperties: Sendable, Equatable {
 6    public var aliases: String?
 7    public var refs: String?
 8    public var exclude: String?
 9
10    public init(aliases: String? = nil, refs: String? = nil, exclude: String? = nil) {
11        self.aliases = aliases
12        self.refs = refs
13        self.exclude = exclude
14    }
15
16    /// From a heading's own property rows; inherited values don't count.
17    init(_ rows: [PropertyRow]) {
18        func own(_ key: String) -> String? { rows.first { $0.key == key && !$0.inherited }?.value }
19        self.init(aliases: own("ROAM_ALIASES"), refs: own("ROAM_REFS"), exclude: own("ROAM_EXCLUDE"))
20    }
21
22    /// `org-roam-db-node-p`: `(org-entry-get (point) "ROAM_EXCLUDE")` is non-nil, which any
23    /// value but `nil` is.
24    public var excluded: Bool { exclude.map { $0.trimmingCharacters(in: .whitespaces) != "nil" } ?? false }
25
26    public var aliasList: [String] { aliases.map(Self.split) ?? [] }
27
28    /// `org-roam-db-insert-refs`: `@key` and the keys of `[cite:…]` as `cite:key`, and links,
29    /// bracketed or plain, as their targets. Anything else is skipped, as org-roam skips it
30    /// with a warning.
31    public var refList: [String] {
32        var result: [String] = []
33        for ref in refs.map(Self.split) ?? [] {
34            if ref.hasPrefix("@") {
35                result.append("cite:" + ref.dropFirst())
36            } else if ref.hasPrefix("[cite:") {
37                result += ref.matches(of: /@([^\s;\]]+)/).map { "cite:" + $0.1 }
38            } else if let bracket = ref.firstMatch(of: /\[\[([^\]]+)\](?:\[[^\]]*\])?\]/) {
39                result.append(String(bracket.1))
40            } else if let plain = ref.firstMatch(of: Self.plainLink) {
41                result.append(String(plain.0))
42            }
43        }
44        return result
45    }
46
47    /// Link types `org-link-plain-re` knows without other packages loaded.
48    nonisolated(unsafe) static let plainLink = /(?:https?|ftp|mailto|doi|file|news|shell|elisp|help|info|id|attachment|bbdb|docview|gnus|irc|rmail|w3m|eww):[^\s\[\]<>]+/
49
50    /// `split-string-and-unquote`: words separated by whitespace, a double-quoted string read as
51    /// one, with its escapes.
52    public static func split(_ string: String) -> [String] {
53        var words: [String] = []
54        var current = ""
55        var chars = string.makeIterator()
56        func flush() {
57            if !current.isEmpty { words.append(current) }
58            current = ""
59        }
60        while let c = chars.next() {
61            if c == "\"" {
62                flush()
63                var quoted = ""
64                while let q = chars.next(), q != "\"" {
65                    if q == "\\", let escaped = chars.next() {
66                        switch escaped {
67                        case "n": quoted.append("\n")
68                        case "t": quoted.append("\t")
69                        case "\n": break
70                        default: quoted.append(escaped)
71                        }
72                    } else {
73                        quoted.append(q)
74                    }
75                }
76                words.append(quoted)
77            } else if c.isWhitespace {
78                flush()
79            } else {
80                current.append(c)
81            }
82        }
83        flush()
84        return words
85    }
86}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form