Counting
Count the number of words.
Count the number of characters.
Versions of each of these that count on the page that contains the block instead of its children.
Insp
let ancestorrule=`[
[ (ancestor ?b ?a)
[?a :block/children ?b] ]
[ (ancestor ?b ?a)
[?parent :block/children ?b ]
(ancestor ?parent ?a) ] ] ]`;
let blocks = window.roamAlphaAPI.q(`[
:find
?string
:in $ ?pagetitle %
:where
[?block :block/string ?string]
[?page :node/title ?pagetitle]
(ancestor ?block ?page)
]`,"<%CURRENTPAGENAME%>", ancestorrule);
var blockCount = blocks.length;
var blocksJoined = blocks.join();
var wordCount = blocksJoined.split(/\s+/).length;
var characterCount = blocksJoined.length;
return ''+blockCount+' blocks, '+wordCount+' words, '+characterCount+' characters';
v1
count the words or the chars for the blocks below
ui
code
(ns counting-v1
(:require
[reagent.core :as r]
[datascript.core :as d]
[roam.datascript.reactive :as dr]
[clojure.string :as string]
[clojure.pprint :as pp]))
(defn flatten-block
"Flattens blocks children into a flat list"
[acc block]
(reduce flatten-block
(conj acc (dissoc block :block/children))
(:block/children block)))
(defn find-child-count
"Counts all children blocks given a parent block uid"
[block-uid]
(flatten-block []
@(dr/q '[:find (pull ?e [:block/string {:block/children ...}]) .
:in $ ?uid
:where
[?e :block/uid ?uid]]
block-uid)
))
(defn count-words [coll]
(->> coll
(apply merge-with (comp flatten vector))
(:block/string )
;;(.log js/console )
;;(apply str)
(string/join " ")
;;(.log js/console )
(filter #{\space})
count
))
(defn count-char [coll]
(->> coll
(apply merge-with (comp flatten vector))
(:block/string )
(drop 1)
string/join
;;(.log js/console )
merge
count
))
(defn main [{:keys [block-uid]} & args]
;;(.log js/console (find-child-count block-uid))
;;(.log js/console (merge (find-child-count block-uid)))
(.log js/console (string/join " " ["asfd" "sdfs"]))
(.log js/console (count-words (find-child-count block-uid)))
[:div ]
)
v2
count the words or the chars for a page
ui
code
(ns counting-v2
(:require
[reagent.core :as r]
[datascript.core :as d]
[roam.datascript.reactive :as dr]
[clojure.string :as string]
[clojure.pprint :as pp]))
(defn flatten-block
"Flattens blocks children into a flat list"
[acc block]
(reduce flatten-block
(conj acc (dissoc block :block/children))
(:block/children block)))
(defn find-child-count
"Counts all children blocks given a parent block uid"
[block-uid]
(flatten-block []
@(dr/q '[:find (pull ?e [:block/string {:block/children ...}]) .
:in $ ?uid
:where
[?e :block/uid ?uid]]
block-uid)
))
(defn count-words [coll]
(->> coll
(apply merge-with (comp flatten vector))
(:block/string )
;;(.log js/console )
;;(apply str)
(string/join " ")
;;(.log js/console )
(filter #{\space})
count
))
(defn count-char [coll]
(->> coll
(apply merge-with (comp flatten vector))
(:block/string )
(drop 1)
string/join
;;(.log js/console )
merge
count
))
(defn main [{:keys [block-uid]} & args]
;;(.log js/console (find-child-count block-uid))
;;(.log js/console (merge (find-child-count block-uid)))
(.log js/console (string/join " " ["asfd" "sdfs"]))
(.log js/console (count-words (find-child-count block-uid)))
[:div ]
)