YAWN YAML is a JavaScript/TypeScript library designed to parse and manipulate YAML documents while meticulously preserving comments, original formatting, and scalar styles. The current stable version is 3.0.0, which represents a complete rewrite, moving from a custom parsing engine to leveraging the modern `yaml` package's Concrete Syntax Tree (CST) model. This fundamental shift significantly enhances robustness in comment preservation and scalar style retention, making it ideal for configuration file management or scenarios where semantic and stylistic integrity of YAML is paramount. Key differentiators include its ability to treat comments as first-class citizens in the AST, ensuring they are not lost during updates, and a substantially reduced bundle size from approximately 300KB to 32KB since version 3.0.0. While no explicit release cadence is stated, major version increments, like v3, indicate significant API changes and architectural overhauls. It offers new API methods for fine-grained control over comments, such as `getCommentBefore()` and `setCommentAfter()`.
npm install yawn-yamlVerified import paths — ran on the pinned version, not inferred.
Demonstrates initializing YAWN with a YAML string, accessing and modifying its JSON representation, and then retrieving the updated YAML with original comments and styling preserved.
Review the v3.0.0 documentation or migration guides if upgrading from an earlier major version. Existing code will likely require refactoring to adapt to the new API surface and methods.
Always reassign the `json` property (`yawn.json = newJsonValue;`) after making modifications to ensure changes are propagated and the YAML string is updated correctly.
Utilize the dedicated comment manipulation methods provided by the YAWN API for precise control over comment placement and content, rather than attempting to inject comments by modifying the `json` property or the raw YAML string.
For CommonJS, if ESM is unavoidable, use dynamic import: `const YAWN = await import('yawn-yaml'); const yawnInstance = new YAWN.default(yamlString);`. For ESM, ensure `import YAWN from 'yawn-yaml';` is used.Consult the `yawn-yaml` v3.0.0 documentation for the updated API. Many methods were refactored or replaced due to the internal architecture change.
Validate the YAML string against a linter or an online YAML validator. Ensure consistent indentation (spaces, not tabs) and correct YAML syntax.