Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How to preserve whitespace/newlines from imported yaml #282

Closed
mcky opened this issue Apr 29, 2016 · 3 comments
Closed

[QUESTION] How to preserve whitespace/newlines from imported yaml #282

mcky opened this issue Apr 29, 2016 · 3 comments

Comments

@mcky
Copy link

mcky commented Apr 29, 2016

Sorry if this isn't the appropriate place to ask, feel free to close and direct me elsewhere.

I have a nicely formatted yaml file with comments and newlines separating sections, that I want to parse, edit with js (via a UI) and save back out. The problem is that the package strips all that stuff (rightly so).

I was wondering if you could provide any suggestions for handling this problem. I'm happy to take a stab at a PR to add an extra option if anyone else would find it useful.

For now I'm playing around with adding another key to the object to store line numbers of blank lines and comments, then adding them back into the string post-dump, but this is far from ideal (and will break with arrays etc)

@dervus
Copy link
Collaborator

dervus commented Apr 29, 2016

It's impossible with JS-YAML, sorry. To do something like this you need a parser that can provide full AST of input document, apply patch to it, and save it back. JS-YAML 1.x.x was able to produce AST, but had no patch nor dump functionality. If you have much desire, you can try to fork 1.x.x branch and implement what you need.

@dervus dervus closed this as completed Apr 29, 2016
@dervus
Copy link
Collaborator

dervus commented Apr 29, 2016

Also take a look at bindings for libyaml. I guess libyaml should have needed functionality out of the box.

@mcky
Copy link
Author

mcky commented May 3, 2016

Thanks for the quick reply and reasoning. For now I'll stick to the current js-yaml as I'm sure all the extra gains from v1-3 outweigh the inconveniences. I'll certainly have a poke around libyaml later on though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants