Prechádzať zdrojové kódy

Add config options to disable property pages and to exclude certain ones

Gabriel Horner 3 rokov pred
rodič
commit
a0087d7792

+ 19 - 12
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -139,8 +139,22 @@
    (vector? block)
    (= "Timestamp" (first block))))
 
+(defn- get-page-refs-from-property-names
+  [properties {:property-pages/keys [enabled? excludelist]}]
+  (if (contains? #{true nil} enabled?)
+    (some->> properties
+             (map (comp name first))
+             (remove string/blank?)
+             (remove (set (map name excludelist)))
+             ;; Remove built-in properties as we don't want pages
+             ;; created for them by default
+             (remove (set (map name (into (gp-property/editable-built-in-properties)
+                                          (gp-property/hidden-built-in-properties)))))
+             distinct)
+    []))
+
 (defn- get-page-ref-names-from-properties
-  [format properties]
+  [format properties user-config]
   (let [page-refs (->>
                    properties
                    (remove (fn [[k _]]
@@ -165,15 +179,8 @@
                             :else
                             nil)))
                    (apply concat))
-        property-keys-page-refs (some->> properties
-                                         (map (comp name first))
-                                         (remove string/blank?)
-                                         ;; Remove built-in properties as we don't want pages
-                                         ;; created for them by default
-                                         (remove (set (map name (into (gp-property/editable-built-in-properties)
-                                                                      (gp-property/hidden-built-in-properties)))))
-                                         (distinct))]
-    (->> (concat page-refs property-keys-page-refs)
+        page-refs-from-property-names (get-page-refs-from-property-names properties user-config)]
+    (->> (concat page-refs page-refs-from-property-names)
          (remove string/blank?)
          distinct)))
 
@@ -186,7 +193,7 @@
   (when (seq properties)
     (let [properties (seq properties)
           properties (into {} properties)
-          page-refs (get-page-ref-names-from-properties format properties)
+          page-refs (get-page-ref-names-from-properties format properties user-config)
           properties (->> properties
                           (map (fn [[k v]]
                                  (let [k (-> (string/lower-case (name k))
@@ -468,7 +475,7 @@
 
 (defn get-page-refs-from-properties
   [format properties db date-formatter]
-  (let [page-refs (get-page-ref-names-from-properties format properties)]
+  (let [page-refs (get-page-ref-names-from-properties format properties {})]
     (map (fn [page] (page-name->map page true db true date-formatter)) page-refs)))
 
 (defn- with-page-block-refs

+ 53 - 28
deps/graph-parser/test/logseq/graph_parser/block_test.cljs

@@ -1,36 +1,61 @@
 (ns logseq.graph-parser.block-test
   (:require [logseq.graph-parser.block :as gp-block]
-            [cljs.test :refer [deftest are]]))
+            [cljs.test :refer [deftest are testing is]]))
 
 (deftest test-extract-properties
   (are [x y] (= (:properties (gp-block/extract-properties :markdown x {})) y)
-    [["year" "1000"]] {:year 1000}
-    [["year" "\"1000\""]] {:year "\"1000\""}
-    [["background-color" "#000000"]] {:background-color "#000000"}
-    [["alias" "name/with space"]] {:alias #{"name/with space"}}
-    [["year" "1000"] ["alias" "name/with space"]] {:year 1000, :alias #{"name/with space"}}
-    [["year" "1000"] ["tags" "name/with space"]] {:year 1000, :tags #{"name/with space"}}
-    [["year" "1000"] ["tags" "name/with space, another"]] {:year 1000, :tags #{"name/with space" "another"}}
-    [["year" "1000"] ["alias" "name/with space, another"]] {:year 1000, :alias #{"name/with space" "another"}}
-    [["year" "1000"] ["alias" "name/with space, [[another [[nested]]]]"]] {:year 1000, :alias #{"name/with space" "another [[nested]]"}}
-    [["year" "1000"] ["alias" "name/with space, [[[[nested]] another]]"]] {:year 1000, :alias #{"name/with space" "[[nested]] another"}}
-    [["foo" "bar"]] {:foo "bar"}
-    [["foo" "bar, baz"]] {:foo #{"bar" "baz"}}
-    [["foo" "bar, [[baz]]"]] {:foo #{"bar" "baz"}}
-    [["foo" "[[bar]], [[baz]]"]] {:foo #{"bar" "baz"}}
-    [["foo" "[[bar]], [[nested [[baz]]]]"]] {:foo #{"bar" "nested [[baz]]"}}
-    [["foo" "[[bar]], [[nested [[baz]]]]"]] {:foo #{"bar" "nested [[baz]]"}}
-    [["foo" "bar, [[baz, test]]"]] {:foo #{"bar" "baz, test"}}
-    [["foo" "bar, [[baz, test, [[nested]]]]"]] {:foo #{"bar" "baz, test, [[nested]]"}}
-    [["file-path" "file:///home/x, y.pdf"]] {:file-path "file:///home/x, y.pdf"})
+       [["year" "1000"]] {:year 1000}
+       [["year" "\"1000\""]] {:year "\"1000\""}
+       [["background-color" "#000000"]] {:background-color "#000000"}
+       [["alias" "name/with space"]] {:alias #{"name/with space"}}
+       [["year" "1000"] ["alias" "name/with space"]] {:year 1000, :alias #{"name/with space"}}
+       [["year" "1000"] ["tags" "name/with space"]] {:year 1000, :tags #{"name/with space"}}
+       [["year" "1000"] ["tags" "name/with space, another"]] {:year 1000, :tags #{"name/with space" "another"}}
+       [["year" "1000"] ["alias" "name/with space, another"]] {:year 1000, :alias #{"name/with space" "another"}}
+       [["year" "1000"] ["alias" "name/with space, [[another [[nested]]]]"]] {:year 1000, :alias #{"name/with space" "another [[nested]]"}}
+       [["year" "1000"] ["alias" "name/with space, [[[[nested]] another]]"]] {:year 1000, :alias #{"name/with space" "[[nested]] another"}}
+       [["foo" "bar"]] {:foo "bar"}
+       [["foo" "bar, baz"]] {:foo #{"bar" "baz"}}
+       [["foo" "bar, [[baz]]"]] {:foo #{"bar" "baz"}}
+       [["foo" "[[bar]], [[baz]]"]] {:foo #{"bar" "baz"}}
+       [["foo" "[[bar]], [[nested [[baz]]]]"]] {:foo #{"bar" "nested [[baz]]"}}
+       [["foo" "[[bar]], [[nested [[baz]]]]"]] {:foo #{"bar" "nested [[baz]]"}}
+       [["foo" "bar, [[baz, test]]"]] {:foo #{"bar" "baz, test"}}
+       [["foo" "bar, [[baz, test, [[nested]]]]"]] {:foo #{"bar" "baz, test, [[nested]]"}}
+       [["file-path" "file:///home/x, y.pdf"]] {:file-path "file:///home/x, y.pdf"})
 
-  (are [x y] (= (vec (:page-refs (gp-block/extract-properties :markdown x {}))) y)
-    [["year" "1000"]] ["year"]
-    [["year" "\"1000\""]] ["year"]
-    [["foo" "[[bar]] test"]] ["bar" "test" "foo"]
-    [["foo" "[[bar]] test [[baz]]"]] ["bar" "test" "baz" "foo"]
-    [["foo" "[[bar]] test [[baz]] [[nested [[baz]]]]"]] ["bar" "test" "baz" "nested [[baz]]" "foo"]
-    [["foo" "#bar, #baz"]] ["bar" "baz" "foo"]
-    [["foo" "[[nested [[page]]]], test"]] ["nested [[page]]" "test" "foo"]))
+  (testing "page-refs"
+    (are [x y] (= (vec (:page-refs
+                        (gp-block/extract-properties :markdown x {:property-pages/enabled? true}))) y)
+         [["year" "1000"]] ["year"]
+         [["year" "\"1000\""]] ["year"]
+         [["year" "1000"] ["month" "12"]] ["year" "month"]
+         [["foo" "[[bar]] test"]] ["bar" "test" "foo"]
+         [["foo" "[[bar]] test [[baz]]"]] ["bar" "test" "baz" "foo"]
+         [["foo" "[[bar]] test [[baz]] [[nested [[baz]]]]"]] ["bar" "test" "baz" "nested [[baz]]" "foo"]
+         [["foo" "#bar, #baz"]] ["bar" "baz" "foo"]
+         [["foo" "[[nested [[page]]]], test"]] ["nested [[page]]" "test" "foo"])
+
+
+    (are [x y] (= (vec (:page-refs
+                        (gp-block/extract-properties :markdown x {:property-pages/enabled? false}))) y)
+         [["year" "1000"]] []
+         [["year" "1000"] ["month" "12"]] []
+         [["foo" "[[bar]] test"]] ["bar" "test"])
+
+    (is (= ["year"]
+           (:page-refs
+            (gp-block/extract-properties :markdown
+                                         [["year" "1000"] ["month" "12"]]
+                                         {:property-pages/enabled? true
+                                          :property-pages/excludelist #{:month :day}})))
+        ":property-pages/exclude-list excludes specified properties")
+
+    (is (= ["year"]
+           (:page-refs
+                (gp-block/extract-properties :markdown
+                                             [["year" "1000"]]
+                                             {})))
+        "Default to enabled when :property-pages/enabled? is not in config")))
 
 #_(cljs.test/run-tests)

+ 5 - 2
src/main/frontend/components/block.cljs

@@ -1805,9 +1805,12 @@
 
 (rum/defc property-cp
   [config block k v]
-  (let [date (and (= k :date) (date/get-locale-string (str v)))]
+  (let [date (and (= k :date) (date/get-locale-string (str v)))
+        property-pages-enabled? (contains? #{true nil} (:property-pages/enabled? (state/get-config)))]
     [:div
-     (page-cp (assoc config :property? true) {:block/name (subs (str k) 1)})
+     (if property-pages-enabled?
+       (page-cp (assoc config :property? true) {:block/name (subs (str k) 1)})
+       [:span.page-property-key.font-medium (name k)])
      [:span.mr-1 ":"]
      (cond
        (int? v)

+ 7 - 0
templates/config.edn

@@ -212,6 +212,13 @@
  ;; E.g. #{:created-at :updated-at}
  ;; :block-hidden-properties #{}
 
+ ;; Enable all your properties to have corresponding pages
+ :property-pages/enabled? true
+
+ ;; Properties to exclude from having property pages
+ ;; E.g. #{:duration :author}
+ ;; :property-pages/excludelist
+
  ;; logbook setup
  ;; :logbook/settings
  ;; {:with-second-support? false ;limit logbook to minutes, seconds will be eliminated