/
extract.cljs
190 lines (181 loc) · 9.36 KB
/
extract.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
(ns frontend.handler.extract
"Extract helper."
(:require [clojure.set :as set]
[clojure.string :as string]
[clojure.walk :as walk]
[frontend.config :as config]
[frontend.db :as db]
[frontend.format :as format]
[frontend.format.block :as block]
[frontend.format.mldoc :as mldoc]
[frontend.state :as state]
[logseq.graph-parser.text :as text]
[frontend.util :as util]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.mldoc :as gp-mldoc]
[frontend.util.property :as property]
[lambdaisland.glogi :as log]))
(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 block/heading-block? ast)))
property-name (when (and (contains? #{"Properties" "Property_Drawer"} (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 #"/"))]
(let [result (first (gp-util/split-last "." file-name))]
(if (config/mldoc-support? (string/lower-case (util/get-file-ext file)))
(util/url-decode (string/replace result "." "/"))
result)))]
(or property-name
(if (= (state/page-name-order) "heading")
(or first-block-name file-name)
(or file-name first-block-name)))))))
;; TODO: performance improvement
(defn- extract-pages-and-blocks
#_:clj-kondo/ignore
[repo-url format ast properties file content]
(try
(let [page (get-page-name file ast)
[_original-page-name page-name _journal-day] (block/convert-page-if-journal page)
blocks (->> (block/extract-blocks ast content false format)
(block/with-parent-and-left {:block/name page-name}))
ref-pages (atom #{})
ref-tags (atom #{})
blocks (map (fn [block]
(let [block-ref-pages (seq (:block/refs block))
page-lookup-ref [:block/name page-name]
block-path-ref-pages (->> (cons page-lookup-ref (seq (:block/path-refs block)))
(remove nil?))]
(when block-ref-pages
(swap! ref-pages set/union (set block-ref-pages)))
(-> block
(dissoc :ref-pages)
(assoc :block/format format
:block/page [:block/name page-name]
:block/refs block-ref-pages
:block/path-refs block-path-ref-pages))))
blocks)
page-entity (let [alias (:alias properties)
alias (if (string? alias) [alias] alias)
aliases (and alias
(seq (remove #(or (= page-name (util/page-name-sanity-lc %))
(string/blank? %)) ;; disable blank alias
alias)))
aliases (->>
(map
(fn [alias]
(let [page-name (util/page-name-sanity-lc alias)
aliases (distinct
(conj
(remove #{alias} aliases)
page))
aliases (when (seq aliases)
(map
(fn [alias]
{:block/name (util/page-name-sanity-lc alias)})
aliases))]
(if (seq aliases)
{:block/name page-name
:block/alias aliases}
{:block/name page-name})))
aliases)
(remove nil?))]
(cond->
(gp-util/remove-nils
(assoc
(block/page-name->map page false)
:block/file {:file/path (gp-util/path-normalize file)}))
(seq properties)
(assoc :block/properties properties)
(seq aliases)
(assoc :block/alias aliases)
(:tags properties)
(assoc :block/tags (let [tags (:tags properties)
tags (if (string? tags) [tags] tags)
tags (remove string/blank? tags)]
(swap! ref-tags set/union (set tags))
(map (fn [tag] {:block/name (util/page-name-sanity-lc tag)
:block/original-name tag})
tags)))))
namespace-pages (let [page (:block/original-name page-entity)]
(when (text/namespace-page? page)
(->> (util/split-namespace-pages page)
(map (fn [page]
(-> (block/page-name->map page true)
(assoc :block/format format)))))))
pages (->> (concat
[page-entity]
@ref-pages
(map
(fn [page]
{:block/original-name page
:block/name (util/page-name-sanity-lc page)})
@ref-tags)
namespace-pages)
;; remove block references
(remove vector?)
(remove nil?))
pages (util/distinct-by :block/name pages)
pages (remove nil? pages)
pages (map (fn [page] (assoc page :block/uuid (db/new-block-id))) pages)
blocks (->> (remove nil? blocks)
(map (fn [b] (dissoc b :block/title :block/body :block/level :block/children :block/meta :block/anchor))))]
[pages blocks])
(catch js/Error e
(log/error :exception e))))
(defn extract-blocks-pages
[repo-url file content]
(if (string/blank? content)
[]
(let [format (format/get-format file)
_ (println "Parsing start: " file)
ast (mldoc/->edn content (gp-mldoc/default-config format
;; {:parse_outline_only? true}
))]
(println "Parsing finished : " file)
(let [first-block (ffirst ast)
properties (let [properties (and (property/properties-ast? first-block)
(->> (last first-block)
(map (fn [[x y]]
[x (if (string? y)
(text/parse-property format x y (state/get-config))
y)]))
(into {})
(walk/keywordize-keys)))]
(when (and properties (seq properties))
(if (:filters properties)
(update properties :filters
(fn [v]
(string/replace (or v "") "\\" "")))
properties)))]
(extract-pages-and-blocks
repo-url
format ast properties
file content)))))
(defn with-block-uuid
[pages]
(->> (util/distinct-by :block/name pages)
(map (fn [page]
(if (:block/uuid page)
page
(assoc page :block/uuid (db/new-block-id)))))))
(defn with-ref-pages
[pages blocks]
(let [ref-pages (->> (mapcat :block/refs blocks)
(filter :block/name))]
(->> (concat pages ref-pages)
(group-by :block/name)
vals
(map (partial apply merge))
(with-block-uuid))))
(defn extract-all-block-refs
[content]
(map second (re-seq #"\(\(([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\)\)" content)))