Skip to content

Commit

Permalink
Don't append extra newline when using |indent filter
Browse files Browse the repository at this point in the history
Fixes #1231
  • Loading branch information
fdintino committed Jul 19, 2020
1 parent 73a4eb3 commit 3071f42
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions nunjucks/src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function indent(str, width, indentfirst) {
const sp = lib.repeat(' ', width);

const res = lines.map((l, i) => {
return (i === 0 && !indentfirst) ? `${l}\n` : `${sp}${l}\n`;
}).join('');
return (i === 0 && !indentfirst) ? l : `${sp}${l}`;
}).join('\n');

return r.copySafeness(str, res);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@

it('indent', function(done) {
equal('{{ "one\ntwo\nthree" | indent }}',
'one\n two\n three\n');
'one\n two\n three');
equal('{{ "one\ntwo\nthree" | indent(2) }}',
'one\n two\n three\n');
'one\n two\n three');
equal('{{ "one\ntwo\nthree" | indent(2, true) }}',
' one\n two\n three\n');
' one\n two\n three');

equal('{{ str | indent }}', {
str: r.markSafe('one\ntwo\nthree')
}, 'one\n two\n three\n');
}, 'one\n two\n three');

equal('{{ "" | indent }}', '');
equal('{{ undefined | indent }}', '');
Expand Down

0 comments on commit 3071f42

Please # to comment.