{"library":"mdast-util-compact","title":"Compact mdast Trees","description":"mdast-util-compact is a specialized utility within the unified (syntax-tree) ecosystem designed to normalize mdast (Markdown Abstract Syntax Tree) trees by merging adjacent text nodes and collapsing blockquotes. Its primary use case is to clean up an mdast tree after programmatically modifying it, making the tree structure more consistent with how it would be generated by a parser. The current stable version is 5.0.0. The package follows semantic versioning with major releases introducing breaking changes, often related to Node.js version support or ESM migration, as well as updates to underlying `mdast` type definitions. A key differentiator is its explicit recommendation against frequent use, suggesting developers should ideally maintain clean trees themselves rather than relying on this utility post-hoc. It is an ESM-only package and fully typed with TypeScript.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mdast-util-compact"],"cli":null},"imports":["import { compact } from 'mdast-util-compact'","import { compact } from 'https://esm.sh/mdast-util-compact@5'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { u } from 'unist-builder';\nimport { compact } from 'mdast-util-compact';\n\n// Create a sample mdast tree with adjacent text nodes and blockquotes\nconst tree = u('root', [\n  u('paragraph', [\n    u('text', 'Hello'),\n    u('text', ' '),\n    u('text', 'world!')\n  ]),\n  u('blockquote', [u('paragraph', [u('text', 'First quote line')])]),\n  u('text', '\\n'), // Newline between blocks might prevent merging without additional parsers\n  u('blockquote', [u('paragraph', [u('text', 'Second quote line')])])\n]);\n\nconsole.log('Original tree:\\n', JSON.stringify(tree, null, 2));\n\n// Compact the tree in-place\ncompact(tree);\n\nconsole.log('\\nCompacted tree:\\n', JSON.stringify(tree, null, 2));\n\n/*\nExpected compacted output (exact structure may vary based on unist-builder and mdast versions, \nbut text nodes and blockquotes will be merged):\nCompacted tree:\n {\n  \"type\": \"root\",\n  \"children\": [\n    {\n      \"type\": \"paragraph\",\n      \"children\": [\n        {\n          \"type\": \"text\",\n          \"value\": \"Hello world!\"\n        }\n      ]\n    },\n    {\n      \"type\": \"blockquote\",\n      \"children\": [\n        {\n          \"type\": \"paragraph\",\n          \"children\": [\n            {\n              \"type\": \"text\",\n              \"value\": \"First quote line\\nSecond quote line\"\n            }\n          ]\n        }\n      ]\n    }\n  ]\n}\n*/","lang":"typescript","description":"This example demonstrates how to use `compact` to merge adjacent text nodes within a paragraph and combine consecutive blockquote nodes in an mdast tree, modifying the tree in-place.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}