Extract portions of Markdown AST trees with configurable behavior for partially included elements.
npm install mdast-util-slice-markdown
Extracts a portion of a markdown AST based on character positions.
tree
- The markdown AST nodestart
- Starting character position (inclusive)end
- Ending character position (exclusive)options
- Configuration object
Control how partially included elements are handled:
{
behavior: {
// Formatting elements
emphasis: 'preserve' | 'trim' | 'exclude' | 'content', // *italic*
strong: 'preserve' | 'trim' | 'exclude' | 'content', // **bold**
link: 'preserve' | 'trim' | 'exclude' | 'content', // [text](url)
// Code elements
inlineCode: 'preserve' | 'trim' | 'exclude', // `code`
code: 'preserve' | 'trim' | 'exclude', // ```blocks```
// Media elements
image: 'preserve' | 'trim' | 'exclude' // 
}
}
Behavior Types:
preserve
- Keep the element with its formattingtrim
- Keep text content, trim slicedexclude
- Remove the element entirelycontent
- Extract visible content (for links)
{
preserveBlocks: true, // Keep empty blocks after slicing
trimWhitespace: false, // Trim whitespace at boundaries
handlers: {} // Custom node handlers
}