Find which section a node belongs to
Goal: You already have a single node (say, the first code block in a doc) and want to know which heading section it lives under, e.g. to report “found this in the Installation section”.
Prerequisites: The section module, via -A or nodes.
Query
$ mq -A 'let n = first(compact(.code)) | section::containing(n) | section::title' README.md
Or with nodes:
import "section"
| nodes
| let n = first(compact(.code))
| section::containing(n) | section::title
Input
# Introduction
Welcome.
## Installation
```bash
npm install
```
## Usage
Run it.
Output
Installation
Notes
section::containing(md_nodes, node)is the reverse of picking a section apart: given all the document’s nodes and one node you already have, it returns thesection::sections-style dict that node belongs to (orNoneif it doesn’t belong to any).- It matches the given node against each section’s
headerandchildrenby equality, so it works with any node you found some other way — a plain selector match (.code,.h2, …), afilter/is_*predicate, or an item you pulled out ofsection::body. - Matching is structural: a document with duplicate content (e.g. two identical code blocks) resolves to the first section that contains a matching node.
- Pass
trueas the third argument to match against depth-extended sections (seesection::sections’sdepthparameter) instead of the immediate section.