Keep only sections at a given heading level
Goal: Drop deeper subsections and keep just the top-level (or any one level) sections of a document.
Prerequisites: The section module. Section functions need all document nodes at once — pass -A on the command line, or nodes in a script.
Query
$ mq -A 'section::sections | section::by_level(1)' README.md
by_level also accepts a range:
$ mq -A 'section::sections | section::by_level(1..2)' README.md
Input
# Chapter 1
Intro.
## Section 1.1
Detail.
# Chapter 2
Content.
Output (by_level(1))
# Chapter 1
Intro.
# Chapter 2
Content.
Notes
by_level(2)on the same input would return## Section 1.1only, sinceChapter 1andChapter 2are level 1.