Finish migration block ns to nbb

pull/5299/head
Gabriel Horner 2022-05-12 16:58:26 -04:00
parent 2c725ca5af
commit b1e226745d
15 changed files with 215 additions and 172 deletions

View File

@ -28,7 +28,8 @@
logseq.graph-parser.mldoc gp-mldoc
logseq.graph-parser.util gp-util
logseq.graph-parser.property gp-property
logseq.graph-parser.config gp-config}}}
logseq.graph-parser.config gp-config
logseq.graph-parser.date-time-util date-time-util}}}
:hooks {:analyze-call {rum.core/defc hooks.rum/defc
rum.core/defcs hooks.rum/defcs}}

View File

@ -9,6 +9,7 @@
[frontend.handler.page :as page-handler]
[frontend.state :as state]
[logseq.graph-parser.text :as text]
[logseq.graph-parser.util :as gp-util]
[frontend.ui :as ui]
[frontend.util :as util]
[goog.object :as gobj]
@ -51,7 +52,7 @@
:page))
(.preventDefault e)))}
[:h1.title
(util/capitalize-all title)]]
(gp-util/capitalize-all title)]]
(blocks-cp repo page format)

View File

@ -34,7 +34,7 @@
[medley.core :as medley]
[rum.core :as rum]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.block :as gp-block]
[frontend.format.block :as block]
[frontend.mobile.util :as mobile-util]))
(defn- get-page-name
@ -332,7 +332,7 @@
(db/entity repo))
(do
(when-not (db/entity repo [:block/name page-name])
(let [m (gp-block/page-name->map path-page-name true)]
(let [m (block/page-name->map path-page-name true)]
(db/transact! repo [m])))
(db/pull [:block/name page-name])))
{:keys [icon]} (:block/properties page)

View File

@ -5,9 +5,10 @@
[cljs-time.core :as t]
[cljs-time.format :as tf]
[cljs-time.local :as tl]
[clojure.string :as string]
[frontend.state :as state]
[frontend.util :as util]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.date-time-util :as date-time-util]
[goog.object :as gobj]
[lambdaisland.glogi :as log]))
@ -16,11 +17,6 @@
(when (string? s)
((gobj/get chrono "parseDate") s)))
(defn format
[date]
(when-let [formatter-string (state/get-date-formatter)]
(tf/unparse (tf/formatter formatter-string) date)))
(def custom-formatter (tf/formatter "yyyy-MM-dd'T'HH:mm:ssZZ"))
(defn journal-title-formatters
@ -55,13 +51,6 @@
"yyyy年MM月dd日"}
(state/get-date-formatter)))
;; (tf/parse (tf/formatter "dd.MM.yyyy") "2021Q4") => 20040120T000000
(defn safe-journal-title-formatters
[]
(->> [(state/get-date-formatter) "yyyy-MM-dd" "yyyy_MM_dd"]
(remove string/blank?)
distinct))
(defn get-date-time-string
([]
(get-date-time-string (t/now)))
@ -115,7 +104,7 @@
([]
(journal-name (tl/local-now)))
([date]
(format date)))
(date-time-util/format date (state/get-date-formatter))))
(defn journal-name-s [s]
(try
@ -183,34 +172,19 @@
(defn valid-journal-title?
[title]
(and title
(valid? (util/capitalize-all title))))
(valid? (gp-util/capitalize-all title))))
(defn journal-title->
([journal-title then-fn]
(journal-title-> journal-title then-fn (safe-journal-title-formatters)))
(journal-title-> journal-title then-fn (date-time-util/safe-journal-title-formatters (state/get-date-formatter))))
([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)))))
(date-time-util/journal-title-> journal-title then-fn formatters)))
(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)))))
(date-time-util/journal-title->int
journal-title
(date-time-util/safe-journal-title-formatters (state/get-date-formatter))))
(defn journal-day->ts
[day]
@ -240,7 +214,7 @@
(defn journal-title->custom-format
[journal-title]
(journal-title-> journal-title format))
(journal-title-> journal-title #(date-time-util/format % (state/get-date-formatter))))
(defn int->local-time-2
[n]

View File

@ -9,6 +9,23 @@
[logseq.graph-parser.property :as gp-property]
[logseq.graph-parser.mldoc :as gp-mldoc]))
(defn extract-blocks
"Wrapper around logseq.graph-parser.block/extract-blocks that adds in system state"
[blocks content with-id? format]
(gp-block/extract-blocks blocks content with-id? format
{:user-config (state/get-config)
:block-pattern (config/get-block-pattern format)
:supported-formats (config/supported-formats)
:db (db/get-db (state/get-current-repo))
:date-formatter (state/get-date-formatter)}))
(defn page-name->map
"Wrapper around logseq.graph-parser.block/page-name->map that adds in db"
([original-page-name with-id?]
(page-name->map original-page-name with-id? true))
([original-page-name with-id? with-timestamp?]
(gp-block/page-name->map original-page-name with-id? (db/get-db (state/get-current-repo)) with-timestamp? (state/get-date-formatter))))
(defn with-parent-and-left
[page-id blocks]
(loop [blocks (map (fn [block] (assoc block :block/level-spaces (:block/level block))) blocks)
@ -87,7 +104,7 @@
(when-not (string/blank? content)
(let [block (dissoc block :block/pre-block?)
ast (format/to-edn content format nil)
blocks (gp-block/extract-blocks ast content with-id? format)
blocks (extract-blocks ast content with-id? format)
new-block (first blocks)
parent-refs (->> (db/get-block-parent (state/get-current-repo) uuid)
:block/path-refs

View File

@ -266,7 +266,7 @@
(if (and (:block/pre-block? block)
(seq (:block/properties block)))
(let [page-properties (:block/properties block)
str->page (fn [n] (gp-block/page-name->map n true))
str->page (fn [n] (block/page-name->map n true))
refs (->> page-properties
(filter (fn [[_ v]] (coll? v)))
(vals)
@ -670,7 +670,9 @@
(defn properties-block
[properties format page]
(let [content (property/insert-properties format "" properties)
refs (gp-block/get-page-refs-from-properties properties)]
refs (gp-block/get-page-refs-from-properties properties
(db/get-db (state/get-current-repo))
(state/get-date-formatter))]
{:block/pre-block? true
:block/uuid (db/new-block-id)
:block/properties properties
@ -1946,7 +1948,7 @@
content* (str (if (= :markdown format) "- " "* ")
(property/insert-properties format content props))
ast (mldoc/->edn content* (gp-mldoc/default-config format))
blocks (gp-block/extract-blocks ast content* true format)
blocks (block/extract-blocks ast content* true format)
fst-block (first blocks)]
(assert fst-block "fst-block shouldn't be nil")
(assoc fst-block :block/level (:block/level block)))))))
@ -2853,7 +2855,7 @@
[format text]
(when-let [editing-block (state/get-edit-block)]
(let [page-id (:db/id (:block/page editing-block))
blocks (gp-block/extract-blocks
blocks (block/extract-blocks
(mldoc/->edn text (gp-mldoc/default-config format)) text true format)
blocks' (block/with-parent-and-left page-id blocks)]
(paste-blocks blocks' {}))))

View File

@ -8,8 +8,9 @@
[clojure.string :as string]
[frontend.db :as db]
[frontend.format.mldoc :as mldoc]
[logseq.graph-parser.block :as gp-block]
[frontend.format.block :as block]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.date-time-util :as date-time-util]
[frontend.handler.page :as page]
[frontend.handler.editor :as editor]
[frontend.util :as util]))
@ -50,7 +51,7 @@
(map
(fn [title]
(let [day (date/journal-title->int title)
page-name (util/page-name-sanity-lc (date/int->journal-title day))]
page-name (util/page-name-sanity-lc (date-time-util/int->journal-title day (state/get-date-formatter)))]
{:block/name page-name
:block/journal? true
:block/journal-day day}))
@ -74,7 +75,7 @@
(when-let [repo (state/get-current-repo)]
(let [[headers parsed-blocks] (mldoc/opml->edn data)
parsed-blocks (->>
(gp-block/extract-blocks parsed-blocks "" true :markdown)
(block/extract-blocks parsed-blocks "" true :markdown)
(mapv editor/wrap-parse-block))
page-name (:title headers)]
(when (not (db/page-exists? page-name))

View File

@ -48,8 +48,8 @@
[repo-url format ast properties file content]
(try
(let [page (get-page-name file ast)
[_original-page-name page-name _journal-day] (gp-block/convert-page-if-journal page)
blocks (->> (gp-block/extract-blocks ast content false format)
[_original-page-name page-name _journal-day] (gp-block/convert-page-if-journal page (state/get-date-formatter))
blocks (->> (block/extract-blocks ast content false format)
(block/with-parent-and-left {:block/name page-name}))
ref-pages (atom #{})
ref-tags (atom #{})
@ -66,7 +66,7 @@
:block/page [:block/name page-name]
:block/refs block-ref-pages
:block/path-refs block-path-ref-pages))))
blocks)
blocks)
page-entity (let [alias (:alias properties)
alias (if (string? alias) [alias] alias)
aliases (and alias
@ -75,56 +75,56 @@
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)
(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
(gp-block/page-name->map page false)
:block/file {:file/path (gp-util/path-normalize file)}))
(seq properties)
(assoc :block/properties properties)
(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)
(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)))))
(: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)
(->> (gp-util/split-namespace-pages page)
(map (fn [page]
(-> (gp-block/page-name->map page true)
(-> (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)
(fn [page]
{:block/original-name page
:block/name (util/page-name-sanity-lc page)})
@ref-tags)
namespace-pages)
;; remove block references
(remove vector?)

View File

@ -35,6 +35,7 @@
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.block :as gp-block]
[frontend.format.block :as block]
[goog.functions :refer [debounce]]))
(defn- get-directory
@ -72,7 +73,9 @@
(let [p (common-handler/get-page-default-properties title)
ps (merge p properties)
content (page-property/insert-properties format "" ps)
refs (gp-block/get-page-refs-from-properties properties)]
refs (gp-block/get-page-refs-from-properties properties
(db/get-db (state/get-current-repo))
(state/get-date-formatter))]
{:block/uuid (db/new-block-id)
:block/properties ps
:block/properties-order (keys ps)
@ -127,7 +130,7 @@
[title])
format (or format (state/get-preferred-format))
pages (map (fn [page]
(-> (gp-block/page-name->map page true)
(-> (block/page-name->map page true)
(assoc :block/format format)))
pages)
txs (->> pages

View File

@ -713,11 +713,6 @@
(defn drop-nth [n coll]
(keep-indexed #(when (not= %1 n) %2) coll))
(defn capitalize-all [s]
(some->> (string/split s #" ")
(map string/capitalize)
(string/join " ")))
#?(:cljs
(defn react
[ref]

View File

@ -1,12 +1,11 @@
(ns logseq.graph-parser.block
(ns ^:nbb-compatible logseq.graph-parser.block
;; Disable clj linters since we don't support clj
#?(:clj {:clj-kondo/config {:linters {:unresolved-namespace {:level :off}
:unresolved-symbol {:level :off}}}})
"Block related code needed for graph-parser"
(:require [clojure.string :as string]
[clojure.walk :as walk]
[frontend.config :as config]
[frontend.date :as date]
[frontend.db :as db]
[datascript.core :as d]
[frontend.state :as state]
[logseq.graph-parser.text :as text]
[logseq.graph-parser.utf8 :as utf8]
[logseq.graph-parser.property :as gp-property]
@ -14,7 +13,8 @@
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.date-time-util :as date-time-util]
[lambdaisland.glogi :as log]))
#?(:org.babashka/nbb [logseq.graph-parser.log :as log]
:default [lambdaisland.glogi :as log])))
(defn heading-block?
[block]
@ -36,8 +36,8 @@
"")))
(string/join))))
(defn get-page-reference
[block]
(defn- get-page-reference
[block supported-formats]
(let [page (cond
(and (vector? block) (= "Link" (first block)))
(let [typ (first (:url (second block)))
@ -65,7 +65,7 @@
(not (string/starts-with? value "file:"))
(not (gp-config/local-asset? value))
(or (= ext :excalidraw)
(not (contains? (config/supported-formats) ext))))
(not (contains? supported-formats ext))))
value)))
(and
@ -147,7 +147,7 @@
;; TODO: we should move this to mldoc
(defn extract-properties
[format properties]
[format properties user-config]
(when (seq properties)
(let [properties (seq properties)
page-refs (->>
@ -177,7 +177,7 @@
(remove string/blank? v)
(if (string/blank? v)
nil
(text/parse-property format k v (state/get-config))))
(text/parse-property format k v user-config)))
k (keyword k)
v (if (and
(string? v)
@ -226,12 +226,12 @@
(defn convert-page-if-journal
"Convert journal file name to user' custom date format"
[original-page-name]
[original-page-name date-formatter]
(when original-page-name
(let [page-name (gp-util/page-name-sanity-lc original-page-name)
day (date/journal-title->int page-name)]
day (date-time-util/journal-title->int page-name (date-time-util/safe-journal-title-formatters date-formatter))]
(if day
(let [original-page-name (date/int->journal-title day)]
(let [original-page-name (date-time-util/int->journal-title day date-formatter)]
[original-page-name (gp-util/page-name-sanity-lc original-page-name) day])
[original-page-name page-name day]))))
@ -241,48 +241,46 @@
with-timestamp?: assign timestampes to the map structure.
Useful when creating new pages from references or namespaces,
as there's no chance to introduce timestamps via editing in page"
([original-page-name with-id?]
(page-name->map original-page-name with-id? true))
([original-page-name with-id? with-timestamp?]
(cond
(and original-page-name (string? original-page-name))
(let [original-page-name (gp-util/remove-boundary-slashes original-page-name)
[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))
page-entity (db/entity [:block/name page-name])
original-page-name (or (:block/original-name page-entity) original-page-name)]
(merge
{:block/name page-name
:block/original-name original-page-name}
(when with-id?
(if page-entity
{:block/uuid (:block/uuid page-entity)}
{:block/uuid (d/squuid)}))
(when namespace?
(let [namespace (first (gp-util/split-last "/" original-page-name))]
(when-not (string/blank? namespace)
{:block/namespace {:block/name (gp-util/page-name-sanity-lc namespace)}})))
(when (and with-timestamp? (not page-entity)) ;; Only assign timestamp on creating new entity
(let [current-ms (date-time-util/time-ms)]
{:block/created-at current-ms
:block/updated-at current-ms}))
(if journal-day
{:block/journal? true
:block/journal-day journal-day}
{:block/journal? false})))
[original-page-name with-id? db with-timestamp? date-formatter]
(cond
(and original-page-name (string? original-page-name))
(let [original-page-name (gp-util/remove-boundary-slashes original-page-name)
[original-page-name page-name journal-day] (convert-page-if-journal original-page-name date-formatter)
namespace? (and (not (boolean (text/get-nested-page-name original-page-name)))
(text/namespace-page? original-page-name))
page-entity (some-> db (d/entity [:block/name page-name]))
original-page-name (or (:block/original-name page-entity) original-page-name)]
(merge
{:block/name page-name
:block/original-name original-page-name}
(when with-id?
(if page-entity
{:block/uuid (:block/uuid page-entity)}
{:block/uuid (d/squuid)}))
(when namespace?
(let [namespace (first (gp-util/split-last "/" original-page-name))]
(when-not (string/blank? namespace)
{:block/namespace {:block/name (gp-util/page-name-sanity-lc namespace)}})))
(when (and with-timestamp? (not page-entity)) ;; Only assign timestamp on creating new entity
(let [current-ms (date-time-util/time-ms)]
{:block/created-at current-ms
:block/updated-at current-ms}))
(if journal-day
{:block/journal? true
:block/journal-day journal-day}
{:block/journal? false})))
(and (map? original-page-name) (:block/uuid original-page-name))
original-page-name
(and (map? original-page-name) (:block/uuid original-page-name))
original-page-name
(and (map? original-page-name) with-id?)
(assoc original-page-name :block/uuid (d/squuid))
(and (map? original-page-name) with-id?)
(assoc original-page-name :block/uuid (d/squuid))
:else
nil)))
:else
nil))
(defn- with-page-refs
[{:keys [title body tags refs marker priority] :as block} with-id?]
[{:keys [title body tags refs marker priority] :as block} with-id? supported-formats db date-formatter]
(let [refs (->> (concat tags refs [marker priority])
(remove string/blank?)
(distinct))
@ -293,7 +291,7 @@
(when-not (and (vector? form)
(= (first form) "Custom")
(= (second form) "query"))
(when-let [page (get-page-reference form)]
(when-let [page (get-page-reference form supported-formats)]
(swap! refs conj page))
(when-let [tag (get-tag form)]
(let [tag (text/page-ref-un-brackets! tag)]
@ -315,7 +313,7 @@
(distinct))
refs (->> (distinct (concat refs children-pages))
(remove nil?))
refs (map (fn [ref] (page-name->map ref with-id?)) refs)]
refs (map (fn [ref] (page-name->map ref with-id? db true date-formatter)) refs)]
(assoc block :refs refs))))
(defn- with-block-refs
@ -409,7 +407,7 @@
block))
(defn- get-block-content
[utf8-content block format meta]
[utf8-content block format meta block-pattern]
(let [content (if-let [end-pos (:end_pos meta)]
(utf8/substring utf8-content
(:start_pos meta)
@ -417,7 +415,7 @@
(utf8/substring utf8-content
(:start_pos meta)))
content (when content
(let [content (text/remove-level-spaces content format (config/get-block-pattern format))]
(let [content (text/remove-level-spaces content format block-pattern)]
(if (or (:pre-block? block)
(= (:format block) :org))
content
@ -437,7 +435,7 @@
(d/squuid)))
(defn get-page-refs-from-properties
[properties]
[properties db date-formatter]
(let [page-refs (mapcat (fn [v] (cond
(coll? v)
v
@ -448,18 +446,18 @@
:else
nil)) (vals properties))
page-refs (remove string/blank? page-refs)]
(map (fn [page] (page-name->map page true)) page-refs)))
(map (fn [page] (page-name->map page true db true date-formatter)) page-refs)))
(defn- with-page-block-refs
[block with-id?]
[block with-id? supported-formats db date-formatter]
(some-> block
(with-page-refs with-id?)
(with-page-refs with-id? supported-formats db date-formatter)
with-block-refs
block-tags->pages
(update :refs (fn [col] (remove nil? col)))))
(defn- with-pre-block-if-exists
[blocks body pre-block-properties encoded-content]
[blocks body pre-block-properties encoded-content {:keys [supported-formats db date-formatter]}]
(let [first-block (first blocks)
first-block-start-pos (get-in first-block [:block/meta :start_pos])
@ -471,7 +469,7 @@
(let [content (utf8/substring encoded-content 0 first-block-start-pos)
{:keys [properties properties-order]} pre-block-properties
id (get-custom-id-or-new-id {:properties properties})
property-refs (->> (get-page-refs-from-properties properties)
property-refs (->> (get-page-refs-from-properties properties db date-formatter)
(map :block/original-name))
block {:uuid id
:content content
@ -482,7 +480,7 @@
:pre-block? true
:unordered true
:body body}
block (with-page-block-refs block false)]
block (with-page-block-refs block false supported-formats db date-formatter)]
(block-keywordize block))
(select-keys first-block [:block/format :block/page]))
blocks)
@ -490,7 +488,7 @@
(with-path-refs blocks)))
(defn- construct-block
[block properties timestamps body encoded-content format pos-meta with-id?]
[block properties timestamps body encoded-content format pos-meta with-id? {:keys [block-pattern supported-formats db date-formatter]}]
(let [id (get-custom-id-or-new-id properties)
ref-pages-in-properties (->> (:page-refs properties)
(remove string/blank?))
@ -518,12 +516,12 @@
(assoc block :collapsed? true)
block)
block (assoc block
:content (get-block-content encoded-content block format pos-meta))
:content (get-block-content encoded-content block format pos-meta block-pattern))
block (if (seq timestamps)
(merge block (timestamps->scheduled-and-deadline timestamps))
block)
block (assoc block :body body)
block (with-page-block-refs block with-id?)
block (with-page-block-refs block with-id? supported-formats db date-formatter)
{:keys [created-at updated-at]} (:properties properties)
block (cond-> block
(and created-at (integer? created-at))
@ -539,8 +537,10 @@
`blocks`: mldoc ast.
`content`: markdown or org-mode text.
`with-id?`: If `with-id?` equals to true, all the referenced pages will have new db ids.
`format`: content's format, it could be either :markdown or :org-mode."
[blocks content with-id? format]
`format`: content's format, it could be either :markdown or :org-mode.
`options`: Options supported are :user-config, :block-pattern :supported-formats,
:date-formatter and :db"
[blocks content with-id? format {:keys [user-config] :as options}]
{:pre [(seq blocks) (string? content) (boolean? with-id?) (contains? #{:markdown :org} format)]}
(try
(let [encoded-content (utf8/encode content)
@ -564,11 +564,11 @@
(recur headings (rest blocks) timestamps' properties body))
(gp-property/properties-ast? block)
(let [properties (extract-properties format (second block))]
(let [properties (extract-properties format (second block) user-config)]
(recur headings (rest blocks) timestamps properties body))
(heading-block? block)
(let [block (construct-block block properties timestamps body encoded-content format pos-meta with-id?)]
(let [block (construct-block block properties timestamps body encoded-content format pos-meta with-id? options)]
(recur (conj headings block) (rest blocks) {} {} []))
:else
@ -577,8 +577,7 @@
sanity-blocks-data)
body
properties]))
result (with-pre-block-if-exists blocks body pre-block-properties encoded-content)]
result (with-pre-block-if-exists blocks body pre-block-properties encoded-content options)]
(map #(dissoc % :block/meta) result))
(catch js/Error e
(js/console.error "extract-blocks-failed")
(log/error :exception e))))
(catch :default e
(log/error :extract-blocks-failure e))))

View File

@ -1,9 +1,51 @@
(ns ^:nbb-compatible logseq.graph-parser.date-time-util
"cljs-time util fns for graph-parser"
(:require [cljs-time.coerce :as tc]
[cljs-time.core :as t]))
[cljs-time.core :as t]
[cljs-time.format :as tf]
[clojure.string :as string]
[logseq.graph-parser.util :as gp-util]))
(defn time-ms
"Copy of util/time-ms. Too basic to couple this to main app"
[]
(tc/to-long (t/now)))
;; (tf/parse (tf/formatter "dd.MM.yyyy") "2021Q4") => 20040120T000000
(defn safe-journal-title-formatters
[date-formatter]
(->> [date-formatter "MMM do, yyyy" "yyyy-MM-dd" "yyyy_MM_dd"]
(remove string/blank?)
distinct))
(defn journal-title->
[journal-title then-fn formatters]
(when-not (string/blank? journal-title)
(when-let [time (->> (map
(fn [formatter]
(try
(tf/parse (tf/formatter formatter) (gp-util/capitalize-all journal-title))
(catch js/Error _e
nil)))
formatters)
(filter some?)
first)]
(then-fn time))))
(defn journal-title->int
[journal-title formatters]
(when journal-title
(let [journal-title (gp-util/capitalize-all journal-title)]
(journal-title-> journal-title
#(gp-util/parse-int (tf/unparse (tf/formatter "yyyyMMdd") %))
formatters))))
(defn format
[date date-formatter]
(when date-formatter
(tf/unparse (tf/formatter date-formatter) date)))
(defn int->journal-title
[day date-formatter]
(when day
(format (tf/parse (tf/formatter "yyyyMMdd") (str day)) date-formatter)))

View File

@ -155,3 +155,9 @@
"Sanitize the query string for a page name (mandate for :block/name)"
[s]
(page-name-sanity (string/lower-case s)))
(defn capitalize-all
[s]
(some->> (string/split s #" ")
(map string/capitalize)
(string/join " ")))

View File

@ -3,7 +3,7 @@
[cljs.test :refer [deftest are]]))
(deftest test-extract-properties
(are [x y] (= (:properties (gp-block/extract-properties :markdown x)) y)
(are [x y] (= (:properties (gp-block/extract-properties :markdown x {})) y)
[["year" "1000"]] {:year 1000}
[["year" "\"1000\""]] {:year "\"1000\""}
[["background-color" "#000000"]] {:background-color "#000000"}
@ -23,7 +23,7 @@
[["foo" "bar, [[baz, test]]"]] {:foo #{"bar" "baz, test"}}
[["foo" "bar, [[baz, test, [[nested]]]]"]] {:foo #{"bar" "baz, test, [[nested]]"}})
(are [x y] (= (vec (:page-refs (gp-block/extract-properties :markdown x))) y)
(are [x y] (= (vec (:page-refs (gp-block/extract-properties :markdown x {}))) y)
[["year" "1000"]] []
[["year" "\"1000\""]] []
[["foo" "[[bar]] test"]] ["bar" "test"]

View File

@ -5,6 +5,7 @@
[logseq.graph-parser.text :as text]
[logseq.graph-parser.text-test]
[logseq.graph-parser.mldoc-test]
[logseq.graph-parser.block-test]
[logseq.graph-parser.property-test]))
(defmethod cljs.test/report [:cljs.test/default :end-run-tests] [m]
@ -19,4 +20,5 @@
(alter-var-root #'gp-mldoc/parse-property (constantly text/parse-property))
(t/run-tests 'logseq.graph-parser.mldoc-test
'logseq.graph-parser.text-test
'logseq.graph-parser.property-test))
'logseq.graph-parser.property-test
'logseq.graph-parser.block-test))