Browse Source

fix: don't parse quoted string as page references for properties

Tienson Qin 4 years ago
parent
commit
cc948b5a23

+ 1 - 1
src/main/frontend/format/block.cljs

@@ -215,7 +215,7 @@
                                            "id"
                                            k)
                                        v (if (coll? v)
-                                           v
+                                           (remove util/wrapped-by-quotes? v)
                                            (property/parse-property k v))
                                        k (keyword k)
                                        v (if (and

+ 1 - 3
src/main/frontend/format/mldoc.cljs

@@ -185,9 +185,7 @@
                          (update :roam_tags (constantly roam-tags))
                          (update :filetags (constantly filetags)))
           properties (medley/filter-kv (fn [k v] (not (empty? v))) properties)
-          properties (medley/map-vals (fn [v] (if (and (string? v) (>= (count v) 2) (= (first v) (last v) "\""))
-                                               (subs v 1 (dec (count v)))
-                                               v)) properties)]
+          properties (medley/map-vals util/unquote-string-if-wrapped properties)]
       (if (seq properties)
         (cons [["Properties" properties] nil] other-ast)
         original-ast))

+ 10 - 11
src/main/frontend/text.cljs

@@ -86,11 +86,6 @@
   [s]
   (string/split s #"(\"[^\"]*\")"))
 
-(defn- surrounded-by-quotes
-  [s]
-  (and (string? s)
-       (= (first s) (last s) \")))
-
 (def markdown-link #"\[([^\[]+)\](\(.*\))")
 (defn split-page-refs-without-brackets
   ([s]
@@ -108,15 +103,19 @@
      (let [result (->> (sep-by-quotes s)
                        (mapcat
                         (fn [s]
-                          (if (surrounded-by-quotes s)
-                            [s]
+                          (when-not (util/wrapped-by-quotes? (string/trim s))
                             (string/split s page-ref-re-2))))
-                       (mapcat (fn [s] (if (and (string/includes? (string/trimr s) "]],")
-                                               (not (surrounded-by-quotes s)))
+                       (mapcat (fn [s] (cond
+                                        (util/wrapped-by-quotes? s)
+                                        nil
+
+                                        (string/includes? (string/trimr s) "]],")
                                         (let [idx (string/index-of s "]],")]
                                           [(subs s 0 idx)
                                            "]]"
                                            (subs s (+ idx 3))])
+
+                                        :else
                                         [s])))
                        (remove #(= % ""))
                        (mapcat (fn [s] (if (string/ends-with? s "]]")
@@ -127,8 +126,8 @@
                        (remove string/blank?)
                        (mapcat (fn [s]
                                  (cond
-                                   (surrounded-by-quotes s)
-                                   [(subs s 1 (dec (count s)))]
+                                   (util/wrapped-by-quotes? s)
+                                   nil
 
                                    (page-ref? s)
                                    [(if un-brackets? (page-ref-un-brackets! s) s)]

+ 14 - 0
src/main/frontend/util.cljc

@@ -1356,3 +1356,17 @@
        (not
         (some #(string/ends-with? path %)
               [".md" ".markdown" ".org" ".edn" ".css"]))))))
+
+(defn wrapped-by-quotes?
+  [v]
+  (and (string? v) (>= (count v) 2) (= "\"" (first v) (last v))))
+
+(defn unquote-string
+  [v]
+  (string/trim (subs v 1 (dec (count v)))))
+
+(defn unquote-string-if-wrapped
+  [v]
+  (if (wrapped-by-quotes? v)
+    (unquote-string v)
+    v))

+ 2 - 2
src/main/frontend/util/property.cljs

@@ -371,8 +371,8 @@
       (util/safe-re-find #"^\d+$" v)
       (util/safe-parse-int v)
 
-      (and (= "\"" (first v) (last v))) ; wrapped in ""
-      (string/trim (subs v 1 (dec (count v))))
+      (util/wrapped-by-quotes? v) ; wrapped in ""
+      (util/unquote-string v)
 
       (contains? @non-parsing-properties (string/lower-case k))
       v