Skip to content

Commit

Permalink
Add :hidden config support to graph-parser.cli
Browse files Browse the repository at this point in the history
Also fix lints and tests from previous commits
  • Loading branch information
logseq-cldwalker authored and tiensonqin committed May 18, 2023
1 parent c86fd3c commit 47508ac
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
frontend.mixins mixins
frontend.mobile.util mobile-util
frontend.page page
frontend.schema.handler.common-config common-config-schema
frontend.search search
frontend.state state
frontend.template template
Expand All @@ -97,6 +98,8 @@
frontend.util.thingatpt thingatpt
lambdaisland.glogi log
logseq.common.path path
logseq.common.graph common-graph
logseq.common.config common-config
logseq.graph-parser graph-parser
logseq.graph-parser.text text
logseq.graph-parser.block gp-block
Expand Down
3 changes: 3 additions & 0 deletions deps/common/.carve/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:paths ["src"]
:api-namespaces [logseq.common.path]
:report {:format :ignore}}
4 changes: 4 additions & 0 deletions deps/common/.carve/ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
;; API fn
logseq.common.config/remove-hidden-files
;; API fn
logseq.common.graph/get-files
25 changes: 25 additions & 0 deletions deps/common/src/logseq/common/config.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns logseq.common.config
"This ns provides common fns related to user config"
(:require [clojure.string :as string]))

(defn- hidden?
[path patterns]
(let [path (if (and (string? path)
(= \/ (first path)))
(subs path 1)
path)]
(some (fn [pattern]
(let [pattern (if (and (string? pattern)
(not= \/ (first pattern)))
(str "/" pattern)
pattern)]
(string/starts-with? (str "/" path) pattern))) patterns)))

(defn remove-hidden-files
"Removes files that match a pattern specified by :hidden config"
[files config get-path-fn]
(if-let [patterns (seq (:hidden config))]
(remove (fn [file]
(let [path (get-path-fn file)]
(hidden? path patterns))) files)
files))
20 changes: 10 additions & 10 deletions deps/common/src/logseq/common/graph.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns logseq.common.graph
(ns ^:node-only logseq.common.graph
"This ns provides common fns for a graph directory and only runs in a node environment"
(:require ["fs" :as fs]
["path" :as node-path]
Expand Down Expand Up @@ -38,15 +38,6 @@
(map fix-win-path!)
(vec)))

(def ^:private allowed-formats
#{:org :markdown :md :edn :json :js :css :excalidraw :tldr})

(defn- get-ext
[p]
(-> (node-path/extname p)
(subs 1)
keyword))

(defn ignored-path?
"Given a graph directory and path, returns truthy value on whether the path is
ignored. Useful for contexts like reading a graph's directory and file watcher
Expand All @@ -65,6 +56,15 @@
(or (re-find #"/\.[^.]+" relpath)
(re-find #"^\.[^.]+" relpath))))))

(def ^:private allowed-formats
#{:org :markdown :md :edn :json :js :css :excalidraw :tldr})

(defn- get-ext
[p]
(-> (node-path/extname p)
(subs 1)
keyword))

(defn get-files
"Given a graph's root dir, returns a list of all files that it recognizes"
[graph-dir]
Expand Down
21 changes: 21 additions & 0 deletions deps/common/test/logseq/common/config_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns logseq.common.config-test
(:require [logseq.common.config :as common-config]
[cljs.test :refer [deftest is]]))

(deftest remove-hidden-files
(let [files ["pages/foo.md" "pages/bar.md"
"script/README.md" "script/config.edn"
"dev/README.md" "dev/config.edn"]]
(is (= ["pages/foo.md" "pages/bar.md"]
(common-config/remove-hidden-files
files
{:hidden ["script" "/dev"]}
identity))
"Removes hidden relative files")

(is (= ["/pages/foo.md" "/pages/bar.md"]
(common-config/remove-hidden-files
(map #(str "/" %) files)
{:hidden ["script" "/dev"]}
identity))
"Removes hidden files if they start with '/'")))
26 changes: 18 additions & 8 deletions deps/graph-parser/src/logseq/graph_parser/cli.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns logseq.graph-parser.cli
(ns ^:node-only logseq.graph-parser.cli
"Primary ns to parse graphs with node.js based CLIs"
(:require ["fs" :as fs]
["child_process" :as child-process]
["path" :as path]
[clojure.edn :as edn]
[logseq.common.graph :as common-graph]
[logseq.common.config :as common-config]
[logseq.graph-parser :as graph-parser]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.util :as gp-util]
Expand All @@ -14,22 +15,31 @@
[file]
(str (fs/readFileSync file)))

(defn- remove-hidden-files [dir config files]
(if (seq (:hidden config))
(->> files
(map #(assoc % ::rel-path (path/relative dir (:file/path %))))
((fn [files] (common-config/remove-hidden-files files config ::rel-path)))
(map #(dissoc % ::rel-path)))
files))

(defn- build-graph-files
"Given a graph directory, return allowed file paths and their contents in preparation
for parsing"
[dir]
[dir config]
(->> (common-graph/get-files dir)
(map #(hash-map :file/path %))
graph-parser/filter-files
(remove-hidden-files dir config)
(mapv #(assoc % :file/content (slurp (:file/path %))))))

(defn- read-config
"Reads repo-specific config from logseq/config.edn"
[dir]
(let [config-file (str dir "/" gp-config/app-name "/config.edn")]
(if (fs/existsSync config-file)
(-> config-file fs/readFileSync str edn/read-string)
{})))
(-> config-file fs/readFileSync str edn/read-string)
{})))

(defn- parse-files
[conn files {:keys [config] :as options}]
Expand Down Expand Up @@ -62,10 +72,10 @@
([dir]
(parse-graph dir {}))
([dir options]
(let [files (or (:files options) (build-graph-files dir))
(let [config (read-config dir)
files (or (:files options) (build-graph-files dir config))
conn (or (:conn options) (ldb/start-conn))
config (read-config dir)
_ (when-not (:files options) (println "Parsing" (count files) "files..."))
_ (when-not (:files options) (println "Parsing" (count files) "files..."))
asts (parse-files conn files (merge options {:config config}))]
{:conn conn
:files (map :file/path files)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@
;; Counts assertions help check for no major regressions. These counts should
;; only increase over time as the docs graph rarely has deletions
(testing "Counts"
(is (= 304 (count files)) "Correct file count")
(is (= 69502 (count (d/datoms db :eavt))) "Correct datoms count")
(is (= 303 (count files)) "Correct file count")
(is (= 69499 (count (d/datoms db :eavt))) "Correct datoms count")

(is (= 5866
(ffirst
Expand Down
2 changes: 1 addition & 1 deletion deps/graph-parser/test/logseq/graph_parser/mldoc_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ body"
(deftest ^:integration test->edn
(let [graph-dir "test/docs-0.9.2"
_ (docs-graph-helper/clone-docs-repo-if-not-exists graph-dir "v0.9.2")
files (gp-cli/build-graph-files graph-dir)
files (#'gp-cli/build-graph-files graph-dir {})
asts-by-file (->> files
(map (fn [{:file/keys [path content]}]
(let [format (if (string/ends-with? path ".org")
Expand Down
2 changes: 1 addition & 1 deletion deps/publishing/src/logseq/publishing.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns logseq.publishing
(ns ^:node-only logseq.publishing
"This node only ns provides api fns for exporting a publishing app"
(:require [logseq.publishing.html :as publish-html]
[logseq.publishing.export :as publish-export]))
Expand Down
2 changes: 1 addition & 1 deletion deps/publishing/src/logseq/publishing/export.cljs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns logseq.publishing.export
(ns ^:node-only logseq.publishing.export
"This electron only ns (for the main process) exports files from multiple
locations to provide a complete publishing app"
(:require ["fs-extra" :as fse]
Expand Down
22 changes: 0 additions & 22 deletions src/main/frontend/handler/common.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"Common fns for handlers"
(:require [cljs-bean.core :as bean]
[cljs.reader :as reader]
[clojure.string :as string]
[frontend.date :as date]
[frontend.state :as state]
[frontend.util :as util]
Expand All @@ -28,27 +27,6 @@
(.filter (bean/->js paths))
(bean/->clj)))

(defn- hidden?
[path patterns]
(let [path (if (and (string? path)
(= \/ (first path)))
(subs path 1)
path)]
(some (fn [pattern]
(let [pattern (if (and (string? pattern)
(not= \/ (first pattern)))
(str "/" pattern)
pattern)]
(string/starts-with? (str "/" path) pattern))) patterns)))

(defn remove-hidden-files
[files config get-path-fn]
(if-let [patterns (seq (:hidden config))]
(remove (fn [file]
(let [path (get-path-fn file)]
(hidden? path patterns))) files)
files))

(defn safe-read-string
[content error-message-or-handler]
(try
Expand Down
10 changes: 5 additions & 5 deletions src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
[frontend.db :as db]
[frontend.fs :as fs]
[frontend.fs.nfs :as nfs]
[frontend.handler.common :as common-handler]
[frontend.handler.file :as file-handler]
[frontend.handler.repo-config :as repo-config-handler]
[frontend.handler.common.file :as file-common-handler]
Expand All @@ -31,7 +30,8 @@
[clojure.core.async :as async]
[frontend.mobile.util :as mobile-util]
[medley.core :as medley]
[logseq.common.path :as path]))
[logseq.common.path :as path]
[logseq.common.config :as common-config]))

;; Project settings should be checked in two situations:
;; 1. User changes the config.edn directly in logseq.com (fn: alter-file)
Expand Down Expand Up @@ -273,7 +273,7 @@
;; config should be loaded to state first
_ (state/set-config! repo-url config)
;; remove :hidden files from file-objs, :hidden
file-objs (common-handler/remove-hidden-files file-objs config :file/path)]
file-objs (common-config/remove-hidden-files file-objs config :file/path)]

;; Load to db even it's empty, (will create default files)
(parse-files-and-load-to-db! repo-url file-objs {:new-graph? new-graph?
Expand All @@ -293,8 +293,8 @@
(state/get-config repo-url))
;; NOTE: Use config while parsing. Make sure it's the current journal title format
_ (state/set-config! repo-url config)
nfs-files (common-handler/remove-hidden-files file-objs config :file/path)
diffs (common-handler/remove-hidden-files diffs config :path)
nfs-files (common-config/remove-hidden-files file-objs config :file/node-node-path)
diffs (common-config/remove-hidden-files diffs config :path)
load-contents (fn [files option]
(file-handler/load-files-contents!
repo-url
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/schema/handler/global_config.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns frontend.schema.handler.global-config
"Malli schemas for global-config"
(:require [frontend.schema.handler.common-config :as common-config]))
(:require [frontend.schema.handler.common-config :as common-config-schema]))

;; For now this just references a common schema but repo-config and
;; global-config could diverge
(def Config-edn
"Schema for global config.edn"
common-config/Config-edn)
common-config-schema/Config-edn)
4 changes: 2 additions & 2 deletions src/main/frontend/schema/handler/repo_config.cljc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns frontend.schema.handler.repo-config
"Malli schemas for repo-config"
(:require [frontend.schema.handler.common-config :as common-config]))
(:require [frontend.schema.handler.common-config :as common-config-schema]))

;; For now this just references a common schema but repo-config and
;; global-config could diverge
(def Config-edn
"Schema for repo config.edn"
common-config/Config-edn)
common-config-schema/Config-edn)
6 changes: 3 additions & 3 deletions src/test/frontend/handler/repo_conversion_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
;; Counts assertions help check for no major regressions. These counts should
;; only increase over time as the docs graph rarely has deletions
(testing "Counts"
(is (= 212 (count files)) "Correct file count")
(is (= 42315 (count (d/datoms db :eavt))) "Correct datoms count")
(is (= 211 (count files)) "Correct file count")
(is (= 42304 (count (d/datoms db :eavt))) "Correct datoms count")

(is (= 3600
(ffirst
Expand Down Expand Up @@ -137,7 +137,7 @@
(deftest ^:integration convert-v067-filenames-parse-and-load-files-to-db
(let [graph-dir "src/test/docs"
_ (docs-graph-helper/clone-docs-repo-if-not-exists graph-dir "v0.6.7")
files (gp-cli/build-graph-files graph-dir)
files (#'gp-cli/build-graph-files graph-dir {})
;; Converting the v0.6.7 ver docs graph under the old namespace naming rule to the new one (:repo/dir-version 0->3)
files (convert-graph-files-path files convert-to-triple-lowbar)
_ (repo-handler/parse-files-and-load-to-db! test-helper/test-db files {:re-render? false :verbose false})
Expand Down
2 changes: 1 addition & 1 deletion src/test/frontend/handler/repo_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
(deftest ^:integration parse-and-load-files-to-db
(let [graph-dir "src/test/docs-0.9.2"
_ (docs-graph-helper/clone-docs-repo-if-not-exists graph-dir "v0.9.2")
files (gp-cli/build-graph-files graph-dir)
repo-config (edn/read-string (str (fs/readFileSync (node-path/join graph-dir "logseq/config.edn"))))
files (#'gp-cli/build-graph-files graph-dir repo-config)
_ (test-helper/with-config repo-config
(repo-handler/parse-files-and-load-to-db! test-helper/test-db files {:re-render? false :verbose false}))
db (conn/get-db test-helper/test-db)]
Expand Down

0 comments on commit 47508ac

Please sign in to comment.