Browse Source

refactor: add markdown unordered list as blocks

Tienson Qin 4 years ago
parent
commit
10ab134f3d

+ 1 - 1
package.json

@@ -73,7 +73,7 @@
         "ignore": "^5.1.8",
         "is-svg": "4.2.2",
         "jszip": "^3.5.0",
-        "mldoc": "0.6.6",
+        "mldoc": "0.6.7",
         "mousetrap": "^1.6.5",
         "path": "^0.12.7",
         "react": "^17.0.1",

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

@@ -109,10 +109,8 @@
     (case format
       :org
       "*"
-      :markdown
-      "#"
 
-      "")))
+      "-")))
 
 (defn get-hr
   [format]

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

@@ -479,7 +479,7 @@
                        (->
                         {:uuid (db/new-block-id)
                          :content content
-                         :level 2
+                         :level 1
                          :meta {:start-pos 0
                                 :end-pos (or first-block-start-pos
                                              (utf8/length encoded-content))}

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

@@ -77,7 +77,7 @@
         file-path (str "/" path)
         default-content (case (name format)
                           "org"
-                          "** What's **Contents**?\n*** It's a normal page called [[Contents]], you can use it for:\n**** 1. table of content/index/MOC\n**** 2. pinning/bookmarking favorites pages/blocks (e.g. [[Logseq]])\n**** 3. You can also put many different things, depending on your personal workflow."
+                          "* What's **Contents**?\n** It's a normal page called [[Contents]], you can use it for:\n*** 1. table of content/index/MOC\n*** 2. pinning/bookmarking favorites pages/blocks (e.g. [[Logseq]])\n*** 3. You can also put many different things, depending on your personal workflow."
                           "markdown"
                           "## What's **Contents**?\n### It's a normal page called [[Contents]], you can use it for:\n#### 1. table of content/index/MOC\n#### 2. pinning/bookmarking favorites pages/blocks (e.g. [[Logseq]])\n#### 3. You can also put many different things, depending on your personal workflow."
                           "")]

+ 19 - 16
src/main/frontend/modules/file/core.cljs

@@ -15,23 +15,27 @@
             [frontend.modules.outliner.tree :as tree]
             [promesa.core :as p]))
 
-(defn clip-content
-  [content]
-  (->
-    (string/replace content #"^\n+" "")
-    (string/replace #"^#+" "")
-    (string/replace #"\n+$" "")))
-
 (defn transform-content
-  [pre-block? content level]
+  [{:block/keys [format pre-block? content unordered]} level]
   (let [content (or content "")]
     (if pre-block?
-     (clip-content content)
-     (let [prefix (->>
-                   (repeat level "#")
-                   (apply str))
-           new-content (clip-content content)]
-       (str prefix " " new-content)))))
+      (string/trim content)
+      (let [prefix (cond
+                     (= format :org)
+                     (->>
+                      (repeat level "*")
+                      (apply str))
+
+                     (and (= format :markdown) (not unordered)) ; heading
+                     ""
+
+                     :else
+                     (str (->>
+                           (repeat (dec level) "  ")
+                           (apply str))
+                          "-"))         ; TODO:
+            new-content (string/trim content)]
+        (str prefix " " new-content)))))
 
 (defn tree->file-content
   [tree init-level]
@@ -40,8 +44,7 @@
          level init-level]
     (if (nil? f)
       (string/join "\n" block-contents)
-      (let [content (transform-content
-                      (:block/pre-block? f) (:block/content f) level)
+      (let [content (transform-content f level)
             new-content
             (if-let [children (seq (:block/children f))]
               [content (tree->file-content children (inc level))]

+ 10 - 3
src/main/frontend/text.cljs

@@ -84,14 +84,21 @@
   ([text format]
    (remove-level-spaces text format false))
   ([text format space?]
-   (if-not (string/blank? text)
+   (cond
+     (string/blank? text)
+     ""
+
+     (and (= "markdown" (name format))
+          (string/starts-with? text "---"))
+     text
+
+     :else
      (let [pattern (util/format
                     (if space?
                       "^[%s]+\\s+"
                       "^[%s]+\\s?")
                     (config/get-block-pattern format))]
-       (string/replace-first text (re-pattern pattern) ""))
-     "")))
+       (string/replace-first (string/triml text) (re-pattern pattern) "")))))
 
 (defn append-newline-after-level-spaces
   [text format]

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

@@ -1025,12 +1025,9 @@
                       "")
          new-block (case (name text-format)
                      "org"
-                     "** "
+                     "* "
 
-                     "markdown"
-                     "## "
-
-                     "")]
+                     "- ")]
      (if contents?
        new-block
        (str properties "\n\n" (if new-block? new-block))))))

+ 4 - 4
yarn.lock

@@ -3853,10 +3853,10 @@ mkdirp@^0.5.4, mkdirp@~0.5.1:
   dependencies:
     minimist "^1.2.5"
 
[email protected].6:
-  version "0.6.6"
-  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.6.6.tgz#b38b64f83424514a2cb29e3a1f2cfc8cffaaca2c"
-  integrity sha512-oktdGC4KRDn5nSeAJY9Io55az+YlZg+k7om0Tuo0YeO3LU0hsxnKRB0h5dsy/lufDQn7Ay2EcSPlNs+oPBzi3A==
[email protected].7:
+  version "0.6.7"
+  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.6.7.tgz#7a6860202032146ae8bc14ea9850ae4ed1ff675f"
+  integrity sha512-jpcSytk28O8ushb3WNHApxw8SJCk0FndCWEIAUE7M1RigLGaI61wZX4tGQuFGLheT1iZS5n592KK1f0G0KKWIQ==
   dependencies:
     yargs "^12.0.2"