浏览代码

fix: :url property values with macros

Macros in :url are useful for having urls with a common hostname that
can be configured once. Without this fix, docs graph is invalid
Gabriel Horner 1 年之前
父节点
当前提交
057d6b459f

+ 13 - 6
deps/db/src/logseq/db/frontend/property/type.cljs

@@ -2,7 +2,8 @@
   "Provides property types and related helper fns e.g. property value validation
   "Provides property types and related helper fns e.g. property value validation
   fns and their allowed schema attributes"
   fns and their allowed schema attributes"
   (:require [datascript.core :as d]
   (:require [datascript.core :as d]
-            [clojure.set :as set]))
+            [clojure.set :as set]
+            [logseq.common.util.macro :as macro-util]))
 
 
 ;; Config vars
 ;; Config vars
 ;; ===========
 ;; ===========
@@ -81,16 +82,22 @@
          (catch :default _e
          (catch :default _e
            false))))
            false))))
 
 
-(defn- entity?
-  [db id]
-  (some? (d/entity db id)))
+(defn macro-url?
+  [s]
+  ;; TODO: Confirm that macro expanded value is url when it's easier to pass data into validations
+  (macro-util/macro? s))
 
 
 (defn- url-entity?
 (defn- url-entity?
   [db val {:keys [new-closed-value?]}]
   [db val {:keys [new-closed-value?]}]
   (if new-closed-value?
   (if new-closed-value?
-    (url? val)
+    (or (url? val) (macro-url? val))
     (when-let [ent (d/entity db val)]
     (when-let [ent (d/entity db val)]
-      (url? (:property.value/content ent)))))
+      (or (url? (:property.value/content ent))
+          (macro-url? (:property.value/content ent))))))
+
+(defn- entity?
+  [db id]
+  (some? (d/entity db id)))
 
 
 (defn- number-entity?
 (defn- number-entity?
   [db id-or-value {:keys [new-closed-value?]}]
   [db id-or-value {:keys [new-closed-value?]}]

+ 22 - 4
deps/db/test/logseq/db/sqlite/create_graph_test.cljs

@@ -6,7 +6,8 @@
             [logseq.db.frontend.schema :as db-schema]
             [logseq.db.frontend.schema :as db-schema]
             [logseq.db.sqlite.create-graph :as sqlite-create-graph]
             [logseq.db.sqlite.create-graph :as sqlite-create-graph]
             [logseq.db.frontend.validate :as db-validate]
             [logseq.db.frontend.validate :as db-validate]
-            [logseq.db.frontend.property :as db-property]))
+            [logseq.db.frontend.property :as db-property]
+            [logseq.db.sqlite.build :as sqlite-build]))
 
 
 (deftest new-graph-db-idents
 (deftest new-graph-db-idents
   (testing "a new graph follows :db/ident conventions for"
   (testing "a new graph follows :db/ident conventions for"
@@ -74,7 +75,24 @@
         _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))
         _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}"))
         validation (db-validate/validate-db! @conn)]
         validation (db-validate/validate-db! @conn)]
     ;; For debugging
     ;; For debugging
-    ;; (cljs.pprint/pprint (map :entity (:errors validation)))
     ;; (println (count (:errors validation)) "errors of" (count (:entities validation)))
     ;; (println (count (:errors validation)) "errors of" (count (:entities validation)))
-    (is (nil? (:errors validation))
-        "New graph has no validation errors")))
+    (is (empty? (map :entity (:errors validation)))
+        "New graph has no validation errors")))
+
+(deftest property-types
+  (let [conn (d/create-conn db-schema/schema-for-db-based-graph)
+        _ (d/transact! conn (sqlite-create-graph/build-db-initial-data
+                             (pr-str {:macros {"docs-base-url" "https://docs.logseq.com/#/page/$1"}})))]
+
+    (testing ":url property"
+      (sqlite-build/create-blocks
+       conn
+       {:properties {:url {:block/schema {:type :url}}}
+        :pages-and-blocks
+        [{:page {:block/original-name "page1"}
+          :blocks [{:block/content "b1" :build/properties {:url "https://logseq.com"}}
+                   ;; :url macros are used for consistently building urls with the same hostname e.g. docs graph
+                   {:block/content "b2" :build/properties {:url "{{docs-base-url test}}"}}]}]})
+
+      (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
+          "Graph with different :url blocks has no validation errors"))))

+ 1 - 0
deps/graph-parser/test/logseq/graph_parser/exporter_test.cljs

@@ -170,6 +170,7 @@
                {:db/ident :user.property/prop-num2 :block/schema {:type :number}}
                {:db/ident :user.property/prop-num2 :block/schema {:type :number}}
                {:db/ident :user.property/type :block/schema {:type :page}}
                {:db/ident :user.property/type :block/schema {:type :page}}
                {:db/ident :user.property/url :block/schema {:type :url}}
                {:db/ident :user.property/url :block/schema {:type :url}}
+               {:db/ident :user.property/sameas :block/schema {:type :url}}
                {:db/ident :user.property/rangeincludes :block/schema {:type :page}}
                {:db/ident :user.property/rangeincludes :block/schema {:type :page}}
                {:db/ident :user.property/unique :block/schema {:type :checkbox}}}
                {:db/ident :user.property/unique :block/schema {:type :checkbox}}}
              (->> @conn
              (->> @conn

+ 2 - 3
deps/graph-parser/test/resources/exporter-test-graph/logseq/config.edn

@@ -262,9 +262,8 @@
  ;; input "{{poem red,blue}}"
  ;; input "{{poem red,blue}}"
  ;; becomes
  ;; becomes
  ;; Rose is red, violet's blue. Life's ordered: Org assists you.
  ;; Rose is red, violet's blue. Life's ordered: Org assists you.
- ;; TODO: test
-;;  :macros
-;;  {"docs-base-url" "https://docs.logseq.com/#/page/$1"}
+ :macros
+ {"docs-base-url" "https://docs.logseq.com/#/page/$1"}
 
 
  ;; Configure the default expansion level for linked references.
  ;; Configure the default expansion level for linked references.
  ;; For example, consider the following block hierarchy:
  ;; For example, consider the following block hierarchy:

+ 2 - 1
deps/graph-parser/test/resources/exporter-test-graph/pages/url.md

@@ -1,4 +1,5 @@
 type:: [[Property]]
 type:: [[Property]]
-url:: https://schema.org/url
+url:: {{docs-base-url url}}
+sameAs:: https://schema.org/url
 rangeIncludes:: [[Uri]]
 rangeIncludes:: [[Uri]]
 unique:: true
 unique:: true