Przeglądaj źródła

enhance: improve property type detection

with macros. This improves :url property detection in docs
graph and other similar graphs. part of LOG-2985
Gabriel Horner 1 rok temu
rodzic
commit
dad848fa73

+ 1 - 0
deps/common/.carve/config.edn

@@ -4,6 +4,7 @@
                   logseq.common.util.block-ref
                   logseq.common.util.block-ref
                   logseq.common.util
                   logseq.common.util
                   logseq.common.util.date-time
                   logseq.common.util.date-time
+                  logseq.common.util.macro
                   logseq.common.marker
                   logseq.common.marker
                   logseq.common.config]
                   logseq.common.config]
  :report {:format :ignore}}
  :report {:format :ignore}}

+ 1 - 1
deps/common/src/logseq/common/util/macro.cljs

@@ -6,7 +6,7 @@
 
 
 (defn macro?
 (defn macro?
   [*s]
   [*s]
-  (let [s (string/trim (str *s))]
+  (when-let [s (and (string? *s) (string/trim *s))]
     (and (string/starts-with? s left-braces) (string/ends-with? s right-braces))))
     (and (string/starts-with? s left-braces) (string/ends-with? s right-braces))))
 
 
 (defn macro-subs
 (defn macro-subs

+ 18 - 13
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -9,7 +9,8 @@
             [logseq.common.config :as common-config]
             [logseq.common.config :as common-config]
             [logseq.db.frontend.content :as db-content]
             [logseq.db.frontend.content :as db-content]
             [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.property :as db-property]
-            [logseq.db.frontend.property.type :as db-property-type]))
+            [logseq.db.frontend.property.type :as db-property-type]
+            [logseq.common.util.macro :as macro-util]))
 
 
 (defn- get-pid
 (defn- get-pid
   "Get a property's id (name or uuid) given its name. For db graphs"
   "Get a property's id (name or uuid) given its name. For db graphs"
@@ -98,7 +99,7 @@
   "Infers a property's schema from the given _user_ property value and adds new ones to
   "Infers a property's schema from the given _user_ property value and adds new ones to
   the property-schemas atom. If a property's :type changes, returns a map of
   the property-schemas atom. If a property's :type changes, returns a map of
   the schema attribute changed and how it changed e.g. `{:type {:from :default :to :url}}`"
   the schema attribute changed and how it changed e.g. `{:type {:from :default :to :url}}`"
-  [prop-val prop refs property-schemas]
+  [prop-val prop refs property-schemas macros]
   ;; Explicitly fail an unexpected case rather cause silent downstream failures
   ;; Explicitly fail an unexpected case rather cause silent downstream failures
   (when (and (coll? prop-val) (not (every? string? prop-val)))
   (when (and (coll? prop-val) (not (every? string? prop-val)))
     (throw (ex-info "Import cannot infer schema of unknown property value"
     (throw (ex-info "Import cannot infer schema of unknown property value"
@@ -108,7 +109,8 @@
                            (set/subset? prop-val
                            (set/subset? prop-val
                                         (set (keep #(when (:block/journal? %) (:block/original-name %)) refs))))
                                         (set (keep #(when (:block/journal? %) (:block/original-name %)) refs))))
                     :date
                     :date
-                    (db-property-type/infer-property-type-from-value prop-val))
+                    (db-property-type/infer-property-type-from-value
+                     (macro-util/expand-value-if-macro prop-val macros)))
         prev-type (get-in @property-schemas [prop :type])]
         prev-type (get-in @property-schemas [prop :type])]
     (when-not prev-type
     (when-not prev-type
       (let [schema (cond-> {:type prop-type}
       (let [schema (cond-> {:type prop-type}
@@ -219,7 +221,7 @@
   "Infers property schemas, update :block/properties and remove deprecated
   "Infers property schemas, update :block/properties and remove deprecated
   property attributes. Only infers property schemas on user properties as
   property attributes. Only infers property schemas on user properties as
   built-in ones shouldn't change"
   built-in ones shouldn't change"
-  [{:block/keys [properties] :as block} db page-names-to-uuids refs {:keys [import-state] :as options}]
+  [{:block/keys [properties] :as block} db page-names-to-uuids refs {:keys [import-state macros] :as options}]
   (-> (if (seq properties)
   (-> (if (seq properties)
         (let [dissoced-props (into ignored-built-in-properties
         (let [dissoced-props (into ignored-built-in-properties
                                    ;; TODO: Add import support for these dissoced built-in properties
                                    ;; TODO: Add import support for these dissoced built-in properties
@@ -228,14 +230,15 @@
                                     :card-ease-factor :card-last-score])
                                     :card-ease-factor :card-last-score])
               properties' (apply dissoc properties dissoced-props)
               properties' (apply dissoc properties dissoced-props)
               properties-to-infer (if (:template properties')
               properties-to-infer (if (:template properties')
-              ;; Ignore template properties as they don't consistently have representative property values
+                                    ;; Ignore template properties as they don't consistently have representative property values
                                     {}
                                     {}
                                     (apply dissoc properties' db-property/built-in-properties-keys))
                                     (apply dissoc properties' db-property/built-in-properties-keys))
-              property-changes (->> properties-to-infer
-                                    (keep (fn [[prop val]]
-                                            (when-let [property-change (infer-property-schema-and-get-property-change val prop refs (:property-schemas import-state))]
-                                              [prop property-change])))
-                                    (into {}))
+              property-changes
+              (->> properties-to-infer
+                   (keep (fn [[prop val]]
+                           (when-let [property-change (infer-property-schema-and-get-property-change val prop refs (:property-schemas import-state) macros)]
+                             [prop property-change])))
+                   (into {}))
               _ (when (seq property-changes) (prn :PROP-CHANGES property-changes))
               _ (when (seq property-changes) (prn :PROP-CHANGES property-changes))
               options' (assoc options :property-changes property-changes)]
               options' (assoc options :property-changes property-changes)]
           (assoc-in block [:block/properties]
           (assoc-in block [:block/properties]
@@ -381,7 +384,8 @@
 * :extract-options - Options map to pass to extract/extract
 * :extract-options - Options map to pass to extract/extract
 * :user-options - User provided options that alter how a file is converted to db graph
 * :user-options - User provided options that alter how a file is converted to db graph
 * :page-tags-uuid - uuid of pageTags property
 * :page-tags-uuid - uuid of pageTags property
-* :import-state - useful import state to maintain across files e.g. property schemas or ignored properties"
+* :import-state - useful import state to maintain across files e.g. property schemas or ignored properties
+* :macros - map of macros for use with macro expansion"
   [conn file content {:keys [extract-options user-options import-state]
   [conn file content {:keys [extract-options user-options import-state]
                       :or {import-state (new-import-state)}
                       :or {import-state (new-import-state)}
                       :as options}]
                       :as options}]
@@ -405,7 +409,7 @@
               (println "Skipped file since its format is not supported:" file))
               (println "Skipped file since its format is not supported:" file))
         ;; Build page and block txs
         ;; Build page and block txs
         {:keys [pages page-names-to-uuids]}
         {:keys [pages page-names-to-uuids]}
-        (build-pages-tx conn (:pages extracted) (:blocks extracted) tag-classes (select-keys options [:page-tags-uuid :import-state]))
+        (build-pages-tx conn (:pages extracted) (:blocks extracted) tag-classes (select-keys options [:page-tags-uuid :import-state :macros]))
         whiteboard-pages (->> pages
         whiteboard-pages (->> pages
                               ;; support old and new whiteboards
                               ;; support old and new whiteboards
                               (filter #(#{"whiteboard" ["whiteboard"]} (:block/type %)))
                               (filter #(#{"whiteboard" ["whiteboard"]} (:block/type %)))
@@ -420,7 +424,8 @@
                     (remove :block/pre-block?)
                     (remove :block/pre-block?)
                     (map #(build-block-tx @conn % pre-blocks tag-classes page-names-to-uuids
                     (map #(build-block-tx @conn % pre-blocks tag-classes page-names-to-uuids
                                           {:whiteboard? (some? (seq whiteboard-pages))
                                           {:whiteboard? (some? (seq whiteboard-pages))
-                                           :import-state import-state})))
+                                           :import-state import-state
+                                           :macros (:macros options)})))
         ;; Build indices
         ;; Build indices
         pages-index (map #(select-keys % [:block/name]) pages)
         pages-index (map #(select-keys % [:block/name]) pages)
         block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks)
         block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks)

+ 2 - 1
src/main/frontend/components/imports.cljs

@@ -200,7 +200,8 @@
                                                    {:extract-options extract-options
                                                    {:extract-options extract-options
                                                     :user-options user-options
                                                     :user-options user-options
                                                     :page-tags-uuid page-tags-uuid
                                                     :page-tags-uuid page-tags-uuid
-                                                    :import-state import-state})]
+                                                    :import-state import-state
+                                                    :macros (state/get-macros)})]
                                               (db-browser/transact! @db-browser/*worker repo (:tx-data tx-report) (:tx-meta tx-report)))
                                               (db-browser/transact! @db-browser/*worker repo (:tx-data tx-report) (:tx-meta tx-report)))
                                             m))
                                             m))
                                   (p/catch (fn [error]
                                   (p/catch (fn [error]