yaml-unist-parser is a JavaScript library designed to parse YAML strings and produce an Abstract Syntax Tree (AST) that is compatible with the unist specification. This makes it a suitable tool for applications that process YAML content using a unified syntax tree, such as linters, formatters, and compilers within the unist ecosystem. The current stable version is 3.1.0, with a recent major release (v3.0.0) indicating active development, though a precise release cadence isn't published. Key differentiators include its focus on generating a unist-compatible AST, enhanced node positioning within the AST, and improved comment attaching, which are crucial for tools like Prettier that rely on precise AST details for formatting.
npm install yaml-unist-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a complex YAML string into a unist AST, handling potential errors.
Replace `allowDuplicateKeysInMap: true` with `uniqueKeys: false` (to allow duplicates) or simply remove the option if you desire the default unique key behavior (`uniqueKeys: true`).
Thoroughly test existing YAML parsing logic when upgrading to v3.0.0 to ensure no unexpected changes in AST output or error conditions, particularly for complex or unconventional YAML structures.
To allow duplicate keys in maps, pass `{ uniqueKeys: false }` as an option to the `parse` function. Example: `parse(yamlString, { uniqueKeys: false });`Either ensure all map keys in your YAML are unique, or allow duplicate keys by calling `parse(yamlString, { uniqueKeys: false });`Use a named ESM import: `import { parse } from 'yaml-unist-parser';`