When using literal multiline block strings inside my YAML file, converting to JS, and then back, sometimes the multiline literal block survives, but not always.
Is there a way to force a particular behavior? Having them be literal block strings inside the YAML is, IMHO, definitely more readable and editable by humans.
An example program:
const yaml = require('js-yaml')
const bad = `
abc:
- name: xxx
sample: |
{
"abc": 2,
"def" : {
"efg" : {
"ok" : 6666,
"nope" : "./yyy",
`.trim() // make sure the whitespace is not the difference
const good = `
abc:
- name: xxx
sample: |
{
"abc": 2,
"def" : {
`.trim() // make sure the whitespace is not the difference
const badDoc = yaml.safeLoad(bad)
const goodDoc = yaml.safeLoad(good)
const goodDumped = yaml.safeDump(goodDoc)
if( goodDumped === good ) {
console.log( "Round trip was good for GOOD file\n" )
}
const badDumped = yaml.safeDump(badDoc).trim()
if( badDumped !== bad ) {
console.log( "Round trip was bad for BAD file" )
console.log( "INPUT: ", bad, "\n\n" )
console.log( "OUTPUT: ", badDumped,"\n\n" )
}
else {
console.log( badDumped, bad )
}
The output of this is:
Round trip was good for GOOD file
Round trip was bad for BAD file
INPUT: abc:
- name: xxx
sample: |
{
"abc": 2,
"def" : {
"efg" : {
"ok" : 6666,
"nope" : "./yyy",
OUTPUT: abc:
- name: xxx
sample: "{\n \"abc\": 2,\n \"def\" : {\n \"efg\" : {\n\t\"ok\" : 6666,\n\t\"nope\" : \"./yyy\",\n"
Note that sample has been turned into a string with newlines encoded, rather than a literal multiline block.
It seems strange that the shorter version (good) does preserve literal block style, but the longer one does not. Am I missing something subtle about using the library?
(IMHO, it would seem more usable to have shorter strings drop the literal multiline block, while longer ones always use the | format, but the library works the opposite way.)
I would really like a way to always have multiline values (any strings with newlines) to be output as literal multiline block strings so that I have the option to edit them by hand easily.
I'm using version "js-yaml": "^3.12.1"
When using literal multiline block strings inside my YAML file, converting to JS, and then back, sometimes the multiline literal block survives, but not always.
Is there a way to force a particular behavior? Having them be literal block strings inside the YAML is, IMHO, definitely more readable and editable by humans.
An example program:
The output of this is:
Note that sample has been turned into a string with newlines encoded, rather than a literal multiline block.
It seems strange that the shorter version (good) does preserve literal block style, but the longer one does not. Am I missing something subtle about using the library?
(IMHO, it would seem more usable to have shorter strings drop the literal multiline block, while longer ones always use the | format, but the library works the opposite way.)
I would really like a way to always have multiline values (any strings with newlines) to be output as literal multiline block strings so that I have the option to edit them by hand easily.
I'm using version
"js-yaml": "^3.12.1"