Browse Source

fix: use mldoc to check links

Tienson Qin 2 years ago
parent
commit
c8db7646e2

+ 10 - 0
deps/graph-parser/src/logseq/graph_parser/mldoc.cljc

@@ -164,3 +164,13 @@
   (when (string? link)
     (some-> (first (inline->edn link (default-config format)))
             ast-link?)))
+
+(defn mldoc-link?
+  "Check whether s is a link (including page/block refs)."
+  [format s]
+  (let [result (inline->edn s (default-config format))]
+    (and
+     (= 1 (count result))
+     (let [result' (first result)]
+       (or (contains? #{"Nested_link"} (first result'))
+           (contains? #{"Page_ref" "Block_ref" "Complex"} (first (:url (second result')))))))))

+ 0 - 6
deps/graph-parser/src/logseq/graph_parser/util.cljs

@@ -88,12 +88,6 @@
          (catch :default _e
            false))))
 
-(defn link?
-  "Check whether s is a link (including page/block refs)."
-  [s]
-  (or (url? s)
-      (re-matches #"^(\[\[|\(\().*(\]\]|\)\))$" (string/trim s))))
-
 (defn json->clj
   [json-string]
   (-> json-string

+ 1 - 1
src/main/frontend/handler/editor.cljs

@@ -131,7 +131,7 @@
      (let [{:keys [selection-start selection-end format selection value edit-id input]} m
            cur-pos (cursor/pos input)
            empty-selection? (= selection-start selection-end)
-           selection-link? (and selection (gp-util/link? selection))
+           selection-link? (and selection (gp-mldoc/mldoc-link? format selection))
            [content forward-pos] (cond
                                    empty-selection?
                                    (config/get-empty-link-and-forward-pos format)