/
text.cljs
365 lines (308 loc) · 10.7 KB
/
text.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
(ns ^:nbb-compatible logseq.graph-parser.text
(:require ["path" :as path]
[goog.string :as gstring]
[clojure.string :as string]
[clojure.set :as set]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.util :as gp-util]))
(def page-ref-re-0 #"\[\[(.*)\]\]")
(def org-page-ref-re #"\[\[(file:.*)\]\[.+?\]\]")
(def markdown-page-ref-re #"\[(.*)\]\(file:.*\)")
(defn get-file-basename
[path]
(when-not (string/blank? path)
;; Same as util/node-path.name
(.-name (path/parse (string/replace path "+" "/")))))
(defn get-page-name
[s]
(and (string? s)
(or (when-let [[_ label _path] (re-matches markdown-page-ref-re s)]
(string/trim label))
(when-let [[_ path _label] (re-matches org-page-ref-re s)]
(some-> (get-file-basename path)
(string/replace "." "/")))
(-> (re-matches page-ref-re-0 s)
second))))
(defn page-ref?
[s]
(and
(string? s)
(string/starts-with? s "[[")
(string/ends-with? s "]]")))
(def block-ref-re #"\(\(([a-zA-z0-9]{8}-[a-zA-z0-9]{4}-[a-zA-z0-9]{4}-[a-zA-z0-9]{4}-[a-zA-z0-9]{12})\)\)")
(defn get-block-ref
[s]
(and (string? s)
(second (re-matches block-ref-re s))))
(defn block-ref?
[s]
(boolean (get-block-ref s)))
(defonce page-ref-re #"\[\[(.*?)\]\]")
(defonce page-ref-re-2 #"(\[\[.*?\]\])")
(def page-ref-re-without-nested #"\[\[([^\[\]]+)\]\]")
(defonce between-re #"\(between ([^\)]+)\)")
(defn page-ref-un-brackets!
[s]
(or (get-page-name s) s))
(defn block-ref-un-brackets!
[s]
(when (string? s)
(if (block-ref? s)
(subs s 2 (- (count s) 2))
s)))
;; E.g "Foo Bar"
(defn sep-by-comma
[s]
(when s
(some->>
(string/split s #"[\,|,]{1}")
(remove string/blank?)
(map string/trim))))
(defn sep-by-hashtag
[s]
(when s
(some->>
(string/split s #"#")
(remove string/blank?)
(map string/trim))))
(defn- not-matched-nested-pages
[s]
(and (string? s)
(> (count (re-seq #"\[\[" s))
(count (re-seq #"\]\]" s)))))
(defn- ref-matched?
[s]
(let [x (re-seq #"\[\[" s)
y (re-seq #"\]\]" s)]
(and (> (count x) 0) (= (count x) (count y)))))
(defn get-nested-page-name
[page-name]
(when-let [first-match (re-find page-ref-re-without-nested page-name)]
(second first-match)))
(defn- concat-nested-pages
[coll]
(first
(reduce (fn [[acc not-matched-s] s]
(cond
(and not-matched-s (= s "]]"))
(let [s' (str not-matched-s s)]
(if (ref-matched? s')
[(conj acc s') nil]
[acc s']))
not-matched-s
[acc (str not-matched-s s)]
(not-matched-nested-pages s)
[acc s]
:else
[(conj acc s) not-matched-s])) [[] nil] coll)))
(defn- sep-by-quotes
[s]
(string/split s #"(\"[^\"]*\")"))
(def markdown-link #"\[([^\[]+)\](\(.*\))")
(defn split-page-refs-without-brackets
([s]
(split-page-refs-without-brackets s {}))
([s {:keys [un-brackets?]
:or {un-brackets? true}}]
(cond
(and (string? s) (gp-util/wrapped-by-quotes? s))
(gp-util/unquote-string s)
(and (string? s) (re-find markdown-link s))
s
(and (string? s)
;; Either a page ref, a tag or a comma separated collection
(or (gp-util/safe-re-find page-ref-re s)
(gp-util/safe-re-find #"[\,|,|#|\"]+" s)))
(let [result (->> (sep-by-quotes s)
(mapcat
(fn [s]
(when-not (gp-util/wrapped-by-quotes? (string/trim s))
(string/split s page-ref-re-2))))
(mapcat (fn [s]
(cond
(gp-util/wrapped-by-quotes? s)
nil
(string/includes? (string/trimr s) "]],")
(let [idx (string/index-of s "]],")]
[(subs s 0 idx)
"]]"
(subs s (+ idx 3))])
:else
[s])))
(remove #(= % ""))
(mapcat (fn [s] (if (string/ends-with? s "]]")
[(subs s 0 (- (count s) 2))
"]]"]
[s])))
concat-nested-pages
(remove string/blank?)
(mapcat (fn [s]
(cond
(gp-util/wrapped-by-quotes? s)
nil
(page-ref? s)
[(if un-brackets? (page-ref-un-brackets! s) s)]
:else
(->> (sep-by-comma s)
(mapcat sep-by-hashtag)))))
(distinct))]
(if (or (coll? result)
(and (string? result)
(string/starts-with? result "#")))
(let [result (if coll? result [result])
result (map (fn [s] (string/replace s #"^#+" "")) result)]
(set result))
(first result)))
:else
s)))
(defn- remove-level-space-aux!
[text pattern space? trim-left?]
(let [pattern (gstring/format
(if space?
"^[%s]+\\s+"
"^[%s]+\\s?")
pattern)
text (if trim-left? (string/triml text) text)]
(string/replace-first text (re-pattern pattern) "")))
(defn remove-level-spaces
([text format block-pattern]
(remove-level-spaces text format block-pattern false true))
([text format block-pattern space?]
(remove-level-spaces text format block-pattern space? true))
([text format block-pattern space? trim-left?]
(when format
(cond
(string/blank? text)
""
(and (= "markdown" (name format))
(string/starts-with? text "---"))
text
:else
(remove-level-space-aux! text block-pattern space? trim-left?)))))
(defn build-data-value
[col]
(let [items (map (fn [item] (str "\"" item "\"")) col)]
(gstring/format "[%s]"
(string/join ", " items))))
(defn media-link?
[media-formats s]
(some (fn [fmt] (gp-util/safe-re-find (re-pattern (str "(?i)\\." fmt "(?:\\?([^#]*))?(?:#(.*))?$")) s)) media-formats))
(defn namespace-page?
[p]
(and (string? p)
(string/includes? p "/")
(not (string/starts-with? p "../"))
(not (string/starts-with? p "./"))
(not (gp-util/url? p))))
(defn add-timestamp
[content key value]
(let [new-line (str (string/upper-case key) ": " value)
lines (string/split-lines content)
new-lines (map (fn [line]
(string/trim
(if (string/starts-with? (string/lower-case line) key)
new-line
line)))
lines)
new-lines (if (not= (map string/trim lines) new-lines)
new-lines
(cons (first new-lines) ;; title
(cons
new-line
(rest new-lines))))]
(string/join "\n" new-lines)))
(defn remove-timestamp
[content key]
(let [lines (string/split-lines content)
new-lines (filter (fn [line]
(not (string/starts-with? (string/lower-case line) key)))
lines)]
(string/join "\n" new-lines)))
(defn get-current-line-by-pos
[s pos]
(let [lines (string/split-lines s)
result (reduce (fn [acc line]
(let [new-pos (+ acc (count line))]
(if (>= new-pos pos)
(reduced line)
(inc new-pos)))) 0 lines)]
(when (string? result)
result)))
(defn get-string-all-indexes
"Get all indexes of `value` in the string `s`."
[s value]
(loop [acc []
i 0]
(if-let [i (string/index-of s value i)]
(recur (conj acc i) (+ i (count value)))
acc)))
(defn surround-by?
"`pos` must be surrounded by `before` and `and` in string `value`, e.g. ((|))"
[value pos before end]
(let [start-pos (if (= :start before) 0 (- pos (count before)))
end-pos (if (= :end end) (count value) (+ pos (count end)))]
(when (>= (count value) end-pos)
(= (cond
(and (= :end end) (= :start before))
""
(= :end end)
before
(= :start before)
end
:else
(str before end))
(subs value start-pos end-pos)))))
(defn wrapped-by?
"`pos` must be wrapped by `before` and `and` in string `value`, e.g. ((a|b))"
[value pos before end]
(let [before-matches (->> (get-string-all-indexes value before)
(map (fn [i] [i :before])))
end-matches (->> (get-string-all-indexes value end)
(map (fn [i] [i :end])))
indexes (sort-by first (concat before-matches end-matches [[pos :between]]))
ks (map second indexes)
q [:before :between :end]]
(true?
(reduce (fn [acc k]
(if (= q (conj acc k))
(reduced true)
(vec (take-last 2 (conj acc k)))))
[]
ks))))
(defn get-graph-name-from-path
[path]
(when (string? path)
(let [parts (->> (string/split path #"/")
(take-last 2))]
(-> (if (not= (first parts) "0")
(string/join "/" parts)
(last parts))
js/decodeURI))))
(defonce non-parsing-properties
(atom #{"background-color" "background_color"}))
(defn parse-property
([k v config-state]
(parse-property :markdown k v config-state))
([format k v config-state]
(let [k (name k)
v (if (or (symbol? v) (keyword? v)) (name v) (str v))
v (string/trim v)]
(cond
(contains? (set/union
#{"title" "filters"}
(get config-state :ignored-page-references-keywords)) k)
v
(= v "true")
true
(= v "false")
false
(and (not= k "alias") (gp-util/safe-re-find #"^\d+$" v))
(gp-util/safe-parse-int v)
(gp-util/wrapped-by-quotes? v) ; wrapped in ""
v
(contains? @non-parsing-properties (string/lower-case k))
v
(gp-mldoc/link? format v)
v
:else
(split-page-refs-without-brackets v)))))