From b991599244be141ec4a6943d8b940ac3945e2b0c Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 12 May 2021 20:10:03 +0800 Subject: [PATCH] fix: filters close #1783 --- src/main/frontend/components/reference.cljs | 3 +- src/main/frontend/db.cljs | 2 +- src/main/frontend/db/model.cljs | 28 ++----------- src/main/frontend/format/block.cljs | 17 +++----- src/main/frontend/format/mldoc.cljs | 13 ------ src/main/frontend/format/mldoc_test.cljs | 42 +------------------ src/main/frontend/handler/extract.cljs | 46 +++++++++++++++++---- src/main/frontend/text.cljs | 7 ++++ src/main/frontend/util.cljc | 5 ++- 9 files changed, 63 insertions(+), 100 deletions(-) diff --git a/src/main/frontend/components/reference.cljs b/src/main/frontend/components/reference.cljs index 76cf88f45..37989bbfa 100644 --- a/src/main/frontend/components/reference.cljs +++ b/src/main/frontend/components/reference.cljs @@ -81,7 +81,8 @@ filtered-ref-blocks (block-handler/filter-blocks repo ref-blocks filters true) n-ref (count filtered-ref-blocks)] (when (or (> n-ref 0) - (seq scheduled-or-deadlines)) + (seq scheduled-or-deadlines) + (seq filter-state)) [:div.references.mt-6.flex-1.flex-row [:div.content (when (seq scheduled-or-deadlines) diff --git a/src/main/frontend/db.cljs b/src/main/frontend/db.cljs index df909b63f..759dec954 100644 --- a/src/main/frontend/db.cljs +++ b/src/main/frontend/db.cljs @@ -45,7 +45,7 @@ get-file-blocks get-file-contents get-file-last-modified-at get-file-no-sub get-file-page get-file-page-id file-exists? get-file-pages get-files get-files-blocks get-files-full get-files-that-referenced-page get-journals-length get-latest-journals get-marker-blocks get-matched-blocks get-page get-page-alias get-page-alias-names get-page-blocks get-page-linked-refs-refed-pages - get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format get-page-name get-page-properties + get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format get-page-properties get-page-properties-content get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references get-page-referenced-blocks-no-cache get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index 63ce0697c..17046ea67 100644 --- a/src/main/frontend/db/model.cljs +++ b/src/main/frontend/db/model.cljs @@ -172,7 +172,8 @@ :where [?file :file/path ?path] [(?pred $ ?path)] - [?block :block/file ?file]] + [?block :block/file ?file] + [(missing? $ ?block :block/name)]] (conn/get-conn repo-url) pred) db-utils/seq-flatten))) @@ -182,7 +183,8 @@ :in $ ?path :where [?file :file/path ?path] - [?block :block/file ?file]] + [?block :block/file ?file] + [(missing? $ ?block :block/name)]] (conn/get-conn repo-url) path) db-utils/seq-flatten)) @@ -701,28 +703,6 @@ (vector? block) (= "Heading" (first block)))) -(defn get-page-name - [file ast] - ;; headline - (let [ast (map first ast)] - (if (string/includes? file "pages/contents.") - "Contents" - (let [first-block (last (first (filter heading-block? ast))) - property-name (when (and (= "Properties" (ffirst ast)) - (not (string/blank? (:title (last (first ast)))))) - (:title (last (first ast)))) - first-block-name (let [title (last (first (:title first-block)))] - (and first-block - (string? title) - title)) - file-name (when-let [file-name (last (string/split file #"/"))] - (-> (first (util/split-last "." file-name)) - (string/replace "." "/")))] - (or property-name - (if (= (state/page-name-order) "heading") - (or first-block-name file-name) - (or file-name first-block-name))))))) - (defn get-page-original-name [page-name] (when page-name diff --git a/src/main/frontend/format/block.cljs b/src/main/frontend/format/block.cljs index d1e370144..151d92343 100644 --- a/src/main/frontend/format/block.cljs +++ b/src/main/frontend/format/block.cljs @@ -41,7 +41,8 @@ (when (and (not (util/starts-with? page "http:")) (not (util/starts-with? page "https:")) (not (util/starts-with? page "file:")) - (or (= ext :excalidraw) (not (contains? (config/supported-formats) ext)))) + (or (= ext :excalidraw) + (not (contains? (config/supported-formats) ext)))) page))) (and @@ -144,13 +145,6 @@ (vector? block) (= "Timestamp" (first block)))) -(defn properties-block? - [block] - (and - (vector? block) - (contains? #{"Property_Drawer" "Properties"} - (first block)))) - (defn definition-list-block? [block] (and @@ -299,7 +293,7 @@ refs) (remove string/blank?)) refs (->> (distinct (concat refs children-pages)) - (remove nil?)) + (remove nil?)) refs (map (fn [ref] (page-name->map ref with-id?)) refs)] (assoc block :refs refs)))) @@ -434,7 +428,7 @@ (drop-while #(= ["Break_Line"] %)))] (recur headings (conj block-body ["Paragraph" other-body]) (rest blocks) timestamps' properties last-pos last-level children)) - (properties-block? block) + (text/properties-block? block) (let [properties (extract-properties block start_pos end_pos)] (recur headings block-body (rest blocks) timestamps properties last-pos last-level children)) @@ -497,7 +491,8 @@ (when (seq block-body) (reset! pre-block-body (reverse block-body))) (when (seq properties) - (reset! pre-block-properties (:properties properties))) + (let [properties (:properties properties)] + (reset! pre-block-properties properties))) (-> (reverse headings) safe-blocks))))] (let [first-block (first blocks) diff --git a/src/main/frontend/format/mldoc.cljs b/src/main/frontend/format/mldoc.cljs index b656be736..7e5a7fcf5 100644 --- a/src/main/frontend/format/mldoc.cljs +++ b/src/main/frontend/format/mldoc.cljs @@ -212,16 +212,3 @@ (defn plain->text [plains] (string/join (map last plains))) - -(defn parse-properties - [content format] - (let [ast (->> (->edn content - (default-config format)) - (map first)) - properties (collect-page-properties ast) - properties (let [properties (and (seq ast) - (= "Properties" (ffirst ast)) - (last (first ast)))] - (if (and properties (seq properties)) - properties))] - (into {} properties))) diff --git a/src/main/frontend/format/mldoc_test.cljs b/src/main/frontend/format/mldoc_test.cljs index 51ea3878f..7b7bbbceb 100644 --- a/src/main/frontend/format/mldoc_test.cljs +++ b/src/main/frontend/format/mldoc_test.cljs @@ -1,44 +1,4 @@ (ns frontend.format.mldoc-test - (:require [frontend.format.mldoc :refer [parse-properties]] + (:require [frontend.format.mldoc] [clojure.string :as string] [cljs.test :refer [deftest are is testing]])) - -(deftest test-parse-org-properties - [] - (testing "just title" - (let [content "#+TITLE: some title " - props (parse-properties content "org")] - (are [x y] (= x y) - ;; TODO: should we trim in parse-properties? - "some title" (string/trim (:title props))))) - - (testing "filetags" - (let [content " -#+FILETAGS: :tag1:tag_2:@tag: -#+ROAM_TAGS: roamtag -body" - props (parse-properties content "org")] - (are [x y] (= x y) - (list "@tag" "tag1" "tag_2") (sort (:filetags props)) - ["roamtag"] (:roam_tags props) - (list "@tag" "roamtag" "tag1" "tag_2") (sort (:tags props))))) - - (testing "roam tags" - (let [content " -#+FILETAGS: filetag -#+ROAM_TAGS: roam1 roam2 -body -" - props (parse-properties content "org")] - (are [x y] (= x y) - ["roam1" "roam2"] (:roam_tags props) - (list "filetag" "roam1" "roam2") (sort (:tags props))))) - - (testing "quoted roam tags" - (let [content " -#+ROAM_TAGS: \"why would\" you use \"spaces\" xxx -body -" - props (parse-properties content "org")] - ;; TODO maybe need to sort or something - (is (= ["why would" "spaces" "you" "use" "xxx"] (:roam_tags props)))))) diff --git a/src/main/frontend/handler/extract.cljs b/src/main/frontend/handler/extract.cljs index bcc5de733..3755a83f4 100644 --- a/src/main/frontend/handler/extract.cljs +++ b/src/main/frontend/handler/extract.cljs @@ -12,7 +12,10 @@ [frontend.format.block :as block] [frontend.format :as format] [cljs-time.core :as t] - [cljs-time.coerce :as tc])) + [cljs-time.coerce :as tc] + [medley.core :as medley] + [clojure.walk :as walk] + [frontend.state :as state])) (defn- extract-page-list [content] @@ -52,12 +55,34 @@ content (remove-indentation-spaces content (:block/level block))))))) +(defn get-page-name + [file ast] + ;; headline + (let [ast (map first ast)] + (if (string/includes? file "pages/contents.") + "Contents" + (let [first-block (last (first (filter block/heading-block? ast))) + property-name (when (and (contains? #{"Properties" "Property_Drawer"} (ffirst ast)) + (not (string/blank? (:title (last (first ast)))))) + (:title (last (first ast)))) + first-block-name (let [title (last (first (:title first-block)))] + (and first-block + (string? title) + title)) + file-name (when-let [file-name (last (string/split file #"/"))] + (-> (first (util/split-last "." file-name)) + (string/replace "." "/")))] + (or property-name + (if (= (state/page-name-order) "heading") + (or first-block-name file-name) + (or file-name first-block-name))))))) + ;; TODO: performance improvement (defn- extract-pages-and-blocks [repo-url format ast properties file content utf8-content journal?] (try (let [now (tc/to-long (t/now)) - page (db/get-page-name file ast) + page (get-page-name file ast) [page page-name journal-day] (block/convert-page-if-journal page) blocks (->> (block/extract-blocks ast content false format) (block/with-parent-and-left [:block/name (string/lower-case page)])) @@ -154,12 +179,17 @@ format (format/get-format file) ast (mldoc/->edn content (mldoc/default-config format)) - first-block (first ast) - properties (let [properties (and (seq first-block) - (= "Properties" (ffirst first-block)) - (last (first first-block)))] - (if (and properties (seq properties)) - properties))] + first-block (ffirst ast) + properties (let [properties (and (text/properties-block? first-block) + (->> (last first-block) + (into {}) + (walk/keywordize-keys)))] + (when (and properties (seq properties)) + (if (:filters properties) + (update properties :filters + (fn [v] + (string/replace (or v "") "\\" ""))) + properties)))] (extract-pages-and-blocks repo-url format ast properties diff --git a/src/main/frontend/text.cljs b/src/main/frontend/text.cljs index 40a2027dc..4cfe595db 100644 --- a/src/main/frontend/text.cljs +++ b/src/main/frontend/text.cljs @@ -433,3 +433,10 @@ (defn image-link? [img-formats s] (some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt "(?:\\?([^#]*))?(?:#(.*))?$")) s)) img-formats)) + +(defn properties-block? + [block] + (and + (vector? block) + (contains? #{"Property_Drawer" "Properties"} + (first block)))) diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index 1f4c3a762..8824b36ed 100644 --- a/src/main/frontend/util.cljc +++ b/src/main/frontend/util.cljc @@ -1133,7 +1133,10 @@ ;; fs (defn get-file-ext [file] - (last (string/split file #"\."))) + (and + (string? file) + (string/includes? file ".") + (last (string/split file #"\.")))) (defn get-dir-and-basename [path]