Browse Source

feat(export): convert markdown to unordered list or heading style

rcmerci 4 years ago
parent
commit
f975f3b4bd

+ 12 - 3
src/main/frontend/components/export.cljs

@@ -21,13 +21,18 @@
         [:li.mb-4
          [: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)}
+          (t :convert-markdown)]]
         [:li.mb-4
          [:a.font-medium {:on-click #(export/export-repo-as-edn! current-repo)}
           (t :export-edn)]]]
        [:a#download-as-edn.hidden]
        [:a#download-as-html.hidden]
        [:a#download-as-zip.hidden]
-       [:a#export-as-markdown.hidden]])))
+       [:a#export-as-markdown.hidden]
+       [:a#convert-markdown-to-unordered-list-or-heading.hidden]
+       ])))
 
 
 (rum/defc export-page
@@ -40,5 +45,9 @@
          [:ul.mr-1
           [:li.mb-4
            [:a.font-medium {:on-click #(export/export-page-as-markdown! page)}
-            (t :export-markdown)]]]
-         [:a#export-page-as-markdown.hidden]]))))
+            (t :export-markdown)]]
+          [:li.mb-4
+           [:a.font-medium {:on-click #(export/convert-page-markdown-unordered-list-or-heading! page)}
+            (t :convert-markdown)]]]
+         [:a#export-page-as-markdown.hidden]
+         [:a#convert-markdown-to-unordered-list-or-heading.hidden]]))))

+ 3 - 0
src/main/frontend/dicts.cljs

@@ -322,6 +322,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)"
         :all-graphs "All graphs"
         :all-pages "All pages"
         :all-files "All files"
@@ -1037,6 +1038,7 @@ title: How to take dummy notes?
            :re-index "重新建立索引"
            :export-json "以 JSON 格式导出"
            :export-markdown "以 Markdown 格式导出"
+           :convert-markdown "转换 Markdown 格式(Unordered list 或 Heading)"
            :unlink "解除绑定"
            :search (if config/publishing?
                      "搜索"
@@ -1303,6 +1305,7 @@ title: How to take dummy notes?
              :re-index "重新建立索引"
              :export-json "以 JSON 格式導出"
              :export-markdown "以 Markdown 格式導出"
+             :convert-markdown "轉換 Markdown 格式(Unordered list 或 Heading)"
              :unlink "解除綁定"
              :search (if config/publishing?
                        "搜索"

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

@@ -36,7 +36,9 @@
   ([format]
    (mldoc/default-config format))
   ([format heading-to-list?]
-   (mldoc/default-config format heading-to-list?)))
+   (mldoc/default-config format heading-to-list?))
+  ([format heading-to-list? exporting-keep-properties?]
+   (mldoc/default-config format heading-to-list? exporting-keep-properties?)))
 
 (defn to-html
   ([content format]

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

@@ -27,13 +27,23 @@
         :heading_number false
         :keep_line_break true
         :format format
-        :heading_to_list export-heading-to-list?})))))
+        :heading_to_list export-heading-to-list?}))))
+  ([format export-heading-to-list? exporting-keep-properties?]
+   (let [format (string/capitalize (name (or format :markdown)))]
+     (js/JSON.stringify
+      (bean/->js
+       {:toc false
+        :heading_number false
+        :keep_line_break true
+        :format format
+        :heading_to_list export-heading-to-list?
+        :exporting_keep_properties exporting-keep-properties?}))))
+  )
 
 (def default-references
   (js/JSON.stringify
    (clj->js {:embed_blocks []
-             :embed_pages []
-             :refer_blocks []})))
+             :embed_pages []})))
 
 (defn parse-json
   [content config]

+ 42 - 0
src/main/frontend/handler/export.cljs

@@ -371,6 +371,16 @@
                                              (clj->js (f (first names)))))])))
          (remove nil?))))
 
+(defn- convert-md-files-unordered-list-or-heading
+  [repo files heading-to-list?]
+  (->> files
+       (mapv (fn [{:keys [path content names format]}]
+               (when (first names)
+                 [path (fp/exportMarkdown f/mldoc-record content
+                                          (f/get-default-config format heading-to-list? true)
+                                          nil)])))
+       (remove nil?)))
+
 (defn export-repo-as-markdown!
   [repo]
   (when-let [repo (state/get-current-repo)]
@@ -403,3 +413,35 @@
                   (.setAttribute anchor "href" url)
                   (.setAttribute anchor "download" path)
                   (.click anchor))))))))))
+
+(defn convert-repo-markdown-unordered-list-or-heading!
+  [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))]
+        (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)]
+    (when-let [file (db/get-page-file page-name)]
+      (when-let [path (:file/path file)]
+        (when-let [content (db/get-file path)]
+          (let [names [page-name]
+                format (f/get-format path)
+                files [{:path path :content content :names names :format format}]]
+            (let [files (convert-md-files-unordered-list-or-heading repo files (state/export-heading-to-list?))]
+              (let [data (js/Blob. [(second (first files))]
+                                   (clj->js {:type "text/plain;charset=utf-8,"}))]
+                (let [anchor (gdom/getElement "convert-markdown-to-unordered-list-or-heading")
+                      url (js/window.URL.createObjectURL data)]
+                  (.setAttribute anchor "href" url)
+                  (.setAttribute anchor "download" path)
+                  (.click anchor))))))))))