Follow denote: links and count them as backlinks !385
8 files changed, +100 −7
Layout: unified · split
Sources/OrgApp/LinkFollow.swift +8 −1
| @@ -12,7 +12,7 @@ public enum LinkFollow: Equatable, Sendable { | |||
| 12 | 12 | ||
| 13 | extension DocumentSession { | 13 | extension DocumentSession { |
| 14 | /// Follows a link as `org-link-open` does: files open as buffers (with a `::` search), | 14 | /// Follows a link as `org-link-open` does: files open as buffers (with a `::` search), |
| 15 | /// `id:` links through the index, internal and radio links in the current file, | 15 | /// `id:` and `denote:` links through the index, internal and radio links in the current file, |
| 16 | /// `attachment:` links in their entry's attachment folder, and web and mail links go to | 16 | /// `attachment:` links in their entry's attachment folder, and web and mail links go to |
| 17 | /// their apps. | 17 | /// their apps. |
| 18 | public func follow(_ link: OrgLink, workspace: WorkspaceModel?, isText: (String) -> Bool) -> LinkFollow { | 18 | public func follow(_ link: OrgLink, workspace: WorkspaceModel?, isText: (String) -> Bool) -> LinkFollow { |
| @@ -29,6 +29,13 @@ extension DocumentSession { | |||
| 29 | } | 29 | } |
| 30 | show(location) | 30 | show(location) |
| 31 | return .shown | 31 | return .shown |
| 32 | case "denote": | ||
| 33 | let (identifier, search) = Denote.split(link.path) | ||
| 34 | let paths = ((try? workspace?.index.files()) ?? []).map(\.path) | ||
| 35 | guard let file = Denote.file(withIdentifier: identifier, in: paths) else { | ||
| 36 | return .message("No file has the Denote identifier \(identifier)") | ||
| 37 | } | ||
| 38 | return followFile(OrgLink(type: "file", path: file, searchOption: search, raw: link.raw), isText: isText) | ||
| 32 | case "custom-id": | 39 | case "custom-id": |
| 33 | return searchCurrent("#" + link.path) | 40 | return searchCurrent("#" + link.path) |
| 34 | case "fuzzy": | 41 | case "fuzzy": |
Sources/OrgCore/Completion/CompletionData.swift +1 −1
| @@ -70,5 +70,5 @@ extension OrgCompletion { | |||
| 70 | ] | 70 | ] |
| 71 | 71 | ||
| 72 | /// Link types offered after `[[` besides `#+LINK` abbreviations. | 72 | /// Link types offered after `[[` besides `#+LINK` abbreviations. |
| 73 | static let linkTypes = ["attachment", "doi", "file", "http", "https", "id", "mailto"] | 73 | static let linkTypes = ["attachment", "denote", "doi", "file", "http", "https", "id", "mailto"] |
| 74 | } | 74 | } |
Sources/OrgCore/Links/Backlinks.swift +8 −2
| @@ -1,7 +1,7 @@ | |||
| 1 | import Foundation | 1 | import Foundation |
| 2 | 2 | ||
| 3 | /// Which links point at a file or one of its headings, read as `org-open-at-point` would | 3 | /// Which links point at a file or one of its headings, read as `org-open-at-point` would |
| 4 | /// follow them: `id:`, `file:` with `::*Title` or `::#custom-id`, plain paths, and `*Title`, | 4 | /// follow them: `id:`, `denote:`, `file:` with `::*Title` or `::#custom-id`, plain paths, and `*Title`, |
| 5 | /// `#custom-id` or a heading's exact title within the same file. | 5 | /// `#custom-id` or a heading's exact title within the same file. |
| 6 | public enum Backlinks { | 6 | public enum Backlinks { |
| 7 | public struct Target: Sendable, Equatable { | 7 | public struct Target: Sendable, Equatable { |
| @@ -28,7 +28,13 @@ public enum Backlinks { | |||
| 28 | } | 28 | } |
| 29 | var path: String | 29 | var path: String |
| 30 | var search: String | 30 | var search: String |
| 31 | if link.lowercased().hasPrefix("file:") { | 31 | if link.lowercased().hasPrefix("denote:") { |
| 32 | let (identifier, denoteSearch) = Denote.split(String(link.dropFirst(7))) | ||
| 33 | guard Denote.identifier(ofFile: target.path) == identifier else { return false } | ||
| 34 | path = "" | ||
| 35 | search = denoteSearch ?? "" | ||
| 36 | if search.isEmpty { return target.title == nil } | ||
| 37 | } else if link.lowercased().hasPrefix("file:") { | ||
| 32 | let parts = link.dropFirst(5).components(separatedBy: "::") | 38 | let parts = link.dropFirst(5).components(separatedBy: "::") |
| 33 | path = parts[0] | 39 | path = parts[0] |
| 34 | search = parts.dropFirst().joined(separator: "::") | 40 | search = parts.dropFirst().joined(separator: "::") |
Sources/OrgCore/Links/Denote.swift added +27
| @@ -0,0 +1,27 @@ | |||
| 1 | import Foundation | ||
| 2 | |||
| 3 | /// Denote's file naming, as `denote-retrieve-filename-identifier` reads it: the identifier | ||
| 4 | /// starts the name (`20261119T120000--title__keywords.org`), or follows `@@` when | ||
| 5 | /// `denote-file-name-components-order` puts it elsewhere. | ||
| 6 | public enum Denote { | ||
| 7 | /// The identifier in the file name at `path`, or nil when it has none. | ||
| 8 | public static func identifier(ofFile path: String) -> String? { | ||
| 9 | let name = (path as NSString).lastPathComponent | ||
| 10 | if let match = name.firstMatch(of: /^(\d{8}T\d{6})(?:--|__|==|@@|\.|$)/) { return String(match.1) } | ||
| 11 | if let match = name.firstMatch(of: /@@([^.]+?)(?=--|__|==|@@|\.|$)/) { return String(match.1) } | ||
| 12 | return nil | ||
| 13 | } | ||
| 14 | |||
| 15 | /// A `denote:` link's path split at `::` into the identifier and the search. | ||
| 16 | public static func split(_ path: String) -> (identifier: String, search: String?) { | ||
| 17 | guard let range = path.range(of: "::") else { return (path, nil) } | ||
| 18 | return (String(path[..<range.lowerBound]), String(path[range.upperBound...])) | ||
| 19 | } | ||
| 20 | |||
| 21 | /// The file `denote-get-path-by-id` finds: the one whose name has `identifier`, an Org | ||
| 22 | /// file before any other. | ||
| 23 | public static func file(withIdentifier identifier: String, in paths: [String]) -> String? { | ||
| 24 | let matches = paths.filter { Self.identifier(ofFile: $0) == identifier } | ||
| 25 | return matches.first { ($0 as NSString).pathExtension == "org" } ?? matches.first | ||
| 26 | } | ||
| 27 | } | ||
Sources/OrgCore/Links/Links.swift +3 −3
| @@ -2,7 +2,7 @@ import Foundation | |||
| 2 | 2 | ||
| 3 | /// A link as `org-element-link-parser` reads it. | 3 | /// A link as `org-element-link-parser` reads it. |
| 4 | public struct OrgLink: Sendable, Equatable { | 4 | public struct OrgLink: Sendable, Equatable { |
| 5 | /// `https`, `file`, `id`, `custom-id`, `coderef`, `fuzzy`, or another registered type. | 5 | /// `https`, `file`, `id`, `denote`, `custom-id`, `coderef`, `fuzzy`, or another registered type. |
| 6 | public let type: String | 6 | public let type: String |
| 7 | public let path: String | 7 | public let path: String |
| 8 | /// What follows `::` in a file link. | 8 | /// What follows `::` in a file link. |
| @@ -22,9 +22,9 @@ public struct OrgLink: Sendable, Equatable { | |||
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | public enum Links { | 24 | public enum Links { |
| 25 | /// `org-link-parameters` in Emacs with Org's default modules, and org-attach's. | 25 | /// `org-link-parameters` in Emacs with Org's default modules, org-attach's and Denote's. |
| 26 | static let types = [ | 26 | static let types = [ |
| 27 | "attachment", "id", "eww", "rmail", "mhe", "irc", "info", "gnus", "docview", "bibtex", "bbdb", "w3m", "doi", "file+sys", "file+emacs", | 27 | "attachment", "id", "denote", "eww", "rmail", "mhe", "irc", "info", "gnus", "docview", "bibtex", "bbdb", "w3m", "doi", "file+sys", "file+emacs", |
| 28 | "shell", "news", "mailto", "https", "http", "ftp", "shortdoc", "help", "file", "elisp", | 28 | "shell", "news", "mailto", "https", "http", "ftp", "shortdoc", "help", "file", "elisp", |
| 29 | ] | 29 | ] |
| 30 | 30 | ||
Tests/OrgAppTests/AppTests.swift +18
| @@ -1422,6 +1422,24 @@ struct LinkFollowTests { | |||
| 1422 | #expect(follow("shell:rm -rf x") == .message("orgstar doesn't run shell links")) | 1422 | #expect(follow("shell:rm -rf x") == .message("orgstar doesn't run shell links")) |
| 1423 | } | 1423 | } |
| 1424 | 1424 | ||
| 1425 | @Test func followsDenoteLinks() async throws { | ||
| 1426 | let notes = try Folder() | ||
| 1427 | let a = try notes.write("a.org", "* Start\n") | ||
| 1428 | let note = try notes.write("sub/20261119T120000--a-note__kw.org", "#+title: A note\n* Inside\n:PROPERTIES:\n:CUSTOM_ID: in\n:END:\n") | ||
| 1429 | _ = try notes.write("20261119T120000--a-note.pdf", "pdf") | ||
| 1430 | let workspace = try WorkspaceModel(directory: nil) | ||
| 1431 | try await workspace.addRoot(notes.url) | ||
| 1432 | let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r"))) | ||
| 1433 | session.open(a.path) | ||
| 1434 | func follow(_ raw: String) -> LinkFollow { session.follow(Links.parse(bracket: raw), workspace: workspace, isText: { $0.hasSuffix(".org") }) } | ||
| 1435 | |||
| 1436 | #expect(follow("denote:20261119T120000") == .shown && session.path == note.path) | ||
| 1437 | session.open(a.path) | ||
| 1438 | #expect(follow("denote:20261119T120000::#in") == .shown && session.path == note.path) | ||
| 1439 | #expect(session.revealRequest?.offset == (try String(contentsOfFile: note.path, encoding: .utf8) as NSString).range(of: "* Inside").location) | ||
| 1440 | #expect(follow("denote:20200101T000000") == .message("No file has the Denote identifier 20200101T000000")) | ||
| 1441 | } | ||
| 1442 | |||
| 1425 | @Test func followsAttachmentAndRadioLinks() throws { | 1443 | @Test func followsAttachmentAndRadioLinks() throws { |
| 1426 | let notes = try Folder() | 1444 | let notes = try Folder() |
| 1427 | let text = "* Task\n:PROPERTIES:\n:ID: ab12-cd\n:END:\nSee [[attachment:scan.pdf]] and <<<the plan>>>.\n* Later\nAs in The Plan.\n" | 1445 | let text = "* Task\n:PROPERTIES:\n:ID: ab12-cd\n:END:\nSee [[attachment:scan.pdf]] and <<<the plan>>>.\n* Later\nAs in The Plan.\n" |
Tests/OrgCoreTests/BacklinkTests.swift +11
| @@ -24,6 +24,17 @@ struct BacklinkTests { | |||
| 24 | #expect(!links("*Fix the sink", from: "/notes/b.org", to: heading)) | 24 | #expect(!links("*Fix the sink", from: "/notes/b.org", to: heading)) |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | @Test func followsDenoteLinks() { | ||
| 28 | let file = Backlinks.Target(path: "/notes/20261119T120000--a-note__kw.org") | ||
| 29 | let heading = Backlinks.Target(path: file.path, title: "Inside", customID: "in") | ||
| 30 | #expect(Backlinks.links("denote:20261119T120000", from: "/notes/b.org", to: file)) | ||
| 31 | #expect(!Backlinks.links("denote:20261119T120000", from: "/notes/b.org", to: heading)) | ||
| 32 | #expect(Backlinks.links("denote:20261119T120000::#in", from: "/notes/b.org", to: heading)) | ||
| 33 | #expect(Backlinks.links("denote:20261119T120000::*Inside", from: "/notes/b.org", to: heading)) | ||
| 34 | #expect(!Backlinks.links("denote:20261119T120001", from: "/notes/b.org", to: file)) | ||
| 35 | #expect(!Backlinks.links("denote:20261119T120000", from: "/notes/b.org", to: Backlinks.Target(path: "/notes/a.org"))) | ||
| 36 | } | ||
| 37 | |||
| 27 | @Test func contextLineShowsLinksAsTheyRead() throws { | 38 | @Test func contextLineShowsLinksAsTheyRead() throws { |
| 28 | let body = ":PROPERTIES:\n:ID: x\n:END:\nFirst line.\nSee [[id:s-1][the sink]] and [[file:a.org]] today.\n" | 39 | let body = ":PROPERTIES:\n:ID: x\n:END:\nFirst line.\nSee [[id:s-1][the sink]] and [[file:a.org]] today.\n" |
| 29 | let context = try #require(Backlinks.context(in: body, link: "id:s-1")) | 40 | let context = try #require(Backlinks.context(in: body, link: "id:s-1")) |
Tests/OrgCoreTests/DenoteTests.swift added +24
| @@ -0,0 +1,24 @@ | |||
| 1 | import Testing | ||
| 2 | @testable import OrgCore | ||
| 3 | |||
| 4 | struct DenoteTests { | ||
| 5 | @Test func readsIdentifiersFromFileNames() { | ||
| 6 | #expect(Denote.identifier(ofFile: "/n/20261119T120000--a-note__kw1_kw2.org") == "20261119T120000") | ||
| 7 | #expect(Denote.identifier(ofFile: "20261119T120000.org") == "20261119T120000") | ||
| 8 | #expect(Denote.identifier(ofFile: "20261119T120000==1a--title.org") == "20261119T120000") | ||
| 9 | #expect(Denote.identifier(ofFile: "--a-note@@20261119T120000__kw.org") == "20261119T120000") | ||
| 10 | #expect(Denote.identifier(ofFile: "a-note.org") == nil) | ||
| 11 | #expect(Denote.identifier(ofFile: "20261119-diary.org") == nil) | ||
| 12 | } | ||
| 13 | |||
| 14 | @Test func splitsSearchOptions() { | ||
| 15 | #expect(Denote.split("20261119T120000") == ("20261119T120000", nil)) | ||
| 16 | #expect(Denote.split("20261119T120000::#h") == ("20261119T120000", "#h")) | ||
| 17 | } | ||
| 18 | |||
| 19 | @Test func prefersOrgFiles() { | ||
| 20 | let paths = ["/n/20261119T120000--a.pdf", "/n/20261119T120000--a.org", "/n/20261119T120001--b.org"] | ||
| 21 | #expect(Denote.file(withIdentifier: "20261119T120000", in: paths) == "/n/20261119T120000--a.org") | ||
| 22 | #expect(Denote.file(withIdentifier: "20261119T120002", in: paths) == nil) | ||
| 23 | } | ||
| 24 | } | ||