Prechádzať zdrojové kódy

fix(export): remove prefix spaces when no-indent (#10807)

* fix(export): remove prefix spaces when no-indent
* fix(export): fix remove-prefix-spaces-in-Plain

use full_content for Src ast when no-indent
rcmerci 1 rok pred
rodič
commit
facc75f635

+ 32 - 8
src/main/frontend/handler/export/common.cljs

@@ -567,6 +567,7 @@
       :result-ast-tcoll
       persistent!))
 
+
 ;;; inline transformers
 
 (defn remove-emphasis
@@ -605,14 +606,35 @@
       ;; else
       [inline-ast])))
 
+(defn remove-prefix-spaces-in-Plain
+  [inline-coll]
+  (:r
+   (reduce
+    (fn [{:keys [r after-break-line?]} ast]
+      (let [[ast-type ast-content] ast]
+        (case ast-type
+          "Plain"
+          (let [trimmed-content (string/triml ast-content)]
+            (if after-break-line?
+              (if (empty? trimmed-content)
+                {:r r :after-break-line? false}
+                {:r (conj r ["Plain" trimmed-content]) :after-break-line? false})
+              {:r (conj r ast) :after-break-line? false}))
+          ("Break_Line" "Hard_Break_Line")
+          {:r (conj r ast) :after-break-line? true}
+        ;; else
+          {:r (conj r ast) :after-break-line? false})))
+    {:r [] :after-break-line? true}
+    inline-coll)))
+
 ;;; inline transformers (ends)
 
 ;;; walk on block-ast, apply inline transformers
 
 (defn- walk-block-ast-helper
-  [inline-coll map-fns-on-inline-ast mapcat-fns-on-inline-ast]
+  [inline-coll map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll]
   (->>
-   inline-coll
+   (reduce (fn [inline-coll f] (f inline-coll)) inline-coll fns-on-inline-coll)
    (mapv #(reduce (fn [inline-ast f] (f inline-ast)) % map-fns-on-inline-ast))
    (mapcatv #(reduce
               (fn [inline-ast-coll f] (mapcatv f inline-ast-coll)) [%] mapcat-fns-on-inline-ast))))
@@ -635,18 +657,20 @@
    list-items))
 
 (defn walk-block-ast
-  [{:keys [map-fns-on-inline-ast mapcat-fns-on-inline-ast] :as fns}
+  [{:keys [map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll] :as fns}
    block-ast]
   (let [[ast-type ast-content] block-ast]
     (case ast-type
       "Paragraph"
-      (mk-paragraph-ast (walk-block-ast-helper ast-content map-fns-on-inline-ast mapcat-fns-on-inline-ast) (meta block-ast))
+      (mk-paragraph-ast
+       (walk-block-ast-helper ast-content map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll)
+       (meta block-ast))
       "Heading"
       (let [{:keys [title]} ast-content]
         ["Heading"
          (assoc ast-content
                 :title
-                (walk-block-ast-helper title map-fns-on-inline-ast mapcat-fns-on-inline-ast))])
+                (walk-block-ast-helper title map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll))])
       "List"
       ["List" (walk-block-ast-for-list ast-content map-fns-on-inline-ast mapcat-fns-on-inline-ast)]
       "Quote"
@@ -654,11 +678,11 @@
       "Footnote_Definition"
       (let [[name contents] (rest block-ast)]
         ["Footnote_Definition"
-         name (walk-block-ast-helper contents map-fns-on-inline-ast mapcat-fns-on-inline-ast)])
+         name (walk-block-ast-helper contents map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll)])
       "Table"
       (let [{:keys [header groups]} ast-content
             header* (mapv
-                     #(walk-block-ast-helper % map-fns-on-inline-ast mapcat-fns-on-inline-ast)
+                     #(walk-block-ast-helper % map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll)
                      header)
             groups* (mapv
                      (fn [group]
@@ -666,7 +690,7 @@
                         (fn [row]
                           (mapv
                            (fn [col]
-                             (walk-block-ast-helper col map-fns-on-inline-ast mapcat-fns-on-inline-ast))
+                             (walk-block-ast-helper col map-fns-on-inline-ast mapcat-fns-on-inline-ast fns-on-inline-coll))
                            row))
                         group))
                      groups)]

+ 4 - 1
src/main/frontend/handler/export/html.cljs

@@ -402,7 +402,10 @@
                                         (update :map-fns-on-inline-ast conj common/remove-page-ref-brackets)
 
                                         (get-in *state* [:export-options :remove-tags?])
-                                        (update :mapcat-fns-on-inline-ast conj common/remove-tags))
+                                        (update :mapcat-fns-on-inline-ast conj common/remove-tags)
+
+                                        (= "no-indent" (get-in *state* [:export-options :indent-style]))
+                                        (update :mapcat-fns-on-inline-ast conj common/remove-prefix-spaces-in-Plain))
             ast*** (if-not (empty? config-for-walk-block-ast)
                      (util/profile :walk-block-ast (mapv (partial common/walk-block-ast config-for-walk-block-ast) ast**))
                      ast**)

+ 4 - 1
src/main/frontend/handler/export/opml.cljs

@@ -420,7 +420,10 @@
                                         (update :map-fns-on-inline-ast conj common/remove-page-ref-brackets)
 
                                         (get-in *state* [:export-options :remove-tags?])
-                                        (update :mapcat-fns-on-inline-ast conj common/remove-tags))
+                                        (update :mapcat-fns-on-inline-ast conj common/remove-tags)
+
+                                        (= "no-indent" (get-in *state* [:export-options :indent-style]))
+                                        (update :mapcat-fns-on-inline-ast conj common/remove-prefix-spaces-in-Plain))
             ast*** (if-not (empty? config-for-walk-block-ast)
                      (mapv (partial common/walk-block-ast config-for-walk-block-ast) ast**)
                      ast**)

+ 29 - 19
src/main/frontend/handler/export/text.cljs

@@ -104,14 +104,18 @@
      l)))
 
 (defn- block-src
-  [{:keys [lines language]}]
-  (let [level (dec (get *state* :current-level 1))]
-    (concatv
-     [(indent-with-2-spaces level) (raw-text "```")]
-     (when language [(raw-text language)])
-     [(newline* 1)]
-     (mapv raw-text lines)
-     [(indent-with-2-spaces level) (raw-text "```") (newline* 1)])))
+  [{:keys [lines language full_content]}]
+  (if (= "no-indent" (get-in *state* [:export-options :indent-style]))
+    ;; when "no-indent", just use :full_content in 'Src' ast
+    [(raw-text full_content) (newline* 1)]
+
+    (let [level (dec (get *state* :current-level 1))]
+      (concatv
+       [(indent-with-2-spaces level) (raw-text "```")]
+       (when language [(raw-text language)])
+       [(newline* 1)]
+       (mapv raw-text lines)
+       [(indent-with-2-spaces level) (raw-text "```") (newline* 1)]))))
 
 (defn- block-quote
   [block-coll]
@@ -313,7 +317,9 @@
 
 (defn- inline-break-line
   []
-  [(raw-text "  \n")
+  [(if (= "no-indent" (get-in *state* [:export-options :indent-style]))
+     (raw-text "\n")
+     (raw-text "  \n"))
    (when (:indent-after-break-line? *state*)
      (let [current-level (get *state* :current-level 1)]
        (when (> current-level 1)
@@ -465,13 +471,17 @@
                                         (update :map-fns-on-inline-ast conj common/remove-page-ref-brackets)
 
                                         (get-in *state* [:export-options :remove-tags?])
-                                        (update :mapcat-fns-on-inline-ast conj common/remove-tags))
+                                        (update :mapcat-fns-on-inline-ast conj common/remove-tags)
+
+                                        (= "no-indent" (get-in *state* [:export-options :indent-style]))
+                                        (update :fns-on-inline-coll conj common/remove-prefix-spaces-in-Plain))
             ast*** (if-not (empty? config-for-walk-block-ast)
                      (mapv (partial common/walk-block-ast config-for-walk-block-ast) ast**)
                      ast**)
             simple-asts (mapcatv block-ast->simple-ast ast***)]
         (simple-asts->string simple-asts)))))
 
+
 (defn export-blocks-as-markdown
   "options:
   :indent-style \"dashes\" | \"spaces\" | \"no-indent\"
@@ -481,15 +491,15 @@
   {:pre [(or (coll? root-block-uuids-or-page-name)
              (string? root-block-uuids-or-page-name))]}
   (util/profile
-   :export-blocks-as-markdown
-   (let [content
-         (if (string? root-block-uuids-or-page-name)
-           ;; page
-           (common/get-page-content root-block-uuids-or-page-name)
-           (common/root-block-uuids->content repo root-block-uuids-or-page-name))
-         first-block (db/entity [:block/uuid (first root-block-uuids-or-page-name)])
-         format (or (:block/format first-block) (state/get-preferred-format))]
-     (export-helper content format options))))
+      :export-blocks-as-markdown
+      (let [content
+            (if (string? root-block-uuids-or-page-name)
+              ;; page
+              (common/get-page-content root-block-uuids-or-page-name)
+              (common/root-block-uuids->content repo root-block-uuids-or-page-name))
+            first-block (db/entity [:block/uuid (first root-block-uuids-or-page-name)])
+            format (or (:block/format first-block) (state/get-preferred-format))]
+        (export-helper content format options))))
 
 (defn export-files-as-markdown
   "options see also `export-blocks-as-markdown`"

+ 17 - 0
src/test/frontend/handler/export_test.cljs

@@ -129,6 +129,23 @@
 	- 4")
     "97a00e55-48c3-48d8-b9ca-417b16e3a616"))
 
+(deftest export-blocks-as-markdown-no-indent
+  (are [expect content]
+      (= (string/trim expect)
+         (string/trim (#'export-text/export-helper (string/trim content) :markdown {:indent-style "no-indent"})))
+      "
+1
+2
+3
+4
+5"
+      "
+- 1
+  2
+  3
+  - 4
+    5"))
+
 
 (deftest-async export-files-as-markdown
   (p/do!