Browse Source

fix: export md indentation

Tienson Qin 4 years ago
parent
commit
855fe82067

+ 13 - 13
src/main/frontend/components/block.cljs

@@ -1932,19 +1932,6 @@
                                (str error)]))]
                (util/hiccup-keywordize result))
 
-             (and (seq result)
-                  (or only-blocks? blocks-grouped-by-page?))
-             (->hiccup result (cond-> (assoc config
-                                             :custom-query? true
-                                             ;; :breadcrumb-show? true
-                                             :group-by-page? blocks-grouped-by-page?
-                                             ;; :ref? true
-                                             )
-                                children?
-                                (assoc :ref? true))
-                       {:style {:margin-top "0.25rem"
-                                :margin-left "0.25rem"}})
-
              ;; page list
              (and (seq result)
                   (:block/name (first result)))
@@ -1964,6 +1951,19 @@
                                                               :path-params {:name name}})))}
                   (or original-name name)]])]
 
+             (and (seq result)
+                  (or only-blocks? blocks-grouped-by-page?))
+             (->hiccup result (cond-> (assoc config
+                                             :custom-query? true
+                                             ;; :breadcrumb-show? true
+                                             :group-by-page? blocks-grouped-by-page?
+                                             ;; :ref? true
+                                             )
+                                children?
+                                (assoc :ref? true))
+                       {:style {:margin-top "0.25rem"
+                                :margin-left "0.25rem"}})
+
              (seq result)                     ;TODO: table
              (let [result (->>
                            (for [record result]

+ 28 - 9
src/main/frontend/db/model.cljs

@@ -551,17 +551,36 @@
             rules)
            (apply concat)))))
 
+;; FIXME: alert
+(defn- keep-only-one-file
+  [blocks parent]
+  (if-let [file (:db/id (:block/file parent))]
+    (filter (fn [b] (= (:db/id (:block/file b)) file)) blocks)
+    blocks))
+
 (defn sort-by-left
   [blocks parent]
-  (when (not= (count blocks) (count (set (map :block/left blocks))))
-    (util/pprint blocks))
-  (assert (= (count blocks) (count (set (map :block/left blocks)))) "Each block should have a different left node")
-  (let [left->blocks (reduce (fn [acc b] (assoc acc (:db/id (:block/left b)) b)) {} blocks)]
-    (loop [block parent
-           result []]
-      (if-let [next (get left->blocks (:db/id block))]
-        (recur next (conj result next))
-        (vec result)))))
+  (let [blocks (keep-only-one-file blocks parent)]
+    (when (not= (count blocks) (count (set (map :block/left blocks))))
+     (let [duplicates (->> (map (comp :db/id :block/left) blocks)
+                           frequencies
+                           (filter (fn [[_k v]] (> v 1)))
+                           (map (fn [[k _v]]
+                                  (let [left (db-utils/pull k)]
+                                    {:left left
+                                     :duplicates (->>
+                                                  (filter (fn [block]
+                                                            (= k (:db/id (:block/left block))))
+                                                          blocks)
+                                                  (map #(select-keys % [:db/id :block/level :block/content :block/file])))}))))]
+       (util/pprint duplicates)))
+    (assert (= (count blocks) (count (set (map :block/left blocks)))) "Each block should have a different left node")
+    (let [left->blocks (reduce (fn [acc b] (assoc acc (:db/id (:block/left b)) b)) {} blocks)]
+      (loop [block parent
+             result []]
+        (if-let [next (get left->blocks (:db/id block))]
+          (recur next (conj result next))
+          (vec result))))))
 
 (defn get-block-immediate-children
   "Doesn't include nested children."

+ 1 - 1
src/main/frontend/dicts.cljs

@@ -318,7 +318,7 @@ title: How to take dummy notes?
         :export-markdown "Export as Markdown"
         :export-public-pages "Export public pages"
         :export-edn "Export as EDN"
-        :convert-markdown "Convert Markdown(Unordered list or Heading)"
+        :convert-markdown "Convert Markdown headings to unordered lists (# -> -)"
         :all-graphs "All graphs"
         :all-pages "All pages"
         :all-files "All files"

+ 34 - 25
src/main/frontend/handler/export.cljs

@@ -24,6 +24,13 @@
    (outliner-tree/blocks->vec-tree
     (db/get-page-blocks-no-cache page) page) {:init-level 1}))
 
+(defn- get-page-content-debug
+  [page]
+  (outliner-file/tree->file-content
+   (outliner-tree/blocks->vec-tree
+    (db/get-page-blocks-no-cache page) page) {:init-level 1
+                                              :heading-to-list? true}))
+
 (defn- get-file-content
   [file-path]
   (let [page-name
@@ -120,19 +127,21 @@
           (.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})])))))
+  ([repo]
+   (get-file-contents repo {:init-level 1}))
+  ([repo file-opts]
+   (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)
+                    file-opts)]))))))
 
 (defn export-repo-as-zip!
   [repo]
@@ -153,7 +162,7 @@
               (let [path (string/lower-case path)]
                 (or (string/ends-with? path ".md")
                     (string/ends-with? path ".markdown"))))
-            (get-file-contents repo))))
+            (get-file-contents repo {:init-level 1 :heading-to-list? true}))))
 
 (defn- get-embed-and-refs-blocks-pages-aux []
   (let [mem (atom {})]
@@ -456,17 +465,6 @@
                   (.setAttribute anchor "download" path)
                   (.click anchor))))))))))
 
-(defn convert-repo-markdown-v2!
-  [repo]
-  (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))
-            (.setAttribute anchor "download" (.-name zipfile))
-            (.click anchor)))))))
-
 (defn convert-page-markdown-unordered-list-or-heading!
   [page-name]
   (when-let [repo (state/get-current-repo)]
@@ -484,3 +482,14 @@
                   (.setAttribute anchor "href" url)
                   (.setAttribute anchor "download" path)
                   (.click anchor))))))))))
+
+(defn convert-repo-markdown-v2!
+  [repo]
+  (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))
+            (.setAttribute anchor "download" (.-name zipfile))
+            (.click anchor)))))))

+ 22 - 6
src/main/frontend/modules/file/core.cljs

@@ -21,7 +21,7 @@
     (string/join (str "\n" spaces-tabs) lines)))
 
 (defn transform-content
-  [{:block/keys [format pre-block? title content unordered body]} level]
+  [{:block/keys [format pre-block? title content unordered body heading-level]} level heading-to-list?]
   (let [content (or content "")
         heading-with-title? (seq title)]
     (cond
@@ -39,28 +39,44 @@
                 (repeat level "*")
                 (apply str)) ""]
 
-              (and (= format :markdown) (not unordered)) ; heading
+              (and (= format :markdown) (not unordered) (not heading-to-list?)) ; heading
               ["" ""]
 
               :else
-              (let [spaces-tabs (->>
+              (let [level (if (and heading-to-list? heading-level)
+                            (if (> heading-level 1)
+                              (dec heading-level)
+                              heading-level)
+                            level)
+                    spaces-tabs (->>
                                  (repeat (dec level) (state/get-export-bullet-indentation))
                                  (apply str))]
                 [(str spaces-tabs "-") (str spaces-tabs "  ")]))
+            content (if heading-to-list?
+                      (-> (string/replace content #"^\s?#+\s+" "")
+                          (string/replace #"^\s?#+\s?$" ""))
+                      content)
             new-content (indented-block-content (string/trim content) spaces-tabs)
-            sep (if heading-with-title?
+            sep (cond
+                  heading-with-title?
                   " "
+
+                  (string/blank? new-content)
+                  ""
+
+                  :else
                   (str "\n" spaces-tabs))]
         (str prefix sep new-content)))))
 
 (defn tree->file-content
-  [tree {:keys [init-level]}]
+  [tree {:keys [init-level heading-to-list?]
+         :or {heading-to-list? false}}]
   (loop [block-contents []
          [f & r] tree
          level init-level]
     (if (nil? f)
       (string/join "\n" block-contents)
-      (let [content (transform-content f level)
+      (let [content (transform-content f level heading-to-list?)
             new-content
             (if-let [children (seq (:block/children f))]
               [content (tree->file-content children {:init-level (inc level)})]

+ 1 - 1
src/main/frontend/modules/outliner/datascript.cljc

@@ -37,7 +37,7 @@
 #?(:cljs
    (defn transact!
      [txs opts]
-     (util/pprint txs)
+     ;; (util/pprint txs)
      (when (and (seq txs)
                 (not (:skip-transact? opts)))
        (let [conn (conn/get-conn false)