浏览代码

fix: markdown export and custom query

Tienson Qin 4 年之前
父节点
当前提交
f8d78549db

+ 7 - 4
src/main/frontend/components/block.cljs

@@ -1899,12 +1899,15 @@
            ;; exclude the current one, otherwise it'll loop forever
            remove-blocks (if current-block-uuid [current-block-uuid] nil)
            query-result (and query-atom (rum/react query-atom))
+           result (cond
+                    (and query-result dsl-query?)
+                    (apply concat query-result)
 
-           result (if (and query-result dsl-query?)
                     query-result
-                    (db/custom-query-result-transform query-result remove-blocks q))
-           result (if query-result
-                    (db/custom-query-result-transform query-result remove-blocks q))
+                    (db/custom-query-result-transform query-result remove-blocks q)
+
+                    :else
+                    nil)
            view-f (and view (sci/eval-string (pr-str view)))
            only-blocks? (:block/uuid (first result))
            blocks-grouped-by-page? (and (seq result)

+ 1 - 1
src/main/frontend/components/export.cljs

@@ -22,7 +22,7 @@
          [:a.font-medium {:on-click #(export/export-repo-as-markdown! current-repo)}
           (t :export-markdown)]]
         [:li.mb-4
-         [:a.font-medium {:on-click #(export/convert-repo-markdown-unordered-list-or-heading! current-repo)}
+         [:a.font-medium {:on-click #(export/convert-repo-markdown-v2! current-repo)}
           (t :convert-markdown)]]
         [:li.mb-4
          [:a.font-medium {:on-click #(export/export-repo-as-edn! current-repo)}

+ 5 - 0
src/main/frontend/db/utils.cljs

@@ -114,3 +114,8 @@
    (when-let [db (conn/get-conn repo-url)]
      (some-> (d/entity db key)
              key))))
+
+(defn q
+  [query & inputs]
+  (when-let [repo (state/get-current-repo)]
+    (apply d/q query (conn/get-conn repo) inputs)))

+ 41 - 37
src/main/frontend/handler/export.cljs

@@ -22,7 +22,7 @@
   [page]
   (outliner-file/tree->file-content
    (outliner-tree/blocks->vec-tree
-    (db/get-page-blocks-no-cache page) page) 1))
+    (db/get-page-blocks-no-cache page) page) {:init-level 1}))
 
 (defn- get-file-content
   [file-path]
@@ -34,26 +34,12 @@
                        [?p :block/name ?pn]] (db/get-conn)))]
     (get-page-content page-name)))
 
-(defn- get-file-contents
-  [repo]
-  (let [conn (db/get-conn repo)]
-    (->> (d/q '[:find ?n ?fp
-                :where
-                [?e :block/file ?f]
-                [?f :file/path ?fp]
-                [?e :block/name ?n]] conn)
-         (mapv (fn [[page-name file-path]]
-                 [file-path
-                  (outliner-file/tree->file-content
-                   (outliner-tree/blocks->vec-tree
-                    (db/get-page-blocks-no-cache page-name) page-name) 1)])))))
-
 (defn- get-blocks-contents
   [repo root-block-uuid]
   (->
    (db/get-block-and-children repo root-block-uuid)
    (outliner-tree/blocks->vec-tree (str root-block-uuid))
-   (outliner-file/tree->file-content 1)))
+   (outliner-file/tree->file-content {:init-level 1})))
 
 (defn copy-block!
   [block-id]
@@ -133,7 +119,20 @@
           (.setAttribute anchor "download" "index.html")
           (.click anchor))))))
 
-
+(defn- get-file-contents
+  [repo]
+  (let [conn (db/get-conn repo)]
+    (->> (d/q '[:find ?n ?fp
+                :where
+                [?e :block/file ?f]
+                [?f :file/path ?fp]
+                [?e :block/name ?n]] conn)
+         (mapv (fn [[page-name file-path]]
+                 [file-path
+                  (outliner-file/tree->file-content
+                   (outliner-tree/blocks->vec-tree
+                    (db/get-page-blocks-no-cache page-name) page-name)
+                   {:init-level 1})])))))
 
 (defn export-repo-as-zip!
   [repo]
@@ -147,21 +146,14 @@
           (.setAttribute anchor "download" (.-name zipfile))
           (.click anchor))))))
 
-(defn- get-file-contents-with-suffix
+(defn- get-md-file-contents
   [repo]
   (let [conn (db/get-conn repo)]
-    (->>
-     (filterv (fn [[path _]]
-                (or (string/ends-with? path ".md")))
-              (get-file-contents repo))
-     (mapv (fn [[path content]] {:path path :content content
-                                 :names (d/q '[:find [?n ?n2]
-                                               :in $ ?p
-                                               :where [?e :file/path ?p]
-                                               [?e2 :block/file ?e]
-                                               [?e2 :block/name ?n]
-                                               [?e2 :block/original-name ?n2]] conn path)
-                                 :format (f/get-format path)})))))
+    (filter (fn [[path _]]
+              (let [path (string/lower-case path)]
+                (or (string/ends-with? path ".md")
+                    (string/ends-with? path ".markdown"))))
+            (get-file-contents repo))))
 
 (defn- get-embed-and-refs-blocks-pages-aux []
   (let [mem (atom {})]
@@ -416,6 +408,21 @@
                                           nil)])))
        (remove nil?)))
 
+(defn- get-file-contents-with-suffix
+  [repo]
+  (let [conn (db/get-conn repo)
+        md-files (get-md-file-contents repo)]
+    (->>
+     md-files
+     (map (fn [[path content]] {:path path :content content
+                               :names (d/q '[:find [?n ?n2]
+                                             :in $ ?p
+                                             :where [?e :file/path ?p]
+                                             [?e2 :block/file ?e]
+                                             [?e2 :block/name ?n]
+                                             [?e2 :block/original-name ?n2]] conn path)
+                               :format (f/get-format path)})))))
+
 (defn export-repo-as-markdown!
   [repo]
   (when-let [repo (state/get-current-repo)]
@@ -449,14 +456,11 @@
                   (.setAttribute anchor "download" path)
                   (.click anchor))))))))))
 
-(defn convert-repo-markdown-unordered-list-or-heading!
+(defn convert-repo-markdown-v2!
   [repo]
-  (when-let [repo (state/get-current-repo)]
-    (when-let [files (get-file-contents-with-suffix repo)]
-      (let [heading-to-list? (state/export-heading-to-list?)
-            files
-            (convert-md-files-unordered-list-or-heading repo files heading-to-list?)
-            zip-file-name (str repo "_markdown_" (quot (util/time-ms) 1000))]
+  (when repo
+    (when-let [files (get-md-file-contents repo)]
+      (let [zip-file-name (str repo "_markdown_" (quot (util/time-ms) 1000))]
         (p/let [zipfile (zip/make-zip zip-file-name files)]
           (when-let [anchor (gdom/getElement "convert-markdown-to-unordered-list-or-heading")]
             (.setAttribute anchor "href" (js/window.URL.createObjectURL zipfile))

+ 3 - 7
src/main/frontend/modules/file/core.cljs

@@ -31,10 +31,6 @@
                         (string/replace "\"" "\\\""))]
         (str content "\n"))
 
-      (and (= format :markdown)
-           (re-find #"#+\s+" content))
-      (string/trim content)
-
       :else
       (let [[prefix spaces-tabs]
             (cond
@@ -58,7 +54,7 @@
         (str prefix sep new-content)))))
 
 (defn tree->file-content
-  [tree init-level]
+  [tree {:keys [init-level]}]
   (loop [block-contents []
          [f & r] tree
          level init-level]
@@ -67,7 +63,7 @@
       (let [content (transform-content f level)
             new-content
             (if-let [children (seq (:block/children f))]
-              [content (tree->file-content children (inc level))]
+              [content (tree->file-content children {:init-level (inc level)})]
               [content])]
         (recur (into block-contents new-content) r level)))))
 
@@ -116,7 +112,7 @@
 (defn save-tree-aux!
   [page-block tree]
   (let [page-block (db/pull (:db/id page-block))
-        new-content (tree->file-content tree init-level)
+        new-content (tree->file-content tree {:init-level init-level})
         file-db-id (-> page-block :block/file :db/id)
         file-path (-> (db-utils/entity file-db-id) :file/path)
         _ (assert (string? file-path) "File path should satisfy string?")