jeremyfa / yaml.js

Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
MIT License
887 stars 142 forks source link

Feature Request: For strings that contain newline characters, use YAML multiline string convention #56

Open mm-gmbd opened 8 years ago

mm-gmbd commented 8 years ago

I'm implementing a build system where I start with a single "master" YAML file, and then with various gulp tasks, other versions of the file are created. There is some meta-information in the "master" YAML file that indicates, based on which gulp task is used, sections to either leave in or omit.

So, I'm using yaml.js to load the master YAML, then use JS to parse through the structure, identify sections that should be added or omitted, and then using stringify to create the resulting output. The issue is, I have some very long strings in the master YAML file that end up being output onto a single line in the resulting YAML. Unfortunately, this causes certain text editors to hang (cough Atom cough), but more importantly it makes whatever that string basically unmaintainable (because it cannot easily be edited).

I have used the YAML >+ operator for multi-line strings in the master YAML, and an option to force stringify to do the same and use the line breaks that are included in the string to make the line breaks in the block string. Apologies if something like this is already included. Thanks for the great tool!

julianxhokaxhiu commented 6 years ago

I also would like to see this implemented. I'll try although to come up with a patch in order to implement this, shouldn't be difficult.

julianxhokaxhiu commented 6 years ago

I did a tentative patch but I ended up on not getting the logic for indentation.

Basically what is missing now, is to pad the strings the right number of times, based on the parent key. I thought getting the indent + @indentation was a smart move, but I ended up in the case where the indent value is 0 when it turns out to print the string ( so it's reverse logic, rather tha growing ).

Is there a way to get the current depth?

The patch can be found here: EDITED

If you have a better idea on how to get this, I could then try to make a PR out of this.

//EDIT: Abandoning the ship.