Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to get the content under a heading? #131

Answered by ChristianMurphy
ooker777 asked this question in Q&A
Discussion options

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 Heading 1, then I'll get:

text 1.1
text 1.2

If my input is Heading 2, then I'll get:

text 2.1
text 2.2

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.

You must be logged in to vote

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.

I'm thinking of using unist-util-select, but it doesn't support heading selectors.

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

Replies: 2 comments 6 replies

Comment options

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.

I'm thinking of using unist-util-select, but it doesn't support heading selectors.

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

You must be logged in to vote
6 replies
Comment options

I'd highly recommend reading the Unified getting started guide, particularly the section on plugins, there are some high level concepts in there which will help make both my comments above, in your PR, and here make more sense. https://unifiedjs.com/learn/

I'm not sure how to use it with for the AST only

It feels like you're trying to build a transformation pipeline, consider using unified rather than hand rolling the pipeline.

If I copy the whole code into my script, then use transform(tree) then the tree is sectionized.

Plugins accept a settings object, then return a transform(tree) with those settings applied.
My guess is you tried to pass the tree into settings, that won't work.

Comment options

I'm not sure what transformation pipeline is, but my guess is that you are guessing that I want to stringify the modified content or convert it to another format like HTML. If so then no, I don't. I want to inspect the tree so that I can get the desired value. But if what you mean is to transform a tree into another tree, then yes it is. The reason I want to work on the tree because it makes the code cleaner. If I work on the processor I still need to declare the variable, then put my function within a plugin, then catch the value inside it, then have it stringify the tree when I don't need it.

Comment options

A transform pipeline is unified https://unifiedjs.com/learn/guide/using-unified/
It lets you take some input, in your case markdown, apply plugins like GFM and your custom logic, and create an output.
Your choice of Markdown, HTML, custom JSON, or anything else.

It's a way of organizing the code to make it easier to work with and follow.

Comment options

So according to this diagram, a transform pipeline is not just a transformer, but the whole process of the processor, right?

| ........................ process ........................... |
| .......... parse ... | ... run ... | ... stringify ..........|
 +--------+ +----------+
Input ->- | Parser | ->- Syntax Tree ->- | Compiler | ->- Output
 +--------+ | +----------+
 X
 |
 +--------------+
 | Transformers |
 +--------------+
Comment options

It is often easiest to do the whole process parser, transformer, to compiler.
But you can also run just the transformer if you want.

See the example from your other question on how the full end to end process could look https://github.com/orgs/syntax-tree/discussions/132#discussioncomment-10465469

Answer selected by ooker777
Comment options

but it doesn't support heading selectors.

it does. The paragraph there is an example. heading works too.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /