Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: path resolution for non-existent files when using :org-mode/insert-file-link #10889

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/main/frontend/handler/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,6 @@
page)
(let [journal? (date/valid-journal-title? page)
ref-file-path (str
(if (or (util/electron?) (mobile-util/native-platform?))
(-> (config/get-repo-dir (state/get-current-repo))
js/decodeURI
(string/replace #"/+$" "")
(str "/"))
"")
(get-directory journal?)
"/"
(get-file-name journal? page)
Expand Down
18 changes: 10 additions & 8 deletions src/main/frontend/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1096,13 +1096,16 @@
parts-2 (directories-f another-file-path)
[parts-1 parts-2] (remove-common-preceding parts-1 parts-2)
another-file-name (last (string/split another-file-path "/"))]
(->> (concat
(if (seq parts-1)
(repeat (count parts-1) "..")
["."])
parts-2
[another-file-name])
string-join-path)))
(->>
(concat
;; when another-file-path starts with "/", don't append any ".."
(when (not (= (first parts-2) ""))
(if (seq parts-1)
(repeat (count parts-1) "..")
["."]))
parts-2
[another-file-name])
string-join-path)))

;; Copied from https://github.com/tonsky/datascript-todo
#?(:clj
Expand Down Expand Up @@ -1200,7 +1203,6 @@
(async/close! ch))))
ch)))


#?(:cljs
(defn trace!
[]
Expand Down
201 changes: 194 additions & 7 deletions src/test/frontend/handler/page_test.cljs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
(ns frontend.handler.page-test
;; namespace local config for private function tests
{:clj-kondo/config {:linters {:private-call {:level :off}}}}
(:require [cljs.test :refer [deftest are]]
(:require [cljs.test :refer [deftest is are testing use-fixtures]]
[clojure.string :as string]
[frontend.util :as util]
[frontend.handler.page :as page-handler]))
[frontend.db.utils :as db-utils]
[frontend.state :as state]
[frontend.mobile.util :as mobile-util]
[frontend.handler.page :as page-handler]
[frontend.test.helper :as test-helper]))

(defn- replace-page-ref!
[content old-name new-name]
Expand Down Expand Up @@ -38,6 +42,189 @@
(replace-page-ref! old-name new-name)
(page-handler/replace-tag-ref! old-name new-name))))

(use-fixtures :each {:before test-helper/start-test-db!
:after test-helper/destroy-test-db!})

;; A hacky way of setting :block/page. I'm not sure why it isn't set by default
;; in this test case
(defn- add-block-page
[block]
(assoc block :block/page block))

;; Test resolution of files in the journal folder
(defn- test-get-page-ref-text-org-journals
[]
(testing "Testing resolution of paths from within the journals folder."
;; journals/failure.org
(with-redefs [state/get-edit-block (constantly (add-block-page (db-utils/pull [:block/name "failure"])))]
;; check non-existent file resolution (platform-dependent) with space in name
(is (= "[[file:../pages/test case.org][Test Case]]" (frontend.handler.page/get-page-ref-text "Test Case")))
;; check existing file resolution (not platform-dependent)
(is (= "[[file:../pages/foo.org][foo]]" (frontend.handler.page/get-page-ref-text "foo")))
;; check existing file resolution created with absolute path
(is (= "[[file:/test-db/fooart.org][fooart]]" (frontend.handler.page/get-page-ref-text "fooart")))
;; check existing journal file resolution (not platform-dependent)
(is (= "[[file:./2024-01-16.org][2024-01-16]]" (frontend.handler.page/get-page-ref-text "2024-01-16")))
;; check non-existent journal file resolution (platform dependent)
(is (= "[[file:./2024-01-15.org][2024-01-15]]" (frontend.handler.page/get-page-ref-text "2024-01-15")))
;; check existent journal file outside of journal folder
(is (= "[[file:../2024-01-13.org][2024-01-13]]" (frontend.handler.page/get-page-ref-text "2024-01-13")))
;; check existent journal file with absolute path
(is (= "[[file:/test-db/2024-01-01.org][2024-01-01]]" (frontend.handler.page/get-page-ref-text "2024-01-01")))
;; Check resolution of existing file in non-standard location
(is (= "[[file:../nonstandard/location.org][location]]" (frontend.handler.page/get-page-ref-text "location")))
;; Check resolution of existing file at root of graph
(is (= "[[file:../root.org][root]]" (frontend.handler.page/get-page-ref-text "root")))
;;
)

;; journals/2024-01-16.org
(with-redefs [state/get-edit-block (constantly (add-block-page (db-utils/pull [:block/name "2024-01-16"])))]
;; check non-existent file resolution (platform-dependent) with space in name
(is (= "[[file:../pages/test case.org][Test Case]]" (frontend.handler.page/get-page-ref-text "Test Case")))
;; check existing file resolution (not platform-dependent)
(is (= "[[file:../pages/foo.org][foo]]" (frontend.handler.page/get-page-ref-text "foo")))
;; check existing file resolution created with absolute path
(is (= "[[file:/test-db/fooart.org][fooart]]" (frontend.handler.page/get-page-ref-text "fooart")))
;; check existing journal file resolution (not platform-dependent)
(is (= "[[file:./2024-01-16.org][2024-01-16]]" (frontend.handler.page/get-page-ref-text "2024-01-16")))
;; check non-existent journal file resolution (platform dependent)
(is (= "[[file:./2024-01-15.org][2024-01-15]]" (frontend.handler.page/get-page-ref-text "2024-01-15")))
;; check existent journal file outside of journal folder
(is (= "[[file:../2024-01-13.org][2024-01-13]]" (frontend.handler.page/get-page-ref-text "2024-01-13")))
;; check existent journal file with absolute path
(is (= "[[file:/test-db/2024-01-01.org][2024-01-01]]" (frontend.handler.page/get-page-ref-text "2024-01-01")))
;; Check resolution of existing file in non-standard location
(is (= "[[file:../nonstandard/location.org][location]]" (frontend.handler.page/get-page-ref-text "location")))
;; Check resolution of existing file at root of graph
(is (= "[[file:../root.org][root]]" (frontend.handler.page/get-page-ref-text "root")))
;;
)))

;; Test resolution of files at non-standard locations
(defn- test-get-page-ref-text-org-nonstandard
[]
(testing "Resolution of files from within non-standard folders."
;; root.org
(with-redefs [state/get-edit-block (constantly (add-block-page (db-utils/pull [:block/name "root"])))]
;; check non-existent file resolution (platform-dependent) with space in name
(is (= "[[file:./pages/test case.org][Test Case]]" (frontend.handler.page/get-page-ref-text "Test Case")))
;; check existing file resolution (not platform-dependent)
(is (= "[[file:./pages/foo.org][foo]]" (frontend.handler.page/get-page-ref-text "foo")))
;; check existing file resolution created with absolute path
(is (= "[[file:/test-db/fooart.org][fooart]]" (frontend.handler.page/get-page-ref-text "fooart")))
;; check existing journal file resolution (not platform-dependent)
(is (= "[[file:./journals/2024-01-16.org][2024-01-16]]" (frontend.handler.page/get-page-ref-text "2024-01-16")))
;; check non-existent journal file resolution (platform dependent)
(is (= "[[file:./journals/2024-01-15.org][2024-01-15]]" (frontend.handler.page/get-page-ref-text "2024-01-15")))
;; check existent journal file outside of journal folder
(is (= "[[file:./2024-01-13.org][2024-01-13]]" (frontend.handler.page/get-page-ref-text "2024-01-13")))
;; check existent journal file with absolute path
(is (= "[[file:/test-db/2024-01-01.org][2024-01-01]]" (frontend.handler.page/get-page-ref-text "2024-01-01")))
;; Check resolution of existing file in non-standard location
(is (= "[[file:./nonstandard/location.org][location]]" (frontend.handler.page/get-page-ref-text "location")))
;;
)

;; nonstandard/location.org
(with-redefs [state/get-edit-block (constantly (add-block-page (db-utils/pull [:block/name "location"])))]
;; check non-existent file resolution (platform-dependent) with space in name
(is (= "[[file:../pages/test case.org][Test Case]]" (frontend.handler.page/get-page-ref-text "Test Case")))
;; check existing file resolution (not platform-dependent)
(is (= "[[file:../pages/foo.org][foo]]" (frontend.handler.page/get-page-ref-text "foo")))
;; check existing file resolution created with absolute path
(is (= "[[file:/test-db/fooart.org][fooart]]" (frontend.handler.page/get-page-ref-text "fooart")))
;; check existing journal file resolution (not platform-dependent)
(is (= "[[file:../journals/2024-01-16.org][2024-01-16]]" (frontend.handler.page/get-page-ref-text "2024-01-16")))
;; check non-existent journal file resolution (platform dependent)
(is (= "[[file:../journals/2024-01-15.org][2024-01-15]]" (frontend.handler.page/get-page-ref-text "2024-01-15")))
;; check existent journal file outside of journal folder
(is (= "[[file:../2024-01-13.org][2024-01-13]]" (frontend.handler.page/get-page-ref-text "2024-01-13")))
;; check existent journal file with absolute path
(is (= "[[file:/test-db/2024-01-01.org][2024-01-01]]" (frontend.handler.page/get-page-ref-text "2024-01-01")))
;; Check resolution of existing file at root of graph
(is (= "[[file:../root.org][root]]" (frontend.handler.page/get-page-ref-text "root")))
;;
)))

;; Test resolution of files in pages folder
(defn- test-get-page-ref-text-org-pages
[]
(testing "Resolution of files from within the pages/ folder."
;; /pages/foo.org
(with-redefs [state/get-edit-block (constantly (add-block-page (db-utils/pull [:block/name "foo"])))]
;; check non-existent file resolutino (platform-dependent) with space in name

Check warning on line 156 in src/test/frontend/handler/page_test.cljs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"resolutino" should be "resolutions" or "resolution".
(is (= "[[file:./test case.org][Test Case]]" (frontend.handler.page/get-page-ref-text "Test Case")))
;; check existing file resolution created with absolute path
(is (= "[[file:/test-db/fooart.org][fooart]]" (frontend.handler.page/get-page-ref-text "fooart")))
;; check existing file resolution (not platform-dependent) for journal
(is (= "[[file:../journals/2024-01-16.org][2024-01-16]]" (frontend.handler.page/get-page-ref-text "2024-01-16")))
;; check non-existent journal file resolution (platform dependent)
(is (= "[[file:../journals/2024-01-15.org][2024-01-15]]" (frontend.handler.page/get-page-ref-text "2024-01-15")))
;; check existent journal file outside of journal folder
(is (= "[[file:../2024-01-13.org][2024-01-13]]" (frontend.handler.page/get-page-ref-text "2024-01-13")))
;; check existent journal file with absolute path
(is (= "[[file:/test-db/2024-01-01.org][2024-01-01]]" (frontend.handler.page/get-page-ref-text "2024-01-01")))
;; Check resolution of existing file at root of graph
(is (= "[[file:../root.org][root]]" (frontend.handler.page/get-page-ref-text "root")))
;; Check resolution of existing file in non-standard location
(is (= "[[file:../nonstandard/location.org][location]]" (frontend.handler.page/get-page-ref-text "location")))
;;
)))

;; we want the same resolution of these pasts /not/ dependent on platform
(defn- test-get-page-ref-text-org
[]
(with-redefs [state/get-current-repo (constantly test-helper/test-db)]

(test-helper/with-config {:preferred-format "Org"
:org-mode/insert-file-link? true
:journal/page-title-format "yyyy-MM-dd"
:journal/file-name-format "yyyy-MM-dd"}

(test-helper/load-test-files [{:file/path "pages/foo.org"
:file/content "* Normal page in the pages folder."}
{:file/path "2024-01-13.org"
:file/content "* Journal file at root of the graph."}
{:file/path "journals/2024-01-16.org"
:file/content "* Journal file in journal folder."}
{:file/path "journals/failure.org"
:file/content "* Non-standard journal file in the journal folder."}
{:file/path "root.org"
:file/content "* This file is at the root of the graph."}
{:file/path "nonstandard/location.org"
:file/content "* This file is in a non-standard location."}
;; hopefully these don't happen in practice, but we should at
;; least parse them in a sane way instead of giving an incorrect
;; parsed file:// url
{:file/path "/test-db/fooart.org"
:file/content "* Absolute Paths?"}
{:file/path "/test-db/2024-01-01.org"
:file/content "* Absolute Paths?"}])

(test-get-page-ref-text-org-journals)
(test-get-page-ref-text-org-pages)
(test-get-page-ref-text-org-nonstandard)
;;
)))

(deftest ^:focus test-get-page-ref-text-mobile
(testing "test page reference resolution for mobile-native platform."

(with-redefs [mobile-util/native-platform? (constantly true)]
(test-get-page-ref-text-org))))

(deftest ^:focus test-get-page-ref-text-native
(testing "test page reference resolution for org on native."
(test-get-page-ref-text-org)))

;; In theory this test should work, but electron does seem to work with unit tests..
;; (deftest ^:focus test-get-page-ref-text-electron
;; (testing "test page reference resolution for org on electron."

;; (with-redefs [util/electron? (constantly true)]
;; (test-get-page-ref-text-org))))

(deftest test-replace-page-ref!
(are [x y] (= (let [[content old-name new-name] x]
(replace-page-ref! content old-name new-name))
Expand All @@ -48,7 +235,7 @@

["bla [[file:./foo.org][foo]] bla" "foo" "bar"]
"bla [[file:./bar.org][bar]] bla"

["bla [[file:./logseq.foo.org][logseq/foo]] bla" "logseq/foo" "logseq/bar"]
"bla [[file:./logseq.bar.org][logseq/bar]] bla"

Expand Down Expand Up @@ -113,10 +300,10 @@
(are [x y] (= (let [[content old-name new-name] x]
(replace-old-page! content old-name new-name))
y)
["#foo bla [[foo]] bla #foo" "foo" "bar"]
"#bar bla [[bar]] bla #bar"
["#foo bla [[foo]] bla #foo" "foo" "bar"]
"#bar bla [[bar]] bla #bar"

["#logseq/foo bla [[logseq/foo]] bla [[file:./pages/logseq.foo.org][logseq/foo]] bla #logseq/foo" "logseq/foo" "logseq/bar"]
"#logseq/bar bla [[logseq/bar]] bla [[file:./pages/logseq.bar.org][logseq/bar]] bla #logseq/bar"))
["#logseq/foo bla [[logseq/foo]] bla [[file:./pages/logseq.foo.org][logseq/foo]] bla #logseq/foo" "logseq/foo" "logseq/bar"]
"#logseq/bar bla [[logseq/bar]] bla [[file:./pages/logseq.bar.org][logseq/bar]] bla #logseq/bar"))

#_(cljs.test/test-ns 'frontend.handler.page-test)
6 changes: 6 additions & 0 deletions src/test/frontend/util_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@
(is (= (config/ext-of-image? "file.svg") true))
(is (= (config/ext-of-image? "a.file.png") true))
(is (= (config/ext-of-image? "file.tiff") false))))

(deftest ^:focus test-get-relative-path
(testing "test relative path resolution"
(is (= "../../foo" (util/get-relative-path "/User/foo/tester/file.org" "/User/foo/")))
(is (= "../pages/Org Mode.org" (util/get-relative-path "/User/foo/journal/2024-01-15.org" "/User/foo/pages/Org Mode.org")))
(is (= "../journal/2024-01-15.org" (util/get-relative-path "/User/foo/pages/Org Mode.org" "/User/foo/journal/2024-01-15.org")))))