fs.cljs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ;; TODO: move all file path related util functions to here (excepts those fit graph-parser)
  2. (ns frontend.util.fs
  3. "Misc util fns built on top of frontend.fs"
  4. (:require ["path" :as node-path]
  5. [frontend.util :as util]
  6. [logseq.graph-parser.util :as gp-util]
  7. [clojure.string :as string]
  8. [frontend.state :as state]
  9. [frontend.fs :as fs]
  10. [frontend.config :as config]
  11. [promesa.core :as p]
  12. [cljs.reader :as reader]
  13. [frontend.worker.file.util :as wfu]))
  14. ;; NOTE: This is not the same ignored-path? as src/electron/electron/utils.cljs.
  15. ;; The assets directory is ignored.
  16. ;;
  17. ;; When in nfs-mode, dir is "", path is relative path to graph dir.
  18. ;; When in native-mode, dir and path are absolute paths.
  19. (defn ignored-path?
  20. "Ignore path for ls-dir-files-with-handler! and reload-dir!"
  21. [dir path]
  22. (let [ignores ["." ".recycle" "node_modules" "logseq/bak"
  23. "logseq/version-files" "logseq/graphs-txid.edn"]]
  24. (when (string? path)
  25. (or
  26. (some #(string/starts-with? path
  27. (if (= dir "")
  28. %
  29. (str dir "/" %))) ignores)
  30. (some #(string/includes? path (if (= dir "")
  31. (str "/" % "/")
  32. (str % "/"))) ignores)
  33. (some #(string/ends-with? path %)
  34. [".DS_Store" "logseq/graphs-txid.edn"])
  35. ;; hidden directory or file
  36. (let [relpath (node-path/relative dir path)]
  37. (or (re-find #"/\.[^.]+" relpath)
  38. (re-find #"^\.[^.]+" relpath)))
  39. (let [path (string/lower-case path)]
  40. (and
  41. (not (string/blank? (node-path/extname path)))
  42. (not
  43. (some #(string/ends-with? path %)
  44. [".md" ".markdown" ".org" ".js" ".edn" ".css"]))))))))
  45. (defn read-graphs-txid-info
  46. [root]
  47. (when (string? root)
  48. (p/let [exists? (fs/file-exists? root "logseq/graphs-txid.edn")]
  49. (when exists?
  50. (-> (p/let [txid-str (fs/read-file root "logseq/graphs-txid.edn")
  51. txid-meta (and txid-str (reader/read-string txid-str))]
  52. txid-meta)
  53. (p/catch
  54. (fn [^js e]
  55. (js/console.error "[fs read txid data error]" e))))))))
  56. (defn inflate-graphs-info
  57. [graphs]
  58. (if (seq graphs)
  59. (p/all (for [{:keys [root] :as graph} graphs]
  60. (p/let [sync-meta (read-graphs-txid-info root)]
  61. (if sync-meta
  62. (assoc graph
  63. :sync-meta sync-meta
  64. :GraphUUID (second sync-meta))
  65. graph))))
  66. []))
  67. (defn read-repo-file
  68. [repo-url file-rpath]
  69. (when-let [repo-dir (config/get-repo-dir repo-url)]
  70. (fs/read-file repo-dir file-rpath)))
  71. (def include-reserved-chars? wfu/include-reserved-chars?)
  72. (def windows-reserved-filebodies wfu/windows-reserved-filebodies)
  73. (def file-name-sanity wfu/file-name-sanity)
  74. (defn create-title-property?
  75. [page-name]
  76. (and (string? page-name)
  77. (let [filename-format (state/get-filename-format)
  78. file-name (file-name-sanity page-name filename-format)
  79. page-name' (gp-util/title-parsing file-name filename-format)
  80. result (or (not= page-name page-name')
  81. (include-reserved-chars? file-name))]
  82. result)))