fix(truncatewords): ignore leading whitespace when counting words (#954)

str.split(/\s+/) yields a leading empty element when the input starts with
whitespace, so that element was counted as a word and joined back into the
output: "  Ground control to Major Tom." truncated to 3 words returned
" Ground control..." instead of "Ground control to...".

Ruby Liquid splits in awk mode, which discards leading whitespace but keeps
trailing whitespace, so trimStart matches it and trim would not.
This commit is contained in:
KBS
2026-09-20 20:06:10 +08:00
committed by GitHub
parent 39233ba9f2
commit 65e200e22d
2 changed files with 7 additions and 1 deletions
+6
View File
@@ -233,6 +233,12 @@ describe('filters/string', function () {
it('should default len to 15', function () {
return test('{{ "1 2 3 4 5 6 7 8 9 a b c d e f" | truncatewords }}', '1 2 3 4 5 6 7 8 9 a b c d e f...')
})
it('should ignore leading whitespace when counting words', function () {
return test('{{ " Ground control to Major Tom." | truncatewords: 3 }}', 'Ground control to...')
})
it('should keep trailing whitespace when not truncating', function () {
return test('{{ "1 2 3 " | truncatewords: 5 }}', '1 2 3 ')
})
})
describe('remove_last', function () {
it('should remove the last occurrence of substring', function () {