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

Ensure lineWidth=0 is respected for block literal vs folded #232

Merged
merged 1 commit into from
Mar 1, 2021
Merged
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
6 changes: 4 additions & 2 deletions src/stringify/stringifyString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const getFoldOptions = ({ indentAtStart }: StringifyContext) =>
// presume that's starting a new document.
const containsDocumentMarker = (str: string) => /^(%|---|\.\.\.)/m.test(str)

function lineLengthOverLimit(str: string, limit: number) {
function lineLengthOverLimit(str: string, lineWidth: number, indentLength: number) {
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 @@ -161,7 +163,7 @@ function blockString(
? 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 @@ -313,4 +313,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)
})
})