Skip to content

Commit

Permalink
Stop folding more-indented lines at start of folded block scalars (Fixes
Browse files Browse the repository at this point in the history
 #55)
  • Loading branch information
eemeli committed Dec 11, 2018
1 parent f17e7b8 commit bf9947c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
6 changes: 6 additions & 0 deletions __tests__/foldFlowLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ Unfolded paragraph.\n`
expect(String(doc)).toBe(src)
})

test('eemeli/yaml#55', () => {
const str = ' first more-indented line\nnext line\n'
const ys = YAML.stringify(str)
expect(ys).toBe('>1\n first more-indented line\nnext line\n')
})

test('plain string', () => {
const src = `- plain value with
enough length to
Expand Down
31 changes: 20 additions & 11 deletions src/foldFlowLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ export const FOLD_FLOW = 'flow'
export const FOLD_BLOCK = 'block'
export const FOLD_QUOTED = 'quoted'

// presumes i+1 is at the start of a line
// returns index of last newline in more-indented block
const consumeMoreIndentedLines = (text, i) => {
let ch = text[i + 1]
while (ch === ' ' || ch === '\t') {
do {
ch = text[(i += 1)]
} while (ch && ch !== '\n')
ch = text[i + 1]
}
return i
}

/**
* Tries to keep input at up to `lineWidth` characters, splitting only on spaces
* not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
Expand Down Expand Up @@ -39,7 +52,12 @@ export default function foldFlowLines(
let split = undefined
let prev = undefined
let overflow = false
for (let i = 0, ch = text[0]; ch; ch = text[(i += 1)]) {
let i = -1
if (mode === FOLD_BLOCK) {
i = consumeMoreIndentedLines(text, i)
if (i !== -1) end = i + endStep
}
for (let ch; (ch = text[(i += 1)]); ) {
if (mode === FOLD_QUOTED && ch === '\\') {
switch (text[i + 1]) {
case 'x':
Expand All @@ -56,16 +74,7 @@ export default function foldFlowLines(
}
}
if (ch === '\n') {
if (mode === FOLD_BLOCK) {
// more-indented lines in blocks can't be folded
let next = text[i + 1]
while (next === ' ' || next === '\t') {
do {
ch = text[(i += 1)]
} while (ch && ch !== '\n')
next = text[i + 1]
}
}
if (mode === FOLD_BLOCK) i = consumeMoreIndentedLines(text, i)
end = i + endStep
split = undefined
} else {
Expand Down

0 comments on commit bf9947c

Please sign in to comment.