From 390089511b476c1e409ed2519e66f0140713807f Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 9 May 2020 07:05:27 +0300 Subject: [PATCH] Also indent top-level scalars with lines starting with % directives (#162) --- src/stringify/stringifyString.js | 4 +++- tests/doc/stringify.js | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/stringify/stringifyString.js b/src/stringify/stringifyString.js index d98d826a..c7750c0f 100644 --- a/src/stringify/stringifyString.js +++ b/src/stringify/stringifyString.js @@ -14,7 +14,9 @@ const getFoldOptions = ({ indentAtStart }) => ? Object.assign({ indentAtStart }, strOptions.fold) : strOptions.fold -const containsDocumentMarker = str => /^(---|\.\.\.)/m.test(str) +// Also checks for lines starting with %, as parsing the output as YAML 1.1 will +// presume that's starting a new document. +const containsDocumentMarker = str => /^(%|---|\.\.\.)/m.test(str) function lineLengthOverLimit(str, limit) { const strLen = str.length diff --git a/tests/doc/stringify.js b/tests/doc/stringify.js index 94e13172..1e797419 100644 --- a/tests/doc/stringify.js +++ b/tests/doc/stringify.js @@ -671,4 +671,10 @@ describe('Document markers in top-level scalars', () => { expect(str).toBe('"foo\n\n ..."\n') expect(YAML.parse(str)).toBe('foo\n...') }) + + test('foo\\n%bar\\n', () => { + const str = YAML.stringify('foo\n%bar\n') + expect(str).toBe('|\n foo\n %bar\n') + expect(YAML.parse(str)).toBe('foo\n%bar\n') + }) })