{"library":"mdast-util-heading-range","title":"MDAST Heading Range Utility","description":"mdast-util-heading-range is a utility for working with Markdown Abstract Syntax Trees (MDAST) to identify and manipulate content sections defined by headings. It allows developers to find a specific heading, capture all content nodes within its section (up to the next heading of the same or lower depth, or the end of the document), and then apply a handler function to modify or replace that content. The current stable version is 4.0.0, released in a mostly ad-hoc fashion following the syntax-tree ecosystem's release schedule, typically with minor versions for features and patch versions for fixes. Key differentiators include its focus on heading-based content segmentation, offering a programmatic way to update generated sections (like a Table of Contents), and its integration within the broader `unified` and `remark` ecosystem. It is an ESM-only package since version 3.0.0 and requires Node.js 16 or higher as of version 4.0.0.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mdast-util-heading-range"],"cli":null},"imports":["import { headingRange } from 'mdast-util-heading-range'","import type { Info } from 'mdast-util-heading-range'","import type { Handler } from 'mdast-util-heading-range'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import {read} from 'to-vfile';\nimport {remark} from 'remark';\nimport {headingRange} from 'mdast-util-heading-range';\n\nconst markdownContent = `# Foo\\n\\nBar.\\n\\n# Baz\\n\\nOther content.\\n\\n## Sub Heading\\n\\nSub content.`;\n\nasync function processMarkdown() {\n  const file = await remark()\n    .use(myPluginThatReplacesFoo)\n    .process(String(markdownContent));\n\n  console.log(String(file));\n}\n\n/** @type {import('unified').Plugin<[], import('mdast').Root>} */\nfunction myPluginThatReplacesFoo() {\n  return function (tree) {\n    headingRange(tree, 'Foo', function (start, nodes, end) {\n      // This handler receives the 'Foo' heading (start), \n      // nodes between 'Foo' and 'Baz' (nodes), and the 'Baz' heading (end).\n      // It replaces the content of the 'Foo' section.\n      return [\n        start,\n        {type: 'paragraph', children: [{type: 'text', value: 'Content updated by mdast-util-heading-range.'}]},\n        {type: 'list', children: [{type: 'listItem', children: [{type: 'paragraph', children: [{type: 'text', value: 'New list item.'}]}]}]},\n        end\n      ]\n    })\n  }\n}\n\nprocessMarkdown();","lang":"typescript","description":"Demonstrates finding the 'Foo' heading section and replacing its content with new nodes using the `headingRange` utility.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}