Homewrecker
Homewrecker

Reputation: 1106

yaml library which supports writing multiple documents

I have a small NodeJS app which generates two YAML-files. I want to merge them into one file so that I have one file with two Document nodes. Something like:

---
yaml1
---
yaml2

I tried using the npm package yaml but to no avail. Browsing through the docs of js-yaml, I cannot find how to achieve this. Any help is appreciated.

Upvotes: 3

Views: 1153

Answers (1)

flyx
flyx

Reputation: 39768

YAML has been designed so that it is easy to merge multiple documents in a stream. Quoting the spec:

Concatenating two YAML streams requires both to use the same character encoding. In addition, it is necessary to separate the last document of the first stream and the first document of the second stream. This is easily ensured by inserting a document end marker between the two streams. Note that this is safe regardless of the content of either stream. In particular, either or both may be empty, and the first stream may or may not already contain such a marker.

The document end marker is ... (followed by a newline). Joining the contents of both files with this marker will do the trick. This works since YAML allows a document to be ended by multiple document end markers. On the other hand, the directives end marker (---) you use always starts a document, so it is not safe to join the documents with it since the second document may already start with one, leading to the creation of an empty document in between.

Upvotes: 2

Related Questions