Roam Research Docs · Developer documentation
{{[[roam/cljs]]}}, then nest a clojure code block underneath it.
(prn "hello from roam/cljs")
?disablejs=true — works exactly as described on roam/js, and its Good practices apply here too.
(ns ...) form — other roam/cljs blocks can then :require it.
:require only works if the defining block has already run.
{:location {:parent-uid ...}} instead of {"parent-uid": ...}).
roam.datascript/q and roam.datascript/pull query the graph directly — no connection argument, and queries are plain quoted vectors instead of strings.
roam.block, roam.page, roam.user) only exist when you have write access to the graph.
js/window, js/console.log, and anything on window.roamAlphaAPI that has no wrapper.
applied-science.js-interop.
:require of a URL string loads it as an ES module, and a .css URL injects a stylesheet.
(ns examples.random-page
(:require
[roam.datascript :as rd]
[roam.ui.command-palette :as command-palette]
[roam.ui.main-window :as main-window]))
(command-palette/add-command
{:label "Open random page"
:callback (fn [_]
(let [uids (rd/q '[:find [?uid ...]
:where [?e :node/title]
[?e :block/uid ?uid]])]
(main-window/open-page {:page {:uid (rand-nth uids)}})))})
(ns examples.daily-journal
(:require
[promesa.core :as p]
[roam.block :as block]
[roam.datascript :as rd]
[roam.page :as page]
[roam.util :as util]))
(defn ensure-today-has-journal []
(let [today-uid (util/date->page-uid (js/Date.))]
(p/do!
(when-not (rd/q '[:find ?p . :in $ ?uid
:where [?p :block/uid ?uid]]
today-uid)
(page/create {:page {:title (util/date->page-title (js/Date.))}}))
(when-not (rd/q '[:find ?c . :in $ ?uid ?text
:where [?p :block/uid ?uid]
[?p :block/children ?c]
[?c :block/string ?text]]
today-uid "Journal")
(block/create {:location {:parent-uid today-uid :order 0}
:block {:string "Journal"}})))))
(ensure-today-has-journal)
my.helpers block first — namespaces do not autoload, so the block that requires one only works after its defining block has run.
(ns my.helpers
(:require
[roam.datascript :as rd]))
(defn get-page-uid [title]
(rd/q '[:find ?uid . :in $ ?title
:where [?e :node/title ?title]
[?e :block/uid ?uid]]
title))
(ns my.scratch
(:require
[my.helpers :as h]))
(prn (h/get-page-uid "Reading List"))