-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
model.cljs
1310 lines (1203 loc) · 42.7 KB
/
model.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns frontend.db.model
"Core db functions."
(:require [frontend.db.conn :as conn]
[frontend.db.utils :as db-utils]
[frontend.db.react :as react]
[datascript.core :as d]
[frontend.date :as date]
[medley.core :as medley]
[frontend.format :as format]
[frontend.state :as state]
[clojure.string :as string]
[clojure.set :as set]
[frontend.utf8 :as utf8]
[frontend.config :as config]
[cljs.reader :as reader]
[cljs-time.core :as t]
[cljs-time.coerce :as tc]
[frontend.util :as util :refer [react] :refer-macros [profile]]
[frontend.db-schema :as db-schema]))
;; TODO: extract to specific models and move data transform logic to the
;; correponding handlers.
(def rules
'[[(parent ?p ?c)
[?p :block/children ?c]]
[(parent ?p ?c)
[?t :block/children ?c]
(parent ?p ?t)]
;; from https://stackoverflow.com/questions/43784258/find-entities-whose-ref-to-many-attribute-contains-all-elements-of-input
;; Quote:
;; You're tackling the general problem of 'dynamic conjunction' in Datomic's Datalog.
;; Write a dynamic Datalog query which uses 2 negations and 1 disjunction or a recursive rule
;; Datalog has no direct way of expressing dynamic conjunction (logical AND / 'for all ...' / set intersection).
;; However, you can achieve it in pure Datalog by combining one disjunction
;; (logical OR / 'exists ...' / set union) and two negations, i.e
;; (For all ?g in ?Gs p(?e,?g)) <=> NOT(Exists ?g in ?Gs, such that NOT(p(?e, ?g)))
;; [(matches-all ?e ?a ?vs)
;; [(first ?vs) ?v0]
;; [?e ?a ?v0]
;; (not-join [?e ?vs]
;; [(identity ?vs) [?v ...]]
;; (not-join [?e ?v]
;; [?e ?a ?v]))]
])
(defn transact-files-db!
([tx-data]
(db-utils/transact! (state/get-current-repo) tx-data))
([repo-url tx-data]
(when-not config/publishing?
(let [tx-data (->> (util/remove-nils tx-data)
(remove nil?)
(map #(dissoc % :file/handle :file/type)))]
(when (seq tx-data)
(when-let [conn (conn/get-files-conn repo-url)]
(d/transact! conn (vec tx-data))))))))
(defn pull-block
[id]
(let [repo (state/get-current-repo)]
(when (conn/get-conn repo)
(->
(react/q repo [:blocks id] {}
'[:find (pull ?block [*])
:in $ ?id
:where
[?block :block/uuid ?id]]
id)
react
ffirst))))
(defn get-tag-pages
[repo tag-name]
(when tag-name
(d/q '[:find ?original-name ?name
:in $ ?tag
:where
[?e :page/name ?tag]
[?page :page/tags ?e]
[?page :page/original-name ?original-name]
[?page :page/name ?name]]
(conn/get-conn repo)
(string/lower-case tag-name))))
(defn get-all-tagged-pages
[repo]
(d/q '[:find ?page-name ?tag
:where
[?page :page/tags ?e]
[?e :page/name ?tag]
[?page :page/name ?page-name]]
(conn/get-conn repo)))
(defn get-pages
[repo]
(->> (d/q
'[:find ?page-original-name
:where
[?page :page/name ?page-name]
[(get-else $ ?page :page/original-name ?page-name) ?page-original-name]]
(conn/get-conn repo))
(map first)))
(defn get-modified-pages
[repo]
(-> (d/q
'[:find ?page-name
:where
[?page :page/original-name ?page-name]]
(conn/get-conn repo))
(db-utils/seq-flatten)))
(defn get-page-alias
[repo page-name]
(when-let [conn (and repo (conn/get-conn repo))]
(some->> (d/q '[:find ?alias
:in $ ?page-name
:where
[?page :page/name ?page-name]
[?page :page/alias ?alias]]
conn
page-name)
db-utils/seq-flatten
distinct)))
(defn get-alias-source-page
[repo alias]
(when-let [conn (and repo (conn/get-conn repo))]
(let [pages (->>
(d/q '[:find (pull ?p [*])
:in $ ?alias
:where
[?a :page/name ?alias]
[?p :page/alias ?a]]
conn
alias)
(db-utils/seq-flatten))]
(when (seq pages)
(some (fn [page]
(let [aliases (->> (get-in page [:page/properties :alias])
(map string/lower-case)
set)]
(when (contains? aliases alias)
page)))
pages)))))
(defn get-files
[repo]
(when-let [conn (conn/get-conn repo)]
(->> (d/q
'[:find ?path
;; ?modified-at
:where
[?file :file/path ?path]
;; [?file :file/last-modified-at ?modified-at]
]
conn)
(seq)
;; (sort-by last)
(reverse))))
(defn get-files-blocks
[repo-url paths]
(let [paths (set paths)
pred (fn [_db e]
(contains? paths e))]
(-> (d/q '[:find ?block
:in $ ?pred
:where
[?file :file/path ?path]
[(?pred $ ?path)]
[?block :block/file ?file]]
(conn/get-conn repo-url) pred)
db-utils/seq-flatten)))
(defn get-file-blocks
[repo-url path]
(-> (d/q '[:find ?block
:in $ ?path
:where
[?file :file/path ?path]
[?block :block/file ?file]]
(conn/get-conn repo-url) path)
db-utils/seq-flatten))
(defn get-file-after-blocks
[repo-url file-id end-pos]
(when end-pos
(let [pred (fn [db meta]
(>= (:start-pos meta) end-pos))]
(-> (d/q '[:find (pull ?block [*])
:in $ ?file-id ?pred
:where
[?block :block/file ?file-id]
[?block :block/meta ?meta]
[(?pred $ ?meta)]]
(conn/get-conn repo-url) file-id pred)
db-utils/seq-flatten
db-utils/sort-by-pos))))
(defn get-file-after-blocks-meta
([repo-url file-id end-pos]
(get-file-after-blocks-meta repo-url file-id end-pos false))
([repo-url file-id end-pos content-level?]
(let [db (conn/get-conn repo-url)
blocks (d/datoms db :avet :block/file file-id)
eids (mapv :e blocks)
ks (if content-level?
'[:block/uuid :block/meta :block/content :block/level]
'[:block/uuid :block/meta])
blocks (db-utils/pull-many repo-url ks eids)]
(->> (filter (fn [{:block/keys [meta]}]
(>= (:start-pos meta) end-pos)) blocks)
db-utils/sort-by-pos))))
(defn get-file-pages
[repo-url path]
(-> (d/q '[:find ?page
:in $ ?path
:where
[?file :file/path ?path]
[?page :page/file ?file]]
(conn/get-conn repo-url) path)
db-utils/seq-flatten))
(defn set-file-last-modified-at!
[repo path last-modified-at]
(when (and repo path last-modified-at)
(when-let [conn (conn/get-conn repo false)]
(d/transact! conn
[{:file/path path
:file/last-modified-at last-modified-at}]))))
(defn get-file-last-modified-at
[repo path]
(when (and repo path)
(when-let [conn (conn/get-conn repo false)]
(-> (d/entity (d/db conn) [:file/path path])
:file/last-modified-at))))
(defn get-file
([path]
(get-file (state/get-current-repo) path))
([repo path]
(when (and repo path)
(->
(react/q repo [:file/content path]
{:files-db? true
:use-cache? true}
'[:find ?content
:in $ ?path
:where
[?file :file/path ?path]
[?file :file/content ?content]]
path)
react
ffirst))))
(defn get-file-contents
[repo]
(when-let [conn (conn/get-files-conn repo)]
(->>
(d/q
'[:find ?path ?content
:where
[?file :file/path ?path]
[?file :file/content ?content]]
@conn)
(into {}))))
(defn get-files-full
[repo]
(when-let [conn (conn/get-files-conn repo)]
(->>
(d/q
'[:find (pull ?file [*])
:where
[?file :file/path]]
@conn)
(flatten))))
(defn get-custom-css
[]
(when-let [repo (state/get-current-repo)]
(get-file (config/get-file-path repo "logseq/custom.css"))))
(defn get-file-no-sub
([path]
(get-file-no-sub (state/get-current-repo) path))
([repo path]
(when (and repo path)
(when-let [conn (conn/get-files-conn repo)]
(:file/content (d/entity (d/db conn) [:file/path path]))))))
(defn get-block-by-uuid
[id]
(db-utils/entity [:block/uuid (if (uuid? id) id (uuid id))]))
(defn query-block-by-uuid
[id]
(db-utils/pull [:block/uuid (if (uuid? id) id (uuid id))]))
(defn get-page-format
[page-name]
(when-let [file (:page/file (db-utils/entity [:page/name page-name]))]
(when-let [path (:file/path (db-utils/entity (:db/id file)))]
(format/get-format path))))
(defn page-alias-set
[repo-url page]
(when-let [page-id (:db/id (db-utils/entity repo-url [:page/name page]))]
(->>
(d/q '[:find ?e
:in $ ?page-name %
:where
[?page :page/name ?page-name]
(alias ?page ?e)]
(conn/get-conn repo-url)
page
'[[(alias ?e2 ?e1)
[?e2 :page/alias ?e1]]
[(alias ?e2 ?e1)
[?e1 :page/alias ?e2]]
[(alias ?e3 ?e1)
[?e1 :page/alias ?e2]
[?e2 :page/alias ?e3]]])
db-utils/seq-flatten
(set)
(set/union #{page-id}))))
(defn get-page-names-by-ids
([ids]
(get-page-names-by-ids (state/get-current-repo) ids))
([repo ids]
(when repo
(->> (db-utils/pull-many repo '[:page/name] ids)
(map :page/name)))))
(defn get-page-ids-by-names
([names]
(get-page-ids-by-names (state/get-current-repo) names))
([repo names]
(when repo
(let [lookup-refs (map (fn [name]
[:page/name (string/lower-case name)]) names)]
(->> (db-utils/pull-many repo '[:db/id] lookup-refs)
(mapv :db/id))))))
(defn get-page-alias-names
[repo page-name]
(let [alias-ids (page-alias-set repo page-name)]
(when (seq alias-ids)
(->> (get-page-names-by-ids repo alias-ids)
distinct
(remove #(= (string/lower-case %) (string/lower-case page-name)))))))
(defn get-block-refs-count
[repo]
(->> (d/q
'[:find ?id2 ?id1
:where
[?id1 :block/ref-blocks ?id2]]
(conn/get-conn repo))
(map first)
(frequencies)))
(defn with-block-refs-count
[repo blocks]
(let [db-ids (map :db/id blocks)
refs (get-block-refs-count repo)]
(map (fn [block]
(assoc block :block/block-refs-count
(get refs (:db/id block))))
blocks)))
(defn page-blocks-transform
[repo-url result]
(let [result (db-utils/seq-flatten result)
sorted (db-utils/sort-by-pos result)]
(->> (db-utils/with-repo repo-url sorted)
(with-block-refs-count repo-url))))
(defn sort-blocks
[blocks]
(let [pages-ids (map (comp :db/id :block/page) blocks)
pages (db-utils/pull-many '[:db/id :page/name :page/original-name :page/journal-day] pages-ids)
pages-map (reduce (fn [acc p] (assoc acc (:db/id p) p)) {} pages)
blocks (map
(fn [block]
(assoc block :block/page
(get pages-map (:db/id (:block/page block)))))
blocks)]
(db-utils/sort-by-pos blocks)))
(defn get-marker-blocks
[repo-url marker]
(let [marker (string/upper-case marker)]
(some->>
(react/q repo-url [:marker/blocks marker]
{:use-cache? true}
'[:find (pull ?h [*])
:in $ ?marker
:where
[?h :block/marker ?m]
[(= ?marker ?m)]]
marker)
react
db-utils/seq-flatten
db-utils/sort-by-pos
(db-utils/with-repo repo-url)
(with-block-refs-count repo-url)
(sort-blocks)
(db-utils/group-by-page))))
(defn get-page-properties
[page]
(when-let [page (db-utils/entity [:page/name page])]
(:page/properties page)))
(defn add-properties!
[page-format properties-content properties]
(let [properties (medley/map-keys name properties)
lines (string/split-lines properties-content)
front-matter-format? (contains? #{:markdown} page-format)
lines (if front-matter-format?
(remove (fn [line]
(contains? #{"---" ""} (string/trim line))) lines)
lines)
property-keys (keys properties)
prefix-f (case page-format
:org (fn [k]
(str "#+" (string/upper-case k) ": "))
:markdown (fn [k]
(str (string/lower-case k) ": "))
identity)
exists? (atom #{})
lines (doall
(mapv (fn [line]
(let [result (filter #(and % (util/starts-with? line (prefix-f %)))
property-keys)]
(if (seq result)
(let [k (first result)]
(swap! exists? conj k)
(str (prefix-f k) (get properties k)))
line))) lines))
lines (concat
lines
(let [not-exists (remove
(fn [[k _]]
(contains? @exists? k))
properties)]
(when (seq not-exists)
(mapv
(fn [[k v]] (str (prefix-f k) v))
not-exists))))]
(util/format
(config/properties-wrapper-pattern page-format)
(string/join "\n" lines))))
(defn get-page-blocks
([page]
(get-page-blocks (state/get-current-repo) page nil))
([repo-url page]
(get-page-blocks repo-url page nil))
([repo-url page {:keys [use-cache? pull-keys]
:or {use-cache? true
pull-keys '[*]}}]
(let [page (string/lower-case page)
page-id (or (:db/id (db-utils/entity repo-url [:page/name page]))
(:db/id (db-utils/entity repo-url [:page/original-name page])))
db (conn/get-conn repo-url)]
(when page-id
(some->
(react/q repo-url [:page/blocks page-id]
{:use-cache? use-cache?
:transform-fn #(page-blocks-transform repo-url %)
:query-fn (fn [db]
(let [datoms (d/datoms db :avet :block/page page-id)
block-eids (mapv :e datoms)]
(db-utils/pull-many repo-url pull-keys block-eids)))}
nil)
react)))))
(defn get-page-blocks-no-cache
([page]
(get-page-blocks-no-cache (state/get-current-repo) page nil))
([repo-url page]
(get-page-blocks-no-cache repo-url page nil))
([repo-url page {:keys [pull-keys]
:or {pull-keys '[*]}}]
(let [page (string/lower-case page)
page-id (or (:db/id (db-utils/entity repo-url [:page/name page]))
(:db/id (db-utils/entity repo-url [:page/original-name page])))
db (conn/get-conn repo-url)]
(when page-id
(let [datoms (d/datoms db :avet :block/page page-id)
block-eids (mapv :e datoms)]
(some->> (db-utils/pull-many repo-url pull-keys block-eids)
(page-blocks-transform repo-url)))))))
(defn get-page-blocks-count
[repo page-id]
(when-let [db (conn/get-conn repo)]
(count (d/datoms db :avet :block/page page-id))))
(defn get-block-parent
[repo block-id]
(when-let [conn (conn/get-conn repo)]
(when-let [block (d/entity conn [:block/uuid block-id])]
(d/entity conn [:block/children [:block/uuid block-id]]))))
;; non recursive query
(defn get-block-parents
[repo block-id depth]
(when-let [conn (conn/get-conn repo)]
(loop [block-id block-id
parents (list)
d 1]
(if (> d depth)
parents
(if-let [parent (get-block-parent repo block-id)]
(recur (:block/uuid parent) (conj parents parent) (inc d))
parents)))))
(defn get-block-page
[repo block-id]
(when-let [block (db-utils/entity repo [:block/uuid block-id])]
(db-utils/entity repo (:db/id (:block/page block)))))
(defn get-block-page-end-pos
[repo page-name]
(or
(when-let [page-id (:db/id (db-utils/entity repo [:page/name (string/lower-case page-name)]))]
(when-let [db (conn/get-conn repo)]
(let [block-eids (->> (d/datoms db :avet :block/page page-id)
(mapv :e))]
(when (seq block-eids)
(let [blocks (db-utils/pull-many repo '[:block/meta] block-eids)]
(-> (last (db-utils/sort-by-pos blocks))
(get-in [:block/meta :end-pos])))))))
;; TODO: need more thoughts
0))
(defn get-blocks-by-priority
[repo priority]
(let [priority (string/capitalize priority)]
(when (conn/get-conn repo)
(->> (react/q repo [:priority/blocks priority] {}
'[:find (pull ?h [*])
:in $ ?priority
:where
[?h :block/priority ?priority]]
priority)
react
db-utils/seq-flatten
sort-blocks
db-utils/group-by-page))))
(defn get-page-properties-content
[page]
(when-let [content (let [blocks (get-page-blocks page)]
(and (:block/pre-block? (first blocks))
(:block/content (first blocks))))]
(let [format (get-page-format page)]
(case format
:org
(->> (string/split-lines content)
(take-while (fn [line]
(or (string/blank? line)
(string/starts-with? line "#+"))))
(string/join "\n"))
:markdown
(let [[m leading-spaces first-dashes] (re-find #"(\s*)(---\n)" content)]
(if m
(let [begin (count leading-spaces)
begin-inner (+ begin (count first-dashes))
second-dashes "\n---\n"
end-inner (string/index-of content second-dashes begin-inner)
end (if end-inner (+ end-inner (count second-dashes)) begin)]
(subs content begin end))
""))
content))))
(defn block-and-children-transform
[result repo-url block-uuid level]
(some->> result
db-utils/seq-flatten
db-utils/sort-by-pos
(take-while (fn [h]
(or
(= (:block/uuid h)
block-uuid)
(> (:block/level h) level))))
(db-utils/with-repo repo-url)
(with-block-refs-count repo-url)))
(defn get-block-children-ids
[repo block-uuid]
(when-let [conn (conn/get-conn repo)]
(let [eid (:db/id (db-utils/entity repo [:block/uuid block-uuid]))]
(->> (d/q
'[:find ?c
:in $ ?p %
:where (parent ?p ?c)]
conn
eid
rules)
(apply concat)))))
(defn get-block-immediate-children
[repo block-uuid]
(when-let [conn (conn/get-conn repo)]
(let [ids (->> (:block/children (db-utils/entity repo [:block/uuid block-uuid]))
(map :db/id))]
(when (seq ids)
(db-utils/pull-many repo '[*] ids)))))
(defn get-block-children
[repo block-uuid]
(when-let [conn (conn/get-conn repo)]
(let [ids (get-block-children-ids repo block-uuid)]
(when (seq ids)
(db-utils/pull-many repo '[*] ids)))))
(defn get-block-and-children
([repo block-uuid]
(get-block-and-children repo block-uuid true))
([repo block-uuid use-cache?]
(let [block (db-utils/entity repo [:block/uuid block-uuid])
page (:db/id (:block/page block))
pos (:start-pos (:block/meta block))
level (:block/level block)
pred (fn []
(let [block (db-utils/entity repo [:block/uuid block-uuid])
pos (:start-pos (:block/meta block))]
(fn [data meta]
(>= (:start-pos meta) pos))))]
(some-> (react/q repo [:block/block block-uuid]
{:use-cache? use-cache?
:transform-fn #(block-and-children-transform % repo block-uuid level)
:inputs-fn (fn []
[page (pred)])}
'[:find (pull ?block [*])
:in $ ?page ?pred
:where
[?block :block/page ?page]
[?block :block/meta ?meta]
[(?pred $ ?meta)]])
react))))
;; TODO: performance
(defn get-block-and-children-no-cache
[repo block-uuid]
(let [block (db-utils/entity repo [:block/uuid block-uuid])
page (:db/id (:block/page block))
pos (:start-pos (:block/meta block))
level (:block/level block)
pred (fn [data meta]
(>= (:start-pos meta) pos))]
(-> (d/q
'[:find (pull ?block [*])
:in $ ?page ?pred
:where
[?block :block/page ?page]
[?block :block/meta ?meta]
[(?pred $ ?meta)]]
(conn/get-conn repo)
page
pred)
(block-and-children-transform repo block-uuid level))))
(defn get-file-page
([file-path]
(get-file-page file-path true))
([file-path original-name?]
(when-let [repo (state/get-current-repo)]
(when-let [conn (conn/get-conn repo)]
(some->
(d/q
(if original-name?
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :page/file ?file]
[?page :page/original-name ?page-name]]
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :page/file ?file]
[?page :page/name ?page-name]])
conn file-path)
db-utils/seq-flatten
first)))))
(defn get-page-file
[page-name]
(some-> (db-utils/entity [:page/name page-name])
:page/file))
(defn get-block-file
[block-id]
(let [page-id (some-> (db-utils/entity [:block/uuid block-id])
:block/page
:db/id)]
(:page/file (db-utils/entity page-id))))
(defn get-file-page-id
[file-path]
(when-let [repo (state/get-current-repo)]
(when-let [conn (conn/get-conn repo)]
(some->
(d/q
'[:find ?page
:in $ ?path
:where
[?file :file/path ?path]
[?page :page/file ?file]]
conn file-path)
db-utils/seq-flatten
first))))
(defn get-page
[page-name]
(if (util/uuid-string? page-name)
(db-utils/entity [:block/uuid (uuid page-name)])
(db-utils/entity [:page/name page-name])))
(defn- heading-block?
[block]
(and
(vector? block)
(= "Heading" (first block))))
(defn get-page-name
[file ast]
;; headline
(let [ast (map first ast)]
(if (string/includes? file "pages/contents.")
"Contents"
(let [first-block (last (first (filter heading-block? ast)))
property-name (when (and (= "Properties" (ffirst ast))
(not (string/blank? (:title (last (first ast))))))
(:title (last (first ast))))
first-block-name (let [title (last (first (:title first-block)))]
(and first-block
(string? title)
title))
file-name (when-let [file-name (last (string/split file #"/"))]
(first (util/split-last "." file-name)))]
(or property-name
(if (= (state/page-name-order) "file")
(or file-name first-block-name)
(or first-block-name file-name)))))))
(defn get-page-original-name
[page-name]
(when page-name
(let [page (db-utils/pull [:page/name (string/lower-case page-name)])]
(or (:page/original-name page)
(:page/name page)))))
(defn get-block-content
[utf8-content block]
(let [meta (:block/meta block)]
(if-let [end-pos (:end-pos meta)]
(utf8/substring utf8-content
(:start-pos meta)
end-pos)
(utf8/substring utf8-content
(:start-pos meta)))))
(defn get-journals-length
[]
(let [today (db-utils/date->int (js/Date.))]
(d/q '[:find (count ?page) .
:in $ ?today
:where
[?page :page/journal? true]
[?page :page/journal-day ?journal-day]
[(<= ?journal-day ?today)]]
(conn/get-conn (state/get-current-repo))
today)))
(defn get-latest-journals
([n]
(get-latest-journals (state/get-current-repo) n))
([repo-url n]
(when (conn/get-conn repo-url)
(let [date (js/Date.)
_ (.setDate date (- (.getDate date) (dec n)))
today (db-utils/date->int (js/Date.))
pages (->>
(react/q repo-url [:journals] {:use-cache? false}
'[:find ?page-name ?journal-day
:in $ ?today
:where
[?page :page/name ?page-name]
[?page :page/journal? true]
[?page :page/journal-day ?journal-day]
[(<= ?journal-day ?today)]]
today)
(react)
(sort-by last)
(reverse)
(map first)
(take n))]
(mapv
(fn [page]
[page
(get-page-format page)])
pages)))))
;; get pages that this page referenced
(defn get-page-referenced-pages
[repo page]
(when (conn/get-conn repo)
(let [pages (page-alias-set repo page)
page-id (:db/id (db-utils/entity [:page/name page]))
ref-pages (->> (react/q repo [:page/ref-pages page-id] {:use-cache? false}
'[:find ?ref-page-name
:in $ ?pages
:where
[?block :block/page ?p]
[(contains? ?pages ?p)]
[?block :block/ref-pages ?ref-page]
[?ref-page :page/name ?ref-page-name]]
pages)
react
db-utils/seq-flatten)]
(mapv (fn [page] [page (get-page-alias repo page)]) ref-pages))))
(defn get-page-linked-refs-refed-pages
[repo page]
(when-let [conn (conn/get-conn repo)]
(->
(d/q
'[:find [?ref-page ...]
:in $ % ?page
:where
[?p :page/name ?page]
[?b :block/path-ref-pages ?p]
[?b :block/ref-pages ?other-p]
[(not= ?p ?other-p)]
[?other-p :page/name ?ref-page]]
conn
rules
page)
(distinct))))
;; Ignore files with empty blocks for now
(defn get-empty-pages
[repo]
(when-let [conn (conn/get-conn repo)]
(->
(d/q
'[:find ?page
:where
[?p :page/name ?page]
(not [?p :page/file])]
conn)
(db-utils/seq-flatten)
(distinct))))
(defn page-empty?
[repo page]
(nil? (:page/file (db-utils/entity repo [:page/name (string/lower-case page)]))))
(defn get-pages-relation
[repo with-journal?]
(when-let [conn (conn/get-conn repo)]
(let [q (if with-journal?
'[:find ?page ?ref-page-name
:where
[?p :page/name ?page]
[?block :block/page ?p]
[?block :block/ref-pages ?ref-page]
[?ref-page :page/name ?ref-page-name]]
'[:find ?page ?ref-page-name
:where
[?p :page/journal? false]
[?p :page/name ?page]
[?block :block/page ?p]
[?block :block/ref-pages ?ref-page]
[?ref-page :page/name ?ref-page-name]])]
(->>
(d/q q conn)
(map (fn [[page ref-page-name]]
[page ref-page-name]))))))
;; get pages who mentioned this page
(defn get-pages-that-mentioned-page
[repo page]
(when (conn/get-conn repo)
(let [page-id (:db/id (db-utils/entity [:page/name page]))
pages (page-alias-set repo page)
mentioned-pages (->> (react/q repo [:page/mentioned-pages page-id] {:use-cache? false}
'[:find ?mentioned-page-name
:in $ ?pages ?page-name
:where
[?block :block/ref-pages ?p]
[(contains? ?pages ?p)]
[?block :block/page ?mentioned-page]
[?mentioned-page :page/name ?mentioned-page-name]]
pages
page)
react
db-utils/seq-flatten)]
(mapv (fn [page] [page (get-page-alias repo page)]) mentioned-pages))))
(defn- remove-children!
[blocks]
(let [childrens (->> (mapcat :block/children blocks)
(map :db/id)
(set))]
(if (seq childrens)
(remove (fn [block] (contains? childrens (:db/id block))) blocks)
blocks)))
(defonce *blocks (atom nil))
(defn with-children-refs
[repo blocks]
(reset! *blocks blocks)
(when-let [conn (conn/get-conn repo)]
(when (seq blocks)
(let [block-ids (set (map :db/id blocks))
refs (d/q
'[:find ?p ?ref
:in $ % ?block-ids
:where
(parent ?p ?b)
[(contains? ?block-ids ?p)]
[?b :block/ref-pages ?ref]]
conn
rules
block-ids)
refs (->> (group-by first refs)
(medley/map-vals #(set (map (fn [[_ id]] {:db/id id}) %))))]
(map (fn [block] (assoc block :block/children-refs
(get refs (:db/id block)))) blocks)))))
(defn get-page-referenced-blocks
([page]
(get-page-referenced-blocks (state/get-current-repo) page))
([repo page]
(when repo
(when (conn/get-conn repo)
(let [page-id (:db/id (db-utils/entity [:page/name page]))
pages (page-alias-set repo page)
aliases (set/difference pages #{page-id})
query-result (if (seq aliases)
(let [rules '[[(find-blocks ?block ?ref-page ?pages ?alias ?aliases)
[?block :block/page ?alias]
[(contains? ?aliases ?alias)]]
[(find-blocks ?block ?ref-page ?pages ?alias ?aliases)
[?block :block/ref-pages ?ref-page]
[(contains? ?pages ?ref-page)]]]]
(react/q repo [:page/refed-blocks page-id] {}
'[:find (pull ?block [*])
:in $ % ?pages ?aliases
:where
(find-blocks ?block ?ref-page ?pages ?alias ?aliases)]
rules
pages
aliases))
(react/q repo [:page/refed-blocks page-id] {}
'[:find (pull ?block [*])
:in $ ?pages
:where
[?block :block/ref-pages ?ref-page]
[(contains? ?pages ?ref-page)]]
pages))
result (->> query-result
react
db-utils/seq-flatten
(remove (fn [block]
(= page-id (:db/id (:block/page block)))))
(remove-children!)
(with-children-refs repo)
sort-blocks
db-utils/group-by-page
(map (fn [[k blocks]]
(let [k (if (contains? aliases (:db/id k))
(assoc k :page/alias? true)
k)]
[k blocks]))))]
result)))))
(defn get-date-scheduled-or-deadlines
[journal-title]
(when-let [date (date/journal-title->int journal-title)]
(when-let [repo (state/get-current-repo)]
(when-let [conn (conn/get-conn repo)]
(->> (react/q repo [:custom :scheduled-deadline journal-title] {}