Browse Source

fix: convert page to tag does not work with special chars

fixes https://github.com/logseq/db-test/issues/4
Tienson Qin 1 year ago
parent
commit
f39e62678e
1 changed files with 13 additions and 3 deletions
  1. 13 3
      deps/db/src/logseq/db/frontend/db_ident.cljs

+ 13 - 3
deps/db/src/logseq/db/frontend/db_ident.cljs

@@ -1,7 +1,8 @@
 (ns logseq.db.frontend.db-ident
   "Helper fns for class and property :db/ident"
   (:require [datascript.core :as d]
-            [clojure.string :as string]))
+            [clojure.string :as string]
+            [clojure.edn :as edn]))
 
 (defn ensure-unique-db-ident
   "Ensures the given db-ident is unique. If a db-ident conflicts, it is made
@@ -42,5 +43,14 @@
               ;; '/' cannot be in name - https://clojure.org/reference/reader
               (string/replace "/" "-")
               (string/trim))]
-    (assert (seq n) "name is not empty")
-    (keyword user-namespace n)))
+    (try
+      (assert (seq n) "name is not empty")
+      (let [k (keyword user-namespace n)]
+        (assert (= k (edn/read-string (str k))))
+        k)
+      (catch :default _e
+        (let [n (->> (filter #(re-find #"[0-9a-zA-Z-]{1}" %) (seq n))
+                     (apply str))]
+          (if (seq n)
+            (keyword user-namespace n)
+            (keyword user-namespace (subs (str (random-uuid)) 30))))))))