Sources/OrgApp/RoamNode.swift
cmc org-roam: aliases, refs, excludes, chooser tags, template choice, capture buffer
ed22b081d6 2 hours ago31 lines · 1498 bytes
1import Foundation
2import OrgCore
3
4/// Where new org-roam nodes go, and their slugs.
5public enum RoamNode {
6 /// `org-roam-directory`: the setting, else the first folder.
7 public static func directory(roots: [String], defaults: UserDefaults = .standard) -> String? {
8 let roam = OrgPreferences.roamDirectory(defaults)
9 return roam.isEmpty ? roots.first : OrgAttach.expand(roam, in: roots.first ?? OrgPaths.home)
10 }
11
12 /// `org-roam-node-slugify`: without the combining marks org-roam drops, everything but
13 /// letters and digits as one `_`, none at either end, lower case.
14 public static func slug(_ title: String) -> String {
15 let trimmed: Set<UInt32> = [0x300, 0x301, 0x302, 0x303, 0x304, 0x306, 0x307, 0x308, 0x309, 0x30A, 0x30B, 0x30C,
16 0x31B, 0x323, 0x324, 0x325, 0x327, 0x32D, 0x32E, 0x330, 0x331]
17 let decomposed = title.decomposedStringWithCanonicalMapping.unicodeScalars.filter { !trimmed.contains($0.value) }
18 let composed = String(String.UnicodeScalarView(decomposed)).precomposedStringWithCanonicalMapping
19 var out = ""
20 for character in composed {
21 if character.isLetter || character.isNumber {
22 out.append(character)
23 } else if out.last != "_" {
24 out.append("_")
25 }
26 }
27 while out.first == "_" { out.removeFirst() }
28 while out.last == "_" { out.removeLast() }
29 return out.lowercased()
30 }
31}