Browse Source

enhance: one-click to convert old markdown to new markdown files

Tienson Qin 4 years ago
parent
commit
a64090e2a3

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

@@ -31,8 +31,7 @@
        [:a#download-as-html.hidden]
        [:a#download-as-zip.hidden]
        [:a#export-as-markdown.hidden]
-       [:a#convert-markdown-to-unordered-list-or-heading.hidden]
-       ])))
+       [:a#convert-markdown-to-unordered-list-or-heading.hidden]])))
 
 
 (rum/defc export-page

+ 10 - 1
src/main/frontend/components/header.cljs

@@ -19,7 +19,8 @@
             [frontend.handler.page :as page-handler]
             [frontend.handler.web.nfs :as nfs]
             [goog.dom :as gdom]
-            [goog.object :as gobj]))
+            [goog.object :as gobj]
+            [frontend.handler.migrate :as migrate]))
 
 (rum/defc logo < rum/reactive
   [{:keys [white?]}]
@@ -122,6 +123,14 @@
          {:title (t :import)
           :options {:href (rfe/href :import)}
           :icon svg/import-sm})
+
+       (when (and current-repo
+                  ;; (not (:markdown/version (state/get-config)))
+                  )
+         {:title "Convert to more standard Markdown"
+          :options {:on-click (fn [] (migrate/show-convert-notification! current-repo))}
+          :icon svg/import-sm})
+
        {:title [:div.flex-row.flex.justify-between.items-center
                 [:span (t :join-community)]]
         :options {:href "https://discord.gg/KpN4eHY"

+ 32 - 27
src/main/frontend/handler.cljs

@@ -25,7 +25,8 @@
             [frontend.version :as version]
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            [frontend.ui :as ui]))
 
 (defn set-global-error-notification!
   []
@@ -89,7 +90,7 @@
                               (state/set-db-restoring! false))
                             (if false   ; FIXME: incompatible changes
                               (notification/show!
-                               [:p "Database schema changed, please export your notes as a zip file, and re-index your repos."]
+                               [:p "Database schema changed, please backup your notes first, and re-index/re-link your graphs."]
                                :warning
                                false)
                               (store-schema!))
@@ -164,31 +165,35 @@
 (defn start!
   [render]
   (set-global-error-notification!)
-  (let [{:keys [me logged? repos]} (get-me-and-repos)]
-    (when me (state/set-state! :me me))
-    (register-components-fns!)
-    (state/set-db-restoring! true)
-    (render)
-    (on-load-events)
-    (set-network-watcher!)
-
-    (util/indexeddb-check?
-     (fn [_error]
-       (notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
-       (state/set-indexedb-support! false)))
-
-    (events/run!)
-
-    (p/let [repos (get-repos)]
-      (state/set-repos! repos)
-      (restore-and-setup! me repos logged?))
-
-    (reset! db/*sync-search-indice-f search/sync-search-indice!)
-    (db/run-batch-txs!)
-    (file-handler/run-writes-chan!)
-    (shortcut/install-shortcuts!)
-    (when (util/electron?)
-      (el/listen!))))
+  (if (:block/name (storage/get :db-schema))
+    (let [{:keys [me logged? repos]} (get-me-and-repos)]
+      (when me (state/set-state! :me me))
+      (register-components-fns!)
+      (state/set-db-restoring! true)
+      (render)
+      (on-load-events)
+      (set-network-watcher!)
+
+      (util/indexeddb-check?
+       (fn [_error]
+         (notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
+         (state/set-indexedb-support! false)))
+
+      (events/run!)
+
+      (p/let [repos (get-repos)]
+        (state/set-repos! repos)
+        (restore-and-setup! me repos logged?))
+
+      (reset! db/*sync-search-indice-f search/sync-search-indice!)
+      (db/run-batch-txs!)
+      (file-handler/run-writes-chan!)
+      (shortcut/install-shortcuts!)
+      (when (util/electron?)
+        (el/listen!)))
+
+    ;; before refactoring
+    (clear-cache!)))
 
 (defn stop! []
   (prn "stop!"))

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

@@ -39,7 +39,7 @@
      (if (seq blocks)
        blocks
        (let [page-block (when page-name (db/pull [:block/name (string/lower-case page-name)]))
-             create-title-property? (and page-name (util/include-windows-reserved-chars? page-name))
+             create-title-property? (util/create-title-property? page-name)
              content (if create-title-property?
                        (let [title (or (:block/original-name page-block)
                                        (:block/name page-block))

+ 2 - 2
src/main/frontend/handler/export.cljs

@@ -151,12 +151,12 @@
         repo-name (str owner "-" repo-name)]
     (when (seq files)
       (p/let [zipfile (zip/make-zip repo-name files repo)]
-        (when-let [anchor (gdom/getElement "download-as-zip")]
+        (when-let [anchor (gdom/getElement "download")]
           (.setAttribute anchor "href" (js/window.URL.createObjectURL zipfile))
           (.setAttribute anchor "download" (.-name zipfile))
           (.click anchor))))))
 
-(defn- get-md-file-contents
+(defn get-md-file-contents
   [repo]
   (let [conn (db/get-conn repo)]
     (filter (fn [[path _]]

+ 59 - 0
src/main/frontend/handler/migrate.cljs

@@ -0,0 +1,59 @@
+(ns frontend.handler.migrate
+  (:require [frontend.handler.export :as export]
+            [frontend.handler.file :as file]
+            [frontend.handler.repo :as repo]
+            [frontend.handler.config :as config-handler]
+            [frontend.handler.notification :as notification]
+            [frontend.handler.web.nfs :as nfs-handler]
+            [frontend.ui :as ui]
+            [frontend.state :as state]
+            [promesa.core :as p]))
+
+(defn convert-md-files!
+  [repo]
+  (when repo
+    (let [files (export/get-md-file-contents repo)]
+      (when (seq files)
+        (-> (p/all (for [[path content] files]
+                     (file/alter-file repo path content {:add-history? false})))
+            (p/then (fn []
+                      (config-handler/set-config! :markdown/version 2)
+
+                      (p/let [_ (repo/push repo {:commit-message "Converted to new Markdown syntax!"})]
+                        (repo/re-index! nfs-handler/rebuild-index!)
+
+                        (notification/show!
+                         [:div
+                          [:p "All markdown files have been converted to the new Markdown syntax successfully!"]]
+                         :success
+                         false))))
+            (p/catch (fn [e]
+                       (throw e))))))))
+
+(defn show-convert-notification!
+  [repo]
+  (notification/show!
+   [:div
+    [:p "Previously Logseq uses `#` Markdown heading as outliner bullets, since beta, we've changed to use more standard `-` unordered list as outliner bullets."]
+
+    [:p.mt-2 "If you've converted this graph before, click on this button so that you won't see this notification again."]
+    (ui/button "Yes, it's already converted!"
+      :on-click (fn []
+                  (notification/clear-all!)
+                  (config-handler/set-config! :markdown/version 2)))
+    [:hr]
+
+    [:p "Firstly, let's download this graph as a zipfile."]
+    [:p
+     (ui/button "Download this graph"
+       :on-click (fn []
+                   (export/export-repo-as-zip! repo)))]
+
+    [:b "Make sure you've downloaded this graph! Now let's convert the Markdown files."]
+    [:p
+     (ui/button "Start to convert!"
+       :on-click (fn []
+                   (notification/clear-all!)
+                   (convert-md-files! repo)))]]
+   :warning
+   false))

+ 4 - 0
src/main/frontend/handler/notification.cljs

@@ -7,6 +7,10 @@
   (let [contents (state/get-notification-contents)]
     (state/set-state! :notification/contents (dissoc contents uid))))
 
+(defn clear-all!
+  []
+  (state/set-state! :notification/contents nil))
+
 (defn show!
   ([content status]
    (show! content status true nil))

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

@@ -73,7 +73,7 @@
          tx (-> (block/page-name->map title true)
                 (assoc :block/format format))
          page-entity [:block/uuid (:block/uuid tx)]
-         create-title-property? (and title (util/include-windows-reserved-chars? title))
+         create-title-property? (util/create-title-property? title)
          default-properties (default-properties-block title format page-entity)
          empty-block {:block/uuid (db/new-block-id)
                       :block/left [:block/uuid (:block/uuid default-properties)]

+ 5 - 18
src/main/frontend/handler/repo.cljs

@@ -429,25 +429,12 @@
                         (and (not= status :pushing)
                              changed-files?)))
            (git-handler/set-git-status! repo-url :pushing)
-           (util/p-handle
+           (->
             (git/push repo-url token merge-push-no-diff?)
-            (fn [_result]
-              (git-handler/set-git-status! repo-url nil)
-              (git-handler/set-git-error! repo-url nil)
-              (common-handler/check-changed-files-status repo-url))
-            (fn [error]
-              (log/error :git/push-error error)
-              (js/console.error error)
-              (common-handler/check-changed-files-status repo-url)
-
-              (git-handler/set-git-status! repo-url :push-failed)
-              (git-handler/set-git-error! repo-url error)
-
-              (if (state/online?)
-                (pull repo-url {:force-pull? true
-                                :show-diff? true})
-                (when custom-commit?
-                  (p/rejected error)))))))
+            (p/then (fn []
+                      (git-handler/set-git-status! repo-url nil)
+                      (git-handler/set-git-error! repo-url nil)
+                      (common-handler/check-changed-files-status repo-url))))))
        (p/catch (fn [error]
                   (log/error :repo/push-error error)
                   (git-handler/set-git-status! repo-url :push-failed)

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

@@ -109,7 +109,7 @@
    opts))
 
 (defn button
-  [text & {:keys [background href class intent]
+  [text & {:keys [background href class intent on-click]
            :as   option}]
   (let [klass (if-not intent ".bg-indigo-600.hover:bg-indigo-700.focus:border-indigo-700.active:bg-indigo-700")
         klass (if background (string/replace klass "indigo" background) klass)]

+ 8 - 2
src/main/frontend/util.cljc

@@ -1087,12 +1087,18 @@
          (when (uuid-string? block-id)
            (first (array-seq (js/document.getElementsByClassName block-id))))))))
 
-(defonce windows-reserved-chars #"[\\/:\\*\\?\"<>|]+")
+(def windows-reserved-chars #"[\\/:\\*\\?\"<>|]+")
 
 (defn include-windows-reserved-chars?
   [s]
   (safe-re-find windows-reserved-chars s))
 
+(defn create-title-property?
+  [s]
+  (and (string? s)
+       (or (include-windows-reserved-chars? s)
+           (string/includes? s "."))))
+
 (defn page-name-sanity
   [page-name]
   (-> page-name
@@ -1372,4 +1378,4 @@
                  (for [[k v] %]
                    (when v (name k)))
                  (name %))
-              args)))
+              args)))

+ 3 - 1
templates/config.edn

@@ -75,4 +75,6 @@
  ;; input "{{{poem red,blue}}}"
  ;; becomes
  ;; Rose is red, violet's blue. Life's ordered: Org assists you.
- :macros {}}
+ :macros {}
+
+ :markdown/version 2}