enhance: replace inline tags with node references

This commit is contained in:
Tienson Qin
2024-08-09 10:50:17 +08:00
parent a2df9564c6
commit b62715ebd8
3 changed files with 37 additions and 38 deletions

View File

@@ -82,14 +82,9 @@
[content refs]
(reduce
(fn [content ref]
(string/replace content
(str page-ref/left-brackets
(:block/title ref)
page-ref/right-brackets)
(str page-ref/left-brackets
page-ref-special-chars
(:block/uuid ref)
page-ref/right-brackets)))
(-> content
(string/replace (str page-ref/left-brackets (:block/title ref) page-ref/right-brackets)
(str page-ref/left-brackets page-ref-special-chars (:block/uuid ref) page-ref/right-brackets))))
content
refs))
@@ -105,31 +100,34 @@
(defn content-without-tags
"Remove tags from content"
[content tags]
[content refs]
(->
(reduce
(fn [content tag]
(fn [content ref]
(-> content
(string/replace (str "#" tag " ") "")
(string/replace (str "#" tag) "")
(string/replace (str "#" page-ref/left-brackets tag page-ref/right-brackets " ") "")
(string/replace (str "#" page-ref/left-brackets tag page-ref/right-brackets) "")))
(string/replace (str "#" page-ref-special-chars (:block/uuid ref))
(block-id->special-id-ref (:block/uuid ref)))
(string/replace (str "#" (block-id->special-id-ref (:block/uuid ref)))
(block-id->special-id-ref (:block/uuid ref)))))
content
tags)
refs)
(string/trim)))
(defn replace-tags-with-page-refs
"Replace tags in content with page-ref ids. Ignore case because tags in
content can have any case and still have a valid ref"
[content tags]
(reduce
(fn [content tag]
(common-util/replace-ignore-case
content
(str "#" (:block/title tag))
(str page-ref/left-brackets
page-ref-special-chars
(:block/uuid tag)
page-ref/right-brackets)))
content
(sort-by :block/title > tags)))
(->>
(reduce
(fn [content tag]
(let [id-ref (block-id->special-id-ref (:block/uuid tag))]
(-> content
;; #[[favorite book]]
(common-util/replace-ignore-case
(str "#" page-ref/left-brackets (:block/title tag) page-ref/right-brackets)
id-ref)
;; #book
(common-util/replace-ignore-case (str "#" (:block/title tag)) id-ref))))
content
(sort-by :block/title > tags))
(string/trim)))

View File

@@ -103,16 +103,7 @@
(replace-page-refs-with-ids))]
(-> result
;; Remove tags from content
(assoc :block/title
(db-content/content-without-tags
(:block/title result)
(->>
(map
(fn [tag]
(when (:block/uuid tag)
(str db-content/page-ref-special-chars (:block/uuid tag))))
(:block/tags (db/entity (:db/id block))))
(remove nil?)))))))
(assoc :block/title (db-content/content-without-tags (:block/title result) (:block/refs result))))))
(defn save-file!
"This fn is the db version of file-handler/alter-file"

View File

@@ -18,7 +18,8 @@
[frontend.format.mldoc :as mldoc]
[lambdaisland.glogi :as log]
[promesa.core :as p]
[frontend.config :as config]))
[frontend.config :as config]
[logseq.db.frontend.content :as db-content]))
(defn- paste-text-parseable
[format text]
@@ -28,7 +29,16 @@
(mldoc/->edn text format)
text format
{:page-name (:block/name (db/entity page-id))})
blocks' (gp-block/with-parent-and-order page-id blocks)]
db-based? (config/db-based-graph? (state/get-current-repo))
blocks' (cond->> (gp-block/with-parent-and-order page-id blocks)
db-based?
(map (fn [block]
(let [refs (:block/refs block)]
(-> block
(dissoc :block/tags)
(update :block/title (fn [title]
(-> (db-content/replace-tags-with-page-refs title refs)
(db-content/page-ref->special-id-ref refs)))))))))]
(editor-handler/paste-blocks blocks' {:keep-uuid? true}))))
(defn- paste-segmented-text