How to get the content under a heading? #131
-
For example, if I have: # Heading 1
text 1.1
text 1.2
# Heading 2
text 2.1
text 2.2
# Heading 3 If my input is
If my input is
My idea is to select all headings first, then match subsequent paragraph before the next heading: import {fromMarkdown} from 'mdast-util-from-markdown'
import {filter} from 'unist-util-filter'
import {Node} from '@types/unist'
import {inspect} from 'unist-util-inspect'
const tree = fromMarkdown(doc)
const headings = filter(tree, {cascade: false}, (node: Node) => node.type === 'paragraph') But unist-util-filter only filters with inequality but not equality. I'm thinking of using unist-util-select, but it doesn't support heading selectors. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Welcome @ooker777! 👋
It is supported. |
Beta Was this translation helpful? Give feedback.
-
it does. The |
Beta Was this translation helpful? Give feedback.
Welcome @ooker777! 👋
You may want to look at https://github.com/jake-low/remark-sectionize as a reference it does something similar to what I suspect you want to achieve.
It is supported.
Headings are a node type, the third item on the list you linked to, is how to query by a node type (
paragraph
being a single example of a type, there are many,heading
is another)See https://github.com/syntax-tree/mdast?tab=readme-ov-file#contents for a full list of node types