diff --git a/src/public-api.ts b/src/public-api.ts index 86680ea4..a5e05327 100644 --- a/src/public-api.ts +++ b/src/public-api.ts @@ -3,6 +3,7 @@ import type { Reviver } from './doc/applyReviver.js' import { Document, Replacer } from './doc/Document.js' import { prettifyError, YAMLParseError } from './errors.js' import { warn } from './log.js' +import { isDocument } from './nodes/identity.js' import type { Node, ParsedNode } from './nodes/Node.js' import type { CreateNodeOptions, @@ -225,5 +226,6 @@ export function stringify( const { keepUndefined } = options ?? (replacer as CreateNodeOptions) ?? {} if (!keepUndefined) return undefined } + if (isDocument(value) && !_replacer) return value.toString(options) return new Document(value, _replacer, options).toString(options) } diff --git a/tests/doc/stringify.ts b/tests/doc/stringify.ts index 400b051f..b49d334c 100644 --- a/tests/doc/stringify.ts +++ b/tests/doc/stringify.ts @@ -1405,6 +1405,12 @@ describe('YAML.stringify on ast Document', () => { const doc = YAML.parseDocument('null') expect(YAML.stringify(doc)).toBe('null\n') }) + + test('Document with comments', () => { + const src = 'a: 1\n\n# c\n' + const doc = YAML.parseDocument(src) + expect(YAML.stringify(doc)).toBe(src) + }) }) describe('flow collection padding', () => {