diff --git a/web/src/main/frontend/components/page.cljs b/web/src/main/frontend/components/page.cljs index 4822ec1f0..cd6255f41 100644 --- a/web/src/main/frontend/components/page.cljs +++ b/web/src/main/frontend/components/page.cljs @@ -52,14 +52,14 @@ [repo page file-path page-name page-original-name encoded-page-name sidebar? journal? block? block-id format] (let [raw-page-blocks (get-blocks repo page-name page-original-name block? block-id) page-blocks (db/with-dummy-block raw-page-blocks format - (if (empty? raw-page-blocks) - (let [content (db/get-file repo file-path)] - {:block/page {:db/id (:db/id page)} - :block/file {:db/id (:db/id (:page/file page))} - :block/meta - (let [file-id (:db/id (:page/file page))] - {:start-pos (utf8/length (utf8/encode content)) - :end-pos nil})})) + (if (empty? raw-page-blocks) + (let [content (db/get-file repo file-path)] + {:block/page {:db/id (:db/id page)} + :block/file {:db/id (:db/id (:page/file page))} + :block/meta + (let [file-id (:db/id (:page/file page))] + {:start-pos (utf8/length (utf8/encode content)) + :end-pos nil})})) journal?) start-level (or (:block/level (first page-blocks)) 1 ) hiccup-config {:id encoded-page-name @@ -220,6 +220,7 @@ repo (or repo current-repo) encoded-page-name (get-page-name state) page-name (string/lower-case (util/url-decode encoded-page-name)) + path-page-name page-name marker-page? (util/marker? page-name) priority-page? (contains? #{"a" "b" "c"} page-name) format (db/get-page-format page-name) @@ -309,11 +310,16 @@ :page {:page page}))))} [:h1.title {:style {:margin-left -2}} - (if (and (string/includes? page-original-name "[[") - (string/includes? page-original-name "]]")) - (let [ast (mldoc/->edn page-original-name (mldoc/default-config format))] - (hiccup/block-cp {} (ffirst ast))) - page-original-name)]]) + (if page-original-name + (if (and (string/includes? page-original-name "[[") + (string/includes? page-original-name "]]")) + (when page-original-name + (let [ast (mldoc/->edn page-original-name (mldoc/default-config format))] + (hiccup/block-cp {} (ffirst ast)))) + page-original-name) + (or + page-name + path-page-name))]]) [:div [:div.content (when (and file-path (not sidebar?) (not block?)) diff --git a/web/src/main/frontend/db.cljs b/web/src/main/frontend/db.cljs index acb0f1854..b60c463f7 100644 --- a/web/src/main/frontend/db.cljs +++ b/web/src/main/frontend/db.cljs @@ -227,7 +227,10 @@ (pull-many (state/get-current-repo) selector eids)) ([repo selector eids] (when-let [conn (get-conn repo)] - (d/pull-many conn selector eids)))) + (try + (d/pull-many conn selector eids) + (catch js/Error e + (js/console.error e)))))) (defn get-handler-keys [{:keys [key data]}] @@ -1119,6 +1122,7 @@ _ (transact! repo-url block-refs) pages (pages-fn blocks ast) ref-pages (atom #{}) + ref-tags (atom #{}) blocks (doall (mapcat (fn [[page blocks]] @@ -1139,73 +1143,82 @@ block-ref-pages))))) blocks))) (remove nil? pages))) - pages (map - (fn [page] - (let [page-file? (= page (string/lower-case file)) - other-alias (and (:alias directives) - (seq (remove #(= page %) - (:alias directives)))) - other-alias (distinct - (remove nil? other-alias)) - journal-date-long (if journal? - (date/journal-title->long (string/capitalize page))) - page-list (when-let [list-content (:list directives)] - (extract-page-list list-content))] - (cond-> - (util/remove-nils - {:page/name (string/lower-case page) - :page/original-name page - :page/file [:file/path file] - :page/journal? journal? - :page/journal-day (if journal? - (date/journal-title->int (string/capitalize page)) - 0) - :page/created-at journal-date-long - :page/last-modified-at journal-date-long}) - (seq directives) - (assoc :page/directives directives) + pages (doall + (map + (fn [page] + (let [page-file? (= page (string/lower-case file)) + other-alias (and (:alias directives) + (seq (remove #(= page %) + (:alias directives)))) + other-alias (distinct + (remove nil? other-alias)) + journal-date-long (if journal? + (date/journal-title->long (string/capitalize page))) + page-list (when-let [list-content (:list directives)] + (extract-page-list list-content))] + (cond-> + (util/remove-nils + {:page/name (string/lower-case page) + :page/original-name page + :page/file [:file/path file] + :page/journal? journal? + :page/journal-day (if journal? + (date/journal-title->int (string/capitalize page)) + 0) + :page/created-at journal-date-long + :page/last-modified-at journal-date-long}) + (seq directives) + (assoc :page/directives directives) - other-alias - (assoc :page/alias - (map - (fn [alias] - (let [alias (string/lower-case alias) - aliases (->> - (distinct - (conj - (remove #{alias} other-alias) - page)) - (remove nil?)) - aliases (if (seq aliases) - (map - (fn [alias] - {:page/name alias}) - aliases))] - (if (seq aliases) - {:page/name alias - :page/alias aliases} - {:page/name alias}))) - other-alias)) + other-alias + (assoc :page/alias + (map + (fn [alias] + (let [alias (string/lower-case alias) + aliases (->> + (distinct + (conj + (remove #{alias} other-alias) + page)) + (remove nil?)) + aliases (if (seq aliases) + (map + (fn [alias] + {:page/name alias}) + aliases))] + (if (seq aliases) + {:page/name alias + :page/alias aliases} + {:page/name alias}))) + other-alias)) - (or (:tags directives) (:roam_tags directives)) - (assoc :page/tags (let [tags (:tags directives) - roam-tags (:roam_tags directives) - tags (if (string? tags) - (string/split tags #",") - tags) - tags (->> (concat tags roam-tags) - (remove nil?) - (distinct))] - (util/->tags tags)))))) - (->> (map first pages) - (remove nil?))) + (or (:tags directives) (:roam_tags directives)) + (assoc :page/tags (let [tags (:tags directives) + roam-tags (:roam_tags directives) + tags (if (string? tags) + (string/split tags #",") + tags) + tags (->> (concat tags roam-tags) + (remove nil?) + (distinct)) + tags (util/->tags tags)] + (swap! ref-tags set/union (set (map :tag/name tags))) + tags))))) + (->> (map first pages) + (remove nil?)))) pages (concat pages + (map + (fn [page] + {:page/original-name page + :page/name page}) + @ref-tags) (map (fn [page] {:page/original-name page :page/name (string/lower-case page)}) @ref-pages))] + (prn {:pages pages}) (vec (->> (concat pages diff --git a/web/src/main/frontend/db_schema.cljs b/web/src/main/frontend/db_schema.cljs index 0ec27df36..69cef4c51 100644 --- a/web/src/main/frontend/db_schema.cljs +++ b/web/src/main/frontend/db_schema.cljs @@ -48,6 +48,8 @@ :db/cardinality :db.cardinality/many} :page/tags {:db/valueType :db.type/ref :db/cardinality :db.cardinality/many} + :page/definitions {:db/valueType :db.type/ref + :db/cardinality :db.cardinality/many} :page/created-at {} :page/last-modified-at {} :page/contributors {} @@ -94,4 +96,11 @@ :db/unique :db.unique/identity} ;; For pages - :tag/name {:db/unique :db.unique/identity}}) + :tag/name {:db/unique :db.unique/identity} + + ;; Definitions, useful for tags and future anki cards + :definition/block {:db/valueType :db.type/ref} + ;; Why not make :definition/key unique? + ;; Multiple definitions with the same key in either one page or multiple pages + :definition/key {} + :definition/value {}}) diff --git a/web/src/main/frontend/format/mldoc.cljs b/web/src/main/frontend/format/mldoc.cljs index 8e6626386..277b9294a 100644 --- a/web/src/main/frontend/format/mldoc.cljs +++ b/web/src/main/frontend/format/mldoc.cljs @@ -5,7 +5,8 @@ [clojure.string :as string] [frontend.loader :as loader] [cljs-bean.core :as bean] - [medley.core :as medley])) + [medley.core :as medley] + [cljs.core.match :refer-macros [match]])) (defn default-config [format] @@ -44,6 +45,39 @@ (map string/lower-case) (map string/trim))))) +;; Org-roam +(defn get-tags-from-definition + [ast] + (loop [ast ast] + (if (seq ast) + (match (first ast) + ["List" l] + (when-let [name (:name (first l))] + (let [name (and (vector? name) + (last (first name)))] + (when (and (string? name) + (= (string/lower-case name) "tags")) + (->> + (last (first (:content (first l)))) + (map second) + (filter (and map? :url)) + (map (fn [x] + (let [label (last (first (:label x))) + search (and (= (first (:url x)) "Search") + (last (:url x))) + tag (if-not (string/blank? label) + label + search)] + (when tag (string/lower-case tag))))) + (remove nil?))))) + + ["Heading" _h] + nil + + :else + (recur (rest ast))) + nil))) + (defn collect-page-directives [ast] (if (seq ast) @@ -82,6 +116,12 @@ (:roam_tags directives) (update :roam_tags sep-by-quote-or-space-or-comma)) directives) + definition-tags (get-tags-from-definition ast) + directives (if definition-tags + (update directives :tags (fn [tags] + (-> (concat tags definition-tags) + distinct))) + directives) directives (cond-> directives (seq macros) (assoc :macros macros)) diff --git a/web/src/main/frontend/handler/editor.cljs b/web/src/main/frontend/handler/editor.cljs index a5f4fdea4..1542a9b44 100644 --- a/web/src/main/frontend/handler/editor.cljs +++ b/web/src/main/frontend/handler/editor.cljs @@ -289,8 +289,8 @@ (reset! last-child-end-pos old-end-pos))) (cond-> - {:block/uuid uuid - :block/meta new-meta} + {:block/uuid uuid + :block/meta new-meta} (and (some? indent-left?) (not @next-leq-level?)) (assoc :block/level (if indent-left? (dec level) (inc level))) (and new-content (not @next-leq-level?)) @@ -403,6 +403,14 @@ {:db/id page-id :page/directives new-directives}] [[:db/retract page-id :page/directives]])) + pages (if (seq page-tags) + (let [tag-pages (map + (fn [page] + {:page/original-name page + :page/name page}) + (map :tag/name page-tags))] + (concat pages tag-pages)) + pages) page-tags (when (and pre-block? (seq page-tags)) (if (seq page-tags) [[:db/retract page-id :page/tags]