Browse Source

fix: issue-9380 - #card should only be appended to the title

https://github.com/logseq/logseq/issues/9380

Bug:
Make block with any text and one property after the text
Use bullet context menu to call «Make Flashcard» command
Uday Kalbhor 2 năm trước cách đây
mục cha
commit
88b61adbd7

+ 44 - 41
src/main/frontend/extensions/srs.cljs

@@ -1,35 +1,36 @@
 (ns frontend.extensions.srs
-  (:require [frontend.template :as template]
-            [frontend.db.query-dsl :as query-dsl]
-            [frontend.db.query-react :as query-react]
-            [frontend.util :as util]
-            [logseq.graph-parser.property :as gp-property]
-            [logseq.graph-parser.util.page-ref :as page-ref]
-            [frontend.util.property :as property]
-            [frontend.util.drawer :as drawer]
-            [frontend.util.persist-var :as persist-var]
-            [frontend.db :as db]
-            [frontend.db.model :as db-model]
-            [frontend.db-mixins :as db-mixins]
-            [frontend.state :as state]
-            [frontend.handler.editor :as editor-handler]
-            [frontend.handler.editor.property :as editor-property]
+  (:require [cljs-time.coerce :as tc]
+            [cljs-time.core :as t]
+            [cljs-time.local :as tl]
+            [clojure.string :as string]
+            [frontend.commands :as commands]
             [frontend.components.block :as component-block]
+            [frontend.components.editor :as editor]
             [frontend.components.macro :as component-macro]
             [frontend.components.select :as component-select]
             [frontend.components.svg :as svg]
-            [frontend.ui :as ui]
+            [frontend.context.i18n :refer [t]]
             [frontend.date :as date]
-            [frontend.commands :as commands]
-            [frontend.components.editor :as editor]
-            [cljs-time.core :as t]
-            [cljs-time.local :as tl]
-            [cljs-time.coerce :as tc]
-            [clojure.string :as string]
-            [rum.core :as rum]
+            [frontend.db :as db]
+            [frontend.db-mixins :as db-mixins]
+            [frontend.db.model :as db-model]
+            [frontend.db.query-dsl :as query-dsl]
+            [frontend.db.query-react :as query-react]
+            [frontend.handler.editor :as editor-handler]
+            [frontend.handler.editor.property :as editor-property]
             [frontend.modules.shortcut.core :as shortcut]
+            [frontend.state :as state]
+            [frontend.template :as template]
+            [frontend.ui :as ui]
+            [frontend.util :as util]
+            [frontend.util.block-content :as content]
+            [frontend.util.drawer :as drawer]
+            [frontend.util.persist-var :as persist-var]
+            [frontend.util.property :as property]
+            [logseq.graph-parser.property :as gp-property]
+            [logseq.graph-parser.util.page-ref :as page-ref]
             [medley.core :as medley]
-            [frontend.context.i18n :refer [t]]))
+            [rum.core :as rum]))
 
 ;;; ================================================================
 ;;; Commentary
@@ -770,30 +771,32 @@
                                   "Create a cloze"])
 
 ;; handlers
+(defn add-card-tag-to-block
+  "given a block struct, adds the #card to title and returns
+   a seq of [original-block new-content-string]"
+  [block]
+    (when-let [content (:block/content block)]
+      (let [format (:block/format block)
+            content (-> (property/remove-built-in-properties format content)
+                        (drawer/remove-logbook))
+            [title body] (content/get-title&body content format)]
+        [block (str title " #" card-hash-tag "\n" body)])))
+
 (defn make-block-a-card!
   [block-id]
   (when-let [block (db/entity [:block/uuid block-id])]
-    (when-let [content (:block/content block)]
-      (let [content (-> (property/remove-built-in-properties (:block/format block) content)
-                        (drawer/remove-logbook))]
-        (editor-handler/save-block!
-         (state/get-current-repo)
-         block-id
-         (str (string/trim content) " #" card-hash-tag))))))
+    (let [block-content (add-card-tag-to-block block)
+          new-content (get block-content 1)]
+      (editor-handler/save-block! (state/get-current-repo) block-id new-content))))
 
 (defn batch-make-cards!
   ([] (batch-make-cards! (state/get-selection-block-ids)))
   ([block-ids]
-   (let [block-content-fn (fn [block]
-                            [block (-> (property/remove-built-in-properties (:block/format block) (:block/content block))
-                                       (drawer/remove-logbook)
-                                       string/trim
-                                       (str " #" card-hash-tag))])
-         blocks (->> block-ids
-                     (map #(db/entity [:block/uuid %]))
-                     (remove card-block?)
-                     (map #(db/pull [:block/uuid (:block/uuid %)]))
-                     (map block-content-fn))]
+   (let [valid-blocks (->> block-ids
+                           (map #(db/entity [:block/uuid %]))
+                           (remove card-block?)
+                           (map #(db/pull [:block/uuid (:block/uuid %)])))
+         blocks (map add-card-tag-to-block valid-blocks)]
      (when-not (empty? blocks)
        (editor-handler/save-blocks! blocks)))))
 

+ 25 - 0
src/main/frontend/util/block_content.cljs

@@ -0,0 +1,25 @@
+(ns frontend.util.block-content
+  "utils for text content residing in a block"
+  (:require [clojure.string :as string]
+            [frontend.format.mldoc :as mldoc]
+            [logseq.graph-parser.mldoc :as gp-mldoc]))
+
+
+
+(defn get-ast
+  [content format]
+  (mldoc/->edn content (gp-mldoc/default-config format)))
+
+(defn has-title?
+  [content format]
+  (let [ast (get-ast content format)]
+    (mldoc/block-with-title? (ffirst (map first ast)))))
+
+(defn get-title&body
+  "parses content and returns [title body]
+   returns nil if no title"
+  [content format]
+  (let [lines (string/split-lines content)]
+    (if (has-title? content format)
+      [(first lines) (string/join "\n" (rest lines))]
+      [nil (string/join "\n" lines)])))

+ 5 - 6
src/main/frontend/util/property.cljs

@@ -12,7 +12,8 @@
             [logseq.graph-parser.text :as text]
             [frontend.db :as db]
             [frontend.state :as state]
-            [frontend.util.cursor :as cursor]))
+            [frontend.util.cursor :as cursor]
+            [frontend.util.block-content :as content]))
 
 (defn hidden-properties
   "These are properties hidden from user including built-in ones and ones
@@ -228,8 +229,8 @@
    (insert-property format content key value false))
   ([format content key value front-matter?]
    (when (string? content)
-     (let [ast (mldoc/->edn content (gp-mldoc/default-config format))
-           title? (mldoc/block-with-title? (ffirst (map first ast)))
+     (let [ast (content/get-ast content format)
+           title? (content/has-title? content format)
            has-properties? (or (and title?
                                     (or (mldoc/properties? (second ast))
                                         (mldoc/properties? (second
@@ -239,9 +240,7 @@
                                                              ast)))))
                                (mldoc/properties? (first ast)))
            lines (string/split-lines content)
-           [title body] (if title?
-                          [(first lines) (string/join "\n" (rest lines))]
-                          [nil (string/join "\n" lines)])
+           [title body] (content/get-title&body content format)
            scheduled (filter #(string/starts-with? % "SCHEDULED") lines)
            deadline (filter #(string/starts-with? % "DEADLINE") lines)
            body-without-timestamps (filter