Skip to content

Commit 438688f

Browse files
committed
fix: Stringify a Document as a Document (fixes #576)
1 parent baaabd0 commit 438688f

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Reviver } from './doc/applyReviver.js'
33
import { Document, Replacer } from './doc/Document.js'
44
import { prettifyError, YAMLParseError } from './errors.js'
55
import { warn } from './log.js'
6+
import { isDocument } from './nodes/identity.js'
67
import type { Node, ParsedNode } from './nodes/Node.js'
78
import type {
89
CreateNodeOptions,
@@ -225,5 +226,6 @@ export function stringify(
225226
const { keepUndefined } = options ?? (replacer as CreateNodeOptions) ?? {}
226227
if (!keepUndefined) return undefined
227228
}
229+
if (isDocument(value) && !_replacer) return value.toString(options)
228230
return new Document(value, _replacer, options).toString(options)
229231
}

tests/doc/stringify.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,12 @@ describe('YAML.stringify on ast Document', () => {
14051405
const doc = YAML.parseDocument('null')
14061406
expect(YAML.stringify(doc)).toBe('null\n')
14071407
})
1408+
1409+
test('Document with comments', () => {
1410+
const src = 'a: 1\n\n# c\n'
1411+
const doc = YAML.parseDocument(src)
1412+
expect(YAML.stringify(doc)).toBe(src)
1413+
})
14081414
})
14091415

14101416
describe('flow collection padding', () => {

0 commit comments

Comments
 (0)