|
@@ -1,87 +1,55 @@
|
|
|
(ns logseq.db.frontend.content
|
|
|
- "Fns to handle block content e.g. special ids"
|
|
|
+ "Fns to handle block content e.g. internal ids"
|
|
|
(:require [clojure.string :as string]
|
|
|
[logseq.common.util.page-ref :as page-ref]
|
|
|
[datascript.core :as d]
|
|
|
[logseq.common.util :as common-util]
|
|
|
[logseq.db.frontend.entity-util :as entity-util]))
|
|
|
|
|
|
-(defonce page-ref-special-chars "~^")
|
|
|
+#_(defonce page-ref-special-chars "~^")
|
|
|
|
|
|
-(defonce special-id-ref-pattern
|
|
|
- (re-pattern
|
|
|
- (str
|
|
|
- "(?i)"
|
|
|
- "~\\^"
|
|
|
- "("
|
|
|
- common-util/uuid-pattern
|
|
|
- ")")))
|
|
|
+(defonce id-ref-pattern
|
|
|
+ #"\[\[([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\]\]")
|
|
|
|
|
|
-(defn block-id->special-id-ref
|
|
|
- [id]
|
|
|
- (str page-ref/left-brackets
|
|
|
- page-ref-special-chars
|
|
|
- id
|
|
|
- page-ref/right-brackets))
|
|
|
-
|
|
|
-(defn special-id-ref->page
|
|
|
- "Convert special id ref backs to page name using refs."
|
|
|
+(defn content-id-ref->page
|
|
|
+ "Convert id ref backs to page name using refs."
|
|
|
[content refs]
|
|
|
(reduce
|
|
|
(fn [content ref]
|
|
|
(if (:block/title ref)
|
|
|
- (-> content
|
|
|
- (string/replace (block-id->special-id-ref (:block/uuid ref))
|
|
|
- (:block/title ref))
|
|
|
- (string/replace
|
|
|
- (str "#" page-ref-special-chars
|
|
|
- (:block/uuid ref))
|
|
|
- (str "#" (:block/title ref))))
|
|
|
+ (string/replace content (page-ref/->page-ref (:block/uuid ref)) (:block/title ref))
|
|
|
content))
|
|
|
content
|
|
|
refs))
|
|
|
|
|
|
-(defn special-id-ref->page-ref
|
|
|
- "Convert special id ref backs to page name refs using refs."
|
|
|
+(defn id-ref->title-ref
|
|
|
+ "Convert id ref backs to page name refs using refs."
|
|
|
[content* refs]
|
|
|
(let [content (str content*)]
|
|
|
- (if (or (string/includes? content (str page-ref/left-brackets page-ref-special-chars))
|
|
|
- (string/includes? content (str "#" page-ref-special-chars)))
|
|
|
+ (if (re-find id-ref-pattern content)
|
|
|
(reduce
|
|
|
(fn [content ref]
|
|
|
(if (:block/title ref)
|
|
|
- (-> content
|
|
|
- ;; Replace page refs
|
|
|
- (string/replace
|
|
|
- (str page-ref/left-brackets
|
|
|
- page-ref-special-chars
|
|
|
- (:block/uuid ref)
|
|
|
- page-ref/right-brackets)
|
|
|
- (page-ref/->page-ref (:block/title ref)))
|
|
|
- ;; Replace tags
|
|
|
- (string/replace
|
|
|
- (str "#" page-ref-special-chars
|
|
|
- (:block/uuid ref))
|
|
|
- (str "#" (:block/title ref))))
|
|
|
-
|
|
|
+ (string/replace content
|
|
|
+ (page-ref/->page-ref (:block/uuid ref))
|
|
|
+ (page-ref/->page-ref (:block/title ref)))
|
|
|
content))
|
|
|
content
|
|
|
refs)
|
|
|
content)))
|
|
|
|
|
|
-(defn get-matched-special-ids
|
|
|
+(defn get-matched-ids
|
|
|
[content]
|
|
|
- (->> (re-seq special-id-ref-pattern content)
|
|
|
+ (->> (re-seq id-ref-pattern content)
|
|
|
(distinct)
|
|
|
(map second)
|
|
|
(map uuid)))
|
|
|
|
|
|
(defn- replace-tag-ref
|
|
|
[content page-name id]
|
|
|
- (let [id' (str page-ref-special-chars id)
|
|
|
- [page wrapped-id] (if (string/includes? page-name " ")
|
|
|
- (map page-ref/->page-ref [page-name id'])
|
|
|
- [page-name id'])
|
|
|
+ (let [[page wrapped-id] (if (string/includes? page-name " ")
|
|
|
+ (map page-ref/->page-ref [page-name id])
|
|
|
+ [page-name id])
|
|
|
page-name (common-util/format "#%s" page)
|
|
|
r (common-util/format "#%s" wrapped-id)]
|
|
|
;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631
|
|
@@ -95,8 +63,7 @@
|
|
|
|
|
|
(defn- replace-page-ref
|
|
|
[content page-name id]
|
|
|
- (let [id' (str page-ref-special-chars id)
|
|
|
- [page wrapped-id] (map page-ref/->page-ref [page-name id'])]
|
|
|
+ (let [[page wrapped-id] (map page-ref/->page-ref [page-name id])]
|
|
|
(common-util/replace-ignore-case content page wrapped-id)))
|
|
|
|
|
|
(defn- replace-page-ref-with-id
|
|
@@ -109,8 +76,8 @@
|
|
|
(replace-tag-ref content' page-name id)
|
|
|
content')))
|
|
|
|
|
|
-(defn refs->special-id-ref
|
|
|
- "Convert ref to special id refs e.g. `[[page name]] -> [[~^...]]."
|
|
|
+(defn title-ref->id-ref
|
|
|
+ "Convert ref to id refs e.g. `[[page name]] -> [[uuid]]."
|
|
|
[title refs & {:keys [replace-tag?]
|
|
|
:or {replace-tag? true}}]
|
|
|
(assert (string? title))
|
|
@@ -134,7 +101,7 @@
|
|
|
(if (entity-util/db-based-graph? db)
|
|
|
(if-let [content (:block/title item)]
|
|
|
(let [refs (:block/refs (d/entity db eid))]
|
|
|
- (assoc item :block/title (special-id-ref->page-ref content refs)))
|
|
|
+ (assoc item :block/title (id-ref->title-ref content refs)))
|
|
|
item)
|
|
|
item))
|
|
|
|
|
@@ -145,13 +112,13 @@
|
|
|
(->>
|
|
|
(reduce
|
|
|
(fn [content tag]
|
|
|
- (let [id-ref (block-id->special-id-ref (:block/uuid tag))]
|
|
|
+ (let [id-ref (page-ref/->page-ref (:block/uuid tag))]
|
|
|
(-> content
|
|
|
- ;; #[[favorite book]]
|
|
|
+ ;; #[[favorite book]]
|
|
|
(common-util/replace-ignore-case
|
|
|
(str "#" page-ref/left-brackets (:block/title tag) page-ref/right-brackets)
|
|
|
id-ref)
|
|
|
- ;; #book
|
|
|
+ ;; #book
|
|
|
(common-util/replace-ignore-case (str "#" (:block/title tag)) id-ref))))
|
|
|
content
|
|
|
(sort-by :block/title > tags))
|