I am exploring the possibility of creating narrative schemas for observable notebooks. That is, validating a notebook against a set of user-defined rules that, for example, require the notebook to contain certain headings, or that a cell contents have a minimum or maximum word count.
It may be that the way forward is to do this via named cells and, as you suggest, use .textContent but am open to other approaches if easier for the document author.
I think you can do most of this stuff with DOM mutation observers. For example, there’s a table of contents component I wrote:
The D3 gallery also does some kind of interesting stuff to accumulate the gallery contents so we can show the number of entries (see the links cell in the appendix):
Another fun example:
There’s another technique which is even more obscure which is to listen to the events that Observable sends to the worker iframe to evaluate your notebook. That gives you not just the output (rendered content) or each cell, but the (compiled) source code which you could statically analyze. That would be a pretty advanced approach, however, and not something we would consider a public API. So mutation observers is probably the way to go. Fabian points out we no longer allow this technique.