Add in more deps and fixes - still not to parse parity

- Added cljs-time and linked-map
- data/diff datoms
- workaround for utils.js
- implicit db workaround
This commit is contained in:
Gabriel Horner
2022-04-18 17:46:04 -04:00
parent 7d16ceb6ca
commit a2bb800825
7 changed files with 93 additions and 29 deletions

View File

@@ -4,17 +4,21 @@
[clojure.walk :as walk]
; [cljs.core.match :as match]
; [frontend.config :as config]
; [frontend.date :as date]
; [frontend.format :as format]
; [frontend.state :as state]
[logseq.graph-parser.text :as text]
[frontend.utf8 :as utf8]
[logseq.graph-parser.date-util :as date-util]
[logseq.graph-parser.util :as util]
[logseq.graph-parser.property :as property]
[logseq.graph-parser.mldoc :as mldoc]
[logseq.graph-parser.time-util :as time-util]
; [lambdaisland.glogi :as log]
[datascript.core :as d]))
;; TODO: Remove global conn when we have better control
(def conn (atom nil))
(defn heading-block?
[block]
(and
@@ -237,12 +241,11 @@
[original-page-name]
(when original-page-name
(let [page-name (util/page-name-sanity-lc original-page-name)
;; TODO: Enable date/* fns
day false #_(date/journal-title->int page-name)]
(if day
(let [original-page-name "" #_(date/int->journal-title day)]
[original-page-name (util/page-name-sanity-lc original-page-name) day])
[original-page-name page-name day]))))
day (date-util/journal-title->int page-name)]
(if day
(let [original-page-name (date-util/int->journal-title day)]
[original-page-name (util/page-name-sanity-lc original-page-name) day])
[original-page-name page-name day]))))
(defn page-name->map
"Create a page's map structure given a original page name (string).
@@ -259,8 +262,7 @@
[original-page-name page-name journal-day] (convert-page-if-journal original-page-name)
namespace? (and (not (boolean (text/get-nested-page-name original-page-name)))
(text/namespace-page? original-page-name))
;; TODO: Pass db down to this fn
page-entity (some-> nil (d/entity [:block/name page-name]))]
page-entity (d/entity (deref @conn) [:block/name page-name])]
(merge
{:block/name page-name
:block/original-name original-page-name}
@@ -273,8 +275,7 @@
(when-not (string/blank? namespace)
{:block/namespace {:block/name (util/page-name-sanity-lc namespace)}})))
(when (and with-timestamp? (not page-entity)) ;; Only assign timestamp on creating new entity
;; TODO: add current time with cljs-core
(let [current-ms 0 #_(util/time-ms)]
(let [current-ms (time-util/time-ms)]
{:block/created-at current-ms
:block/updated-at current-ms}))
(if journal-day

View File

@@ -4,16 +4,19 @@
[clojure.set :as set]
[clojure.edn :as edn]
[clojure.walk :as walk]
[clojure.data :as data]
[datascript.core :as d]
[datascript.transit :as dt]
["fs" :as fs]
["child_process" :as child-process]
[frontend.db-schema :as db-schema]
[frontend.db.default :as default-db]
[logseq.graph-parser.mldoc :as mldoc]
[logseq.graph-parser.util :as util]
[logseq.graph-parser.property :as property]
[logseq.graph-parser.text :as text]
[logseq.graph-parser.block :as block]
[logseq.graph-parser.time-util :as time-util]
;; Disable for now since kondo can't pick it up
; #?(:org.babashka/nbb [nbb.core :as nbb])
[nbb.core :as nbb]))
@@ -414,8 +417,7 @@
;; does order matter?
(concat file-content pages-index delete-blocks pages block-ids blocks))
file-content)
;; TODO: Implement :file/created-at with cljs-time
tx (concat tx [(let [t 0 #_(tc/to-long (t/now))] ;; TODO: use file system timestamp?
tx (concat tx [(let [t (time-util/time-ms)] ;; TODO: use file system timestamp?
(cond->
{:file/path file}
new?
@@ -441,10 +443,11 @@
;; ========
(defn db-start
;; TODO: Add frontend.db.default/built-in-pages
[]
(let [db-conn (d/create-conn db-schema/schema)]
(d/transact! db-conn [{:schema/version db-schema/version}])
(d/transact! db-conn default-db/built-in-pages)
(reset! block/conn db-conn)
db-conn))
(defn -main
@@ -464,11 +467,15 @@
file-maps
files)
(prn :PAGES (d/q '[:find ?n :where [?b :block/name ?n]]
@conn))
@conn))
(prn :DATOMS (count (d/datoms @conn :eavt)))
(when cached-graph-file
(let [db (dt/read-transit-str (slurp cached-graph-file))]
(prn :ACTUAL-DATOMS (count (d/datoms db :eavt)))))))
(prn :ACTUAL-DATOMS (count (d/datoms db :eavt)))
(fs/writeFileSync "diff.edn"
(pr-str (butlast (data/diff (mapv seq (take 10 (d/datoms @conn :eavt)))
(mapv seq (take 10 (d/datoms db :eavt)))))))))))
(when (= nbb/*file* (:file (meta #'-main)))
(-main *command-line-args*))

View File

@@ -0,0 +1,47 @@
(ns logseq.graph-parser.date-util
"Date utilities that only depend on core fns and cljs-time. These are a subset of
ones from frontend.date"
(:require [cljs-time.format :as tf]
[clojure.string :as string]
[logseq.graph-parser.util :as util]
; [frontend.state :as state]
))
(defn format
[date]
(when-let [formatter-string "MMM do, yyyy" #_(state/get-date-formatter)]
(tf/unparse (tf/formatter formatter-string) date)))
;; (tf/parse (tf/formatter "dd.MM.yyyy") "2021Q4") => 20040120T000000
(defn safe-journal-title-formatters
[]
(->> [#_(state/get-date-formatter)"MMM do, yyyy" "yyyy-MM-dd" "yyyy_MM_dd"]
(remove string/blank?)
distinct))
(defn journal-title->
([journal-title then-fn]
(journal-title-> journal-title then-fn (safe-journal-title-formatters)))
([journal-title then-fn formatters]
(when-not (string/blank? journal-title)
(when-let [time (->> (map
(fn [formatter]
(try
(tf/parse (tf/formatter formatter) (util/capitalize-all journal-title))
(catch js/Error _e
nil)))
formatters)
(filter some?)
first)]
(then-fn time)))))
(defn journal-title->int
[journal-title]
(when journal-title
(let [journal-title (util/capitalize-all journal-title)]
(journal-title-> journal-title #(util/parse-int (tf/unparse (tf/formatter "yyyyMMdd") %))))))
(defn int->journal-title
[day]
(when day
(format (tf/parse (tf/formatter "yyyyMMdd") (str day)))))

View File

@@ -1,4 +1,5 @@
(ns logseq.graph-parser.mldoc
"Modified version of frontend.format.mldoc"
(:require [cljs-bean.core :as bean]
[clojure.string :as string]
; [frontend.format.protocol :as protocol]
@@ -6,9 +7,8 @@
[goog.object :as gobj]
[logseq.graph-parser.util :as util]
; [lambdaisland.glogi :as log]
; [medley.core :as medley]
["mldoc$default" :as mldoc :refer [Mldoc]]
; [linked.core :as linked]
[linked.core :as linked]
#_[frontend.config :as config]))
(defonce parseJson (gobj/get Mldoc "parseJson"))
@@ -132,7 +132,7 @@
v
(parse-property k v))]
[k v]))))
properties (into {} #_(linked/map) properties)
properties (into (linked/map) properties)
macro-properties (filter (fn [x] (= :macro (first x))) properties)
macros (if (seq macro-properties)
(->>
@@ -146,7 +146,7 @@
(into {}))
{})
properties (->> (remove (fn [x] (= :macro (first x))) properties)
(into {} #_(linked/map)))
(into (linked/map)))
properties (cond-> properties
(seq macros)
(assoc :macros macros))
@@ -164,8 +164,7 @@
properties (assoc properties :tags tags :alias alias)
properties (-> properties
(update :filetags (constantly filetags)))
; properties (medley/remove-kv (fn [_k v] (or (nil? v) (and (coll? v) (empty? v)))) properties)
properties (into {} (remove (fn [[_k v]] (or (nil? v) (and (coll? v) (empty? v)))) properties))]
properties (into (linked/map) (remove (fn [[_k v]] (or (nil? v) (and (coll? v) (empty? v)))) properties))]
(if (seq properties)
(cons [["Properties" properties] nil] other-ast)
original-ast))

View File

@@ -1,7 +1,7 @@
(ns logseq.graph-parser.text
"Modified version of frontend.text"
(:require [logseq.graph-parser.util :as util]
; ["/frontend/utils" :as utils]
["path" :as path]
[goog.string :as gstring]
[clojure.string :as string]
[logseq.graph-parser.mldoc :as mldoc]
@@ -11,14 +11,11 @@
(def org-page-ref-re #"\[\[(file:.*)\]\[.+?\]\]")
(def markdown-page-ref-re #"\[(.*)\]\(file:.*\)")
; (defonce ^js node-path utils/nodePath)
;; TODO: Load frontend/utils.js
(defn get-file-basename
[path]
path
#_(when-not (string/blank? path)
(node-path.name path)))
(when-not (string/blank? path)
;; Reimplemented utils/nodePath.name
(.-name (path/parse (string/replace path "+" "/")))))
(defn get-page-name
[s]

View File

@@ -0,0 +1,8 @@
(ns logseq.graph-parser.time-util
"Time specific utilities"
(:require [cljs-time.coerce :as tc]
[cljs-time.core :as t]))
(defn time-ms
[]
(tc/to-long (t/now)))

View File

@@ -169,3 +169,8 @@
m
#_(cske/transform-keys csk/->kebab-case-keyword m)
m))))
(defn capitalize-all [s]
(some->> (string/split s #" ")
(map string/capitalize)
(string/join " ")))