diff --git a/src/filters/string.ts b/src/filters/string.ts index 84eb34dd8..6cfb56eec 100644 --- a/src/filters/string.ts +++ b/src/filters/string.ts @@ -180,7 +180,7 @@ export function truncatewords (this: FilterImpl, v: string, words = 15, o = '... const str = stringify(v) o = stringify(o) this.context.memoryLimit.use(str.length + o.length) - const arr = str.split(/\s+/) + const arr = str.trimStart().split(/\s+/) if (words <= 0) words = 1 let ret = arr.slice(0, words).join(' ') if (arr.length >= words) ret += o diff --git a/test/integration/filters/string.spec.ts b/test/integration/filters/string.spec.ts index 7f6d271f7..c30c8976e 100644 --- a/test/integration/filters/string.spec.ts +++ b/test/integration/filters/string.spec.ts @@ -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 () {