export.cljs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (ns frontend.worker.export
  2. "Export data"
  3. (:require [datascript.core :as d]
  4. [frontend.common.file.core :as common-file]
  5. [logseq.db :as ldb]
  6. [logseq.graph-parser.property :as gp-property]
  7. [logseq.outliner.tree :as otree]
  8. [cljs-bean.core :as bean]
  9. [logseq.db.sqlite.util :as sqlite-util]
  10. [clojure.string :as string]))
  11. (defn- safe-keywordize
  12. [block]
  13. (update block :block/properties
  14. (fn [properties]
  15. (when (seq properties)
  16. (->> (filter (fn [[k _v]]
  17. (gp-property/valid-property-name? (str k))) properties)
  18. (into {}))))))
  19. (defn get-all-pages
  20. "Get all pages and their children blocks."
  21. [repo db]
  22. (->> (d/q '[:find (pull ?b [*])
  23. :in $
  24. :where
  25. [?b :block/title]
  26. [?b :block/name]] db)
  27. (map (fn [[page]]
  28. (let [whiteboard? (ldb/whiteboard? page)
  29. blocks (ldb/get-page-blocks db (:db/id page))
  30. blocks' (if whiteboard?
  31. blocks
  32. (map (fn [b]
  33. (let [b' (if (seq (:block/properties b))
  34. (update b :block/title
  35. (fn [content]
  36. (gp-property/remove-properties (:block/format b) content)))
  37. b)]
  38. (safe-keywordize b'))) blocks))
  39. children (if whiteboard?
  40. blocks'
  41. (otree/blocks->vec-tree repo db blocks' (:db/id page)))
  42. page' (safe-keywordize page)]
  43. (assoc page' :block/children children))))))
  44. (defn get-all-page->content
  45. [repo db]
  46. (->> (d/datoms db :avet :block/name)
  47. (map (fn [d]
  48. (let [e (d/entity db (:e d))]
  49. [(:block/title e)
  50. (common-file/block->content repo db (:block/uuid e) {} {})])))))
  51. (defn get-debug-datoms
  52. [^Object db]
  53. (some->> (.exec db #js {:sql "select content from kvs"
  54. :rowMode "array"})
  55. bean/->clj
  56. (mapcat (fn [result]
  57. (let [result (sqlite-util/transit-read (first result))]
  58. (when (map? result)
  59. (:keys result)))))
  60. (group-by first)
  61. (mapcat (fn [[_id col]]
  62. (let [type (some (fn [[_e a v _t]]
  63. (when (= a :block/type)
  64. v)) col)
  65. ident (some (fn [[_e a v _t]]
  66. (when (= a :db/ident)
  67. v)) col)]
  68. (map
  69. (fn [[e a v t]]
  70. (cond
  71. (and (contains? #{:block/title :block/name} a)
  72. (or
  73. ;; normal page or block
  74. (not (contains? #{"class" "property" "journal" "closed value" "hidden"} type))
  75. ;; class/property created by user
  76. (and ident
  77. (contains? #{"class" "property"} type)
  78. (not (string/starts-with? (namespace ident) "logseq")))))
  79. [e a (str "debug " e) t]
  80. (= a :block/uuid)
  81. [e a (str v) t]
  82. :else
  83. [e a v t]))
  84. col))))
  85. (distinct)
  86. (sort-by first)))