8bitgentleman / Roam-Render-Components

A collection of custom components for Roam Research written in clojurescript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Request: Extend Child counter to count words/characters; work for pages

jbnv opened this issue · comments

I'd appreciate versions of the child counter code that could do the following:

  • 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.

For inspiration, here's the code for a SmartBlock I wrote that provides the counts for the page. I'd like this to be auto-updating but I'm still fairly novice on Clojure.

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';