There are a lot of tricky details to this, and certainly some plugin-side caching will be required eventually since the queries could get messy, so I’m happy to discuss more.
If preferred, the plugin could allow the user to provide Logseq page and block properties to create a grammar. This could still be done in the notes like so: