Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/hjson-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ module.exports = function(data, opt) {
var i, a = string.replace(/\r/g, "").split('\n');
gap += indent;

if (a.length === 1) {
// The string contains only a single line. We still use the multiline
// format as it avoids escaping the \ character (e.g. when used in a
// regex).
if (a.length === 1 && a[0][0] !== "'" && a[0][a[0].length - 1] !== "'") {
// The string contains only a single line and does not touch the ''' at
// either end, so we can use the compact multiline format. This still
// avoids escaping the \ character (e.g. when used in a regex). If the
// line started or ended with a quote we'd get a run of four or more
// quotes where it meets the ''' delimiter, which can't be told apart
// from the closing delimiter again, so those cases fall through to the
// block form below where the delimiters sit on their own line.
return wrap(token.mstr, a[0]);
} else {
var res = eol + gap + token.mstr[0];
Expand Down
15 changes: 15 additions & 0 deletions test/assets/mlStrQuote1_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
bothQuotes:
'''
'Hello,\nWorld!'
'''
startQuote:
'''
'Hello,\nWorld!
'''
endQuote:
'''
#Hello,\nWorld!'
'''
safe: Hello,\nWorld!
}
6 changes: 6 additions & 0 deletions test/assets/mlStrQuote1_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bothQuotes": "'Hello,\\nWorld!'",
"startQuote": "'Hello,\\nWorld!",
"endQuote": "#Hello,\\nWorld!'",
"safe": "Hello,\\nWorld!"
}
6 changes: 6 additions & 0 deletions test/assets/mlStrQuote1_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bothQuotes": "'Hello,\\nWorld!'",
"startQuote": "'Hello,\\nWorld!",
"endQuote": "#Hello,\\nWorld!'",
"safe": "Hello,\\nWorld!"
}
1 change: 1 addition & 0 deletions test/assets/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ failStr7a_test.hjson
failStr8a_test.hjson
kan_test.hjson
keys_test.hjson
mlStrQuote1_test.json
mltabs_test.json
oa_test.hjson
pass1_test.json
Expand Down