Skip to content

Commit

Permalink
Prefer literal over folded block scalar when lineWidth=0 is set (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
muff1nman authored and eemeli committed Mar 13, 2021
1 parent fd817be commit 7ddb18b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/stringify/stringifyString.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const getFoldOptions = ({ indentAtStart }) =>
// presume that's starting a new document.
const containsDocumentMarker = str => /^(%|---|\.\.\.)/m.test(str)

function lineLengthOverLimit(str, limit) {
function lineLengthOverLimit(str, lineWidth, indentLength) {
if (!lineWidth || lineWidth < 0) return false
const limit = lineWidth - indentLength
const strLen = str.length
if (strLen <= limit) return false
for (let i = 0, start = 0; i < strLen; ++i) {
Expand Down Expand Up @@ -151,7 +153,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
? false
: type === Type.BLOCK_LITERAL
? true
: !lineLengthOverLimit(value, strOptions.fold.lineWidth - indent.length)
: !lineLengthOverLimit(value, strOptions.fold.lineWidth, indent.length)
let header = literal ? '|' : '>'
if (!value) return header + '\n'
let wsStart = ''
Expand Down
22 changes: 22 additions & 0 deletions tests/doc/foldFlowLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,26 @@ Unfolded paragraph.\n`
expect(doc.contents.items[1].value).toBe('plain with comment')
expect(String(doc)).toBe(src)
})

test('long line width', () => {
const src = {
lorem: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.\n\nDuis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti. \n'
}

YAML.scalarOptions.str.fold.lineWidth = 1000
const ysWithLineWidthGreater = YAML.stringify(src)

YAML.scalarOptions.str.fold.lineWidth = 0
const ysWithUnlimitedLength = YAML.stringify(src)

YAML.scalarOptions.str.fold.lineWidth = -1
const ysWithUnlimitedLength2 = YAML.stringify(src)

expect(ysWithLineWidthGreater).toBe('lorem: |\n' +
' Lorem ipsum dolor sit amet, consectetur adipiscing elit. In laoreet massa eros, dignissim aliquam nunc elementum sit amet. Mauris pulvinar nunc eget ante sodales viverra. Vivamus quis convallis sapien, ut auctor magna. Cras volutpat erat eu lacus luctus facilisis. Aenean sapien leo, auctor sed tincidunt at, scelerisque a urna. Nunc ullamcorper, libero non mollis aliquet, nulla diam lobortis neque, ac rutrum dui nibh iaculis lectus. Aenean lobortis interdum arcu eget sollicitudin.\n' +
'\n' +
' Duis quam enim, ultricies a enim non, tincidunt lobortis ipsum. Mauris condimentum ultrices eros rutrum euismod. Fusce et mi eget quam dapibus blandit. Maecenas sodales tempor euismod. Phasellus vulputate purus felis, eleifend ullamcorper tortor semper sit amet. Sed nunc quam, iaculis et neque sit amet, consequat egestas lectus. Aenean dapibus lorem sed accumsan porttitor. Curabitur eu magna congue, mattis urna ac, lacinia eros. In in iaculis justo, nec faucibus enim. Fusce id viverra purus, nec ultricies mi. Aliquam eu risus risus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse potenti. \n')
expect(ysWithUnlimitedLength).toBe(ysWithLineWidthGreater)
expect(ysWithUnlimitedLength2).toBe(ysWithLineWidthGreater)
})
})

0 comments on commit 7ddb18b

Please sign in to comment.