Sources/OrgApp/RoamNode.swift

49c5da97098ed000be935c19462d4102812b0471
orgstar/Sources/OrgApp/RoamNode.swift

31 lines · 1498 bytes

3 symbols in this file
 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}
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