diff options
| author | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-06-15 14:35:36 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <jj@r.bdr.sh> | 2026-06-16 14:47:30 +0200 |
| commit | 657aa83a5ca24dc35572c040df64a0abb85e8612 (patch) | |
| tree | fe0b6fd425d8cad9dc6a28ada4ad8600e42b367e /Sources/NorgUI/Helpers | |
Initial extraction from Norganize
Diffstat (limited to 'Sources/NorgUI/Helpers')
| -rw-r--r-- | Sources/NorgUI/Helpers/NorgLinkURL.swift | 25 | ||||
| -rw-r--r-- | Sources/NorgUI/Helpers/Ordinals.swift | 32 |
2 files changed, 57 insertions, 0 deletions
diff --git a/Sources/NorgUI/Helpers/NorgLinkURL.swift b/Sources/NorgUI/Helpers/NorgLinkURL.swift new file mode 100644 index 0000000..9b73fe3 --- /dev/null +++ b/Sources/NorgUI/Helpers/NorgLinkURL.swift @@ -0,0 +1,25 @@ +import Foundation +import NorgKit + +public enum NorgLinkURL { + public static let scheme = "norgui-link" + + public static func encode(_ link: InlineLink) -> URL? { + var components = URLComponents() + components.scheme = scheme + components.host = link.kind == .anchor ? "anchor" : "link" + if let target = link.target { + components.queryItems = [URLQueryItem(name: "target", value: target)] + } + return components.url + } + + public static func decode(_ url: URL) -> InlineLink? { + guard url.scheme == scheme else { return nil } + let kind: InlineLink.Kind = url.host == "anchor" ? .anchor : .link + let target = + URLComponents(url: url, resolvingAgainstBaseURL: false)? + .queryItems?.first { $0.name == "target" }?.value + return InlineLink(kind: kind, target: target) + } +} diff --git a/Sources/NorgUI/Helpers/Ordinals.swift b/Sources/NorgUI/Helpers/Ordinals.swift new file mode 100644 index 0000000..649c3e0 --- /dev/null +++ b/Sources/NorgUI/Helpers/Ordinals.swift @@ -0,0 +1,32 @@ +import NorgKit + +/// Assigns sequential numbers to ordered list items. +func orderedOrdinals(_ blocks: [NorgBlock]) -> [Int?] { + var counters: [Int: Int] = [:] + return blocks.map { block in + switch block { + case .orderedListItem(let level, _, _, _): + counters = counters.filter { $0.key <= level } + counters[level, default: 0] += 1 + return counters[level] + case .unorderedListItem(let level, _, _, _): + counters = counters.filter { $0.key < level } + return nil + default: + counters.removeAll() + return nil + } + } +} + +/// Numbers the ordered list items within a single tree branch. +func ordinals(forSiblings nodes: [NorgNode]) -> [Int?] { + var counter = 0 + return nodes.map { node in + if case .orderedListItem = node.block { + counter += 1 + return counter + } + return nil + } +} |