Browse Source

fix: property regression : # should be a delimiter

Close #3916

Tests added.
Tienson Qin 3 years ago
parent
commit
2401ce9e9a

+ 3 - 0
src/main/frontend/components/block.cljs

@@ -1689,6 +1689,9 @@
          (for [elem elems]
            (rum/with-key elem (str (random-uuid)))))
 
+       (and (string? v) (util/wrapped-by-quotes? v))
+       (util/unquote-string v)
+
        :else
        (inline-text (:block/format block) (str v)))]))
 

+ 2 - 2
src/main/frontend/db/query_dsl.cljs

@@ -264,7 +264,7 @@
        (let [k (string/replace (name (nth e 1)) "_" "-")
              v (nth e 2)
              v (if-not (nil? v)
-                 (property/parse-property k v)
+                 (text/parse-property k v)
                  v)
              v (if (coll? v) (first v) v)
              sym (if (= current-filter 'or)
@@ -346,7 +346,7 @@
        (let [[k v] (rest e)
              k (string/replace (name k) "_" "-")]
          (if-not (nil? v)
-           (let [v (property/parse-property k v)
+           (let [v (text/parse-property k v)
                  v (if (coll? v) (first v) v)
                  sym '?v]
              [['?p :block/name]

+ 2 - 6
src/main/frontend/format/block.cljs

@@ -168,9 +168,6 @@
    (= "List" (first block))
    (:name (first (second block)))))
 
-(defonce non-parsing-properties
-  (atom #{"background-color" "background_color"}))
-
 ;; TODO: we should move this to mldoc
 (defn extract-properties
   [properties]
@@ -199,11 +196,10 @@
                                            "id"
                                            k)
                                        v (if (coll? v)
-                                           (->> (remove util/wrapped-by-quotes? v)
-                                                (remove string/blank?))
+                                           (remove string/blank? v)
                                            (if (string/blank? v)
                                              nil
-                                             (property/parse-property k v)))
+                                             (text/parse-property k v)))
                                        k (keyword k)
                                        v (if (and
                                               (string? v)

+ 6 - 56
src/main/frontend/format/mldoc.cljs

@@ -89,41 +89,6 @@
                      config
                      (or references default-references)))
 
-;; Org-roam
-;; TODO: recur is in wrong place. Unclear what the intent is but likely a bug
-#_:clj-kondo/ignore
-(defn get-tags-from-definition
-  [ast]
-  (loop [ast ast]
-    (if (seq ast)
-      (match (first ast)
-        ["List" l]
-        (when-let [name (:name (first l))]
-          (let [name (and (vector? name)
-                          (last (first name)))]
-            (when (and (string? name)
-                       (= (string/lower-case name) "tags"))
-              (->>
-               (last (first (:content (first l))))
-               (map second)
-               (filter (and map? :url))
-               (map (fn [x]
-                      (let [label (last (first (:label x)))
-                            search (and (= (first (:url x)) "Search")
-                                        (last (:url x)))
-                            tag (if-not (string/blank? label)
-                                  label
-                                  search)]
-                        (when tag (string/lower-case tag)))))
-               (remove nil?)))))
-
-        ["Heading" _h]
-        nil
-
-        :else
-        (recur (rest ast)))
-      nil)))
-
 (defn- ->vec
   [s]
   (if (string? s) [s] s))
@@ -154,9 +119,9 @@
                       properties-ast
                       (map (fn [[k v]]
                              (let [k (keyword (string/lower-case k))
-                                   v (if (contains? #{:title :description :filters :roam_tags :macro} k)
+                                   v (if (contains? #{:title :description :filters :macro} k)
                                        v
-                                       (text/split-page-refs-without-brackets v))]
+                                       (text/parse-property k v))]
                                [k v]))))
           properties (into (linked/map) properties)
           macro-properties (filter (fn [x] (= :macro (first x))) properties)
@@ -173,36 +138,21 @@
                    {})
           properties (->> (remove (fn [x] (= :macro (first x))) properties)
                           (into (linked/map)))
-          properties (if (seq properties)
-                       (cond-> properties
-                         (:roam_key properties)
-                         (assoc :key (:roam_key properties)))
-                       properties)
-          definition-tags (get-tags-from-definition ast)
           properties (cond-> properties
                        (seq macros)
                        (assoc :macros macros))
-          alias (->> (->vec-concat (:roam_alias properties) (:alias properties))
+          alias (->> (:alias properties)
                      (remove string/blank?))
           filetags (when-let [org-file-tags (:filetags properties)]
                      (->> (string/split org-file-tags ":")
                           (remove string/blank?)))
-          roam-tags (when-let [org-roam-tags (:roam_tags properties)]
-                      (let [pat #"\"(.*?)\"" ;; note: lazy, capturing group
-                            quoted (map second (re-seq pat org-roam-tags))
-                            rest   (string/replace org-roam-tags pat "")
-                            rest (->> (string/split rest " ")
-                                      (remove string/blank?))]
-                        (concat quoted rest)))
-          tags (->> (->vec-concat roam-tags (:tags properties) definition-tags filetags)
+          tags (:tags properties)
+          tags (->> (->vec-concat tags filetags)
                     (remove string/blank?))
           properties (assoc properties :tags tags :alias alias)
           properties (-> properties
-                         (update :roam_alias ->vec)
-                         (update :roam_tags (constantly roam-tags))
                          (update :filetags (constantly filetags)))
-          properties (medley/filter-kv (fn [_k v] (seq v)) properties)
-          properties (medley/map-vals util/unquote-string-if-wrapped properties)]
+          properties (medley/filter-kv (fn [_k v] (seq v)) properties)]
       (if (seq properties)
         (cons [["Properties" properties] nil] other-ast)
         original-ast))

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

@@ -171,7 +171,7 @@
                                               (->> (last first-block)
                                                    (map (fn [[x y]]
                                                           [x (if (string? y)
-                                                               (property/parse-property x y)
+                                                               (text/parse-property x y)
                                                                y)]))
                                                    (into {})
                                                    (walk/keywordize-keys)))]

+ 1 - 8
src/main/frontend/handler/link.cljs

@@ -2,17 +2,10 @@
   (:require [frontend.util :as util]))
 
 (def plain-link "(\bhttps?://)?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]")
-(def plain-link-re (re-pattern plain-link))
 (def org-link-re-1 (re-pattern (util/format "\\[\\[(%s)\\]\\[(.+)\\]\\]" plain-link)))
 (def org-link-re-2 (re-pattern (util/format "\\[\\[(%s)\\]\\]" plain-link)))
 (def markdown-link-re (re-pattern (util/format "^\\[(.+)\\]\\((%s)\\)" plain-link)))
 
-(defn- plain-link?
-  [link]
-  (when-let [matches (re-matches plain-link-re link)]
-    {:type "plain-link"
-     :url matches}))
-
 (defn- org-link?
   [link]
   (when-let [matches (or (re-matches org-link-re-1 link)
@@ -30,6 +23,6 @@
 
 (defn link?
   [link]
-  (or (plain-link? link)
+  (or (util/url? link)
       (org-link? link)
       (markdown-link? link)))

+ 60 - 17
src/main/frontend/text.cljs

@@ -1,7 +1,8 @@
 (ns frontend.text
   (:require [frontend.config :as config]
             [frontend.util :as util]
-            [clojure.string :as string]))
+            [clojure.string :as string]
+            [frontend.handler.link :as link]))
 
 (def page-ref-re-0 #"\[\[(.*)\]\]")
 (def org-page-ref-re #"\[\[(file:.*)\]\[.+?\]\]")
@@ -69,6 +70,14 @@
      (remove string/blank?)
      (map string/trim))))
 
+(defn sep-by-hashtag
+  [s]
+  (when s
+    (some->>
+     (string/split s #"#")
+     (remove string/blank?)
+     (map string/trim))))
+
 (defn- not-matched-nested-pages
   [s]
   (and (string? s)
@@ -132,23 +141,24 @@
                         (fn [s]
                           (when-not (util/wrapped-by-quotes? (string/trim s))
                             (string/split s page-ref-re-2))))
-                       (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])))
+                       (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 "]]")
-                                        [(subs s 0 (- (count s) 2))
-                                         "]]"]
-                                        [s])))
+                                         [(subs s 0 (- (count s) 2))
+                                          "]]"]
+                                         [s])))
                        concat-nested-pages
                        (remove string/blank?)
                        (mapcat (fn [s]
@@ -160,7 +170,8 @@
                                    [(if un-brackets? (page-ref-un-brackets! s) s)]
 
                                    :else
-                                   (sep-by-comma s))))
+                                   (->> (sep-by-comma s)
+                                        (mapcat sep-by-hashtag)))))
                        (distinct))]
        (if (or (coll? result)
                (and (string? result)
@@ -347,3 +358,35 @@
             (string/join "/" parts)
             (last parts))
           js/decodeURI))))
+
+(defonce non-parsing-properties
+  (atom #{"background-color" "background_color"}))
+
+(defn parse-property
+  [k v]
+  (let [k (name k)
+        v (if (or (symbol? v) (keyword? v)) (name v) (str v))
+        v (string/trim v)]
+    (cond
+      (contains? #{"title" "filters"} k)
+      v
+
+      (= v "true")
+      true
+      (= v "false")
+      false
+
+      (util/safe-re-find #"^\d+$" v)
+      (util/safe-parse-int v)
+
+      (util/wrapped-by-quotes? v) ; wrapped in ""
+      v
+
+      (contains? @non-parsing-properties (string/lower-case k))
+      v
+
+      (link/link? v)
+      v
+
+      :else
+      (split-page-refs-without-brackets v))))

+ 1 - 34
src/main/frontend/util/property.cljs

@@ -6,8 +6,7 @@
             [medley.core :as medley]
             [frontend.format.mldoc :as mldoc]
             [frontend.text :as text]
-            [frontend.util.cursor :as cursor]
-            [frontend.handler.link :as link]))
+            [frontend.util.cursor :as cursor]))
 
 (defonce properties-start ":PROPERTIES:")
 (defonce properties-end ":END:")
@@ -439,35 +438,3 @@
    (vector? block)
    (contains? #{"Property_Drawer" "Properties"}
               (first block))))
-
-(defonce non-parsing-properties
-  (atom #{"background-color" "background_color"}))
-
-(defn parse-property
-  [k v]
-  (let [k (name k)
-        v (if (or (symbol? v) (keyword? v)) (name v) (str v))
-        v (string/trim v)]
-    (cond
-      (contains? #{"title" "filters"} k)
-      v
-
-      (= v "true")
-      true
-      (= v "false")
-      false
-
-      (util/safe-re-find #"^\d+$" v)
-      (util/safe-parse-int v)
-
-      (util/wrapped-by-quotes? v) ; wrapped in ""
-      (util/unquote-string v)
-
-      (contains? @non-parsing-properties (string/lower-case k))
-      v
-
-      (link/link? v)
-      v
-
-      :else
-      (text/split-page-refs-without-brackets v))))

+ 1 - 1
src/test/frontend/format/block_test.cljs

@@ -5,7 +5,7 @@
 (deftest test-extract-properties
   (are [x y] (= (:properties (block/extract-properties x)) y)
     [["year" "1000"]] {:year 1000}
-    [["year" "\"1000\""]] {:year "1000"}
+    [["year" "\"1000\""]] {:year "\"1000\""}
     [["background-color" "#000000"]] {:background-color "#000000"}
     [["alias" "name/with space"]] {:alias #{"name/with space"}}
     [["year" "1000"] ["alias" "name/with space"]] {:year 1000, :alias #{"name/with space"}}

+ 19 - 0
src/test/frontend/text_test.cljs

@@ -137,4 +137,23 @@
     (text/get-string-all-indexes "a.c a.c ab" "a.")
     [0 4]))
 
+(deftest test-parse-property
+  (testing "parse-property"
+    (are [k v y] (= (text/parse-property k v) y)
+      :tags "foo" "foo"
+      :tags "foo, bar" #{"foo" "bar"}
+      :tags "foo,bar" #{"foo" "bar"}
+      :tags "[[foo]]" "[[foo]]"
+      :tags "[[foo]] [[bar]]" #{"foo" "bar"}
+      :tags "[[foo]], [[bar]]" #{"foo" "bar"}
+      :tags "[[foo]], [[bar]], #baz" #{"foo" "bar" "baz"}
+      :tags "#baz, [[foo]], [[bar]]" #{"foo" "bar" "baz"}
+      :tags "[[foo [[bar]]]]" #{"foo [[bar]]"}
+      :tags "[[foo [[bar]]]], baz" #{"baz" "foo [[bar]]"}))
+  (testing "parse-property with quoted strings"
+    (are [k v y] (= (text/parse-property k v) y)
+      :tags "\"foo, bar\"" "\"foo, bar\""
+      :tags "\"[[foo]], [[bar]]\"" "\"[[foo]], [[bar]]\""
+      :tags "baz, \"[[foo]], [[bar]]\"" #{"baz"})))
+
 #_(cljs.test/test-ns 'frontend.text-test)