Skip to content

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

Merged
harttle merged 1 commit into
harttle:masterfrom
youdie006:truncatewords-leading-whitespace
Sep 20, 2026
Merged

harttle merged 1 commit into
harttle:masterfrom
youdie006:truncatewords-leading-whitespace

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

truncatewords does str.split(/\s+/) (src/filters/string.ts:183). When the input starts with whitespace that yields a leading '' element, which gets counted as a word and joined back into the output:

{{ "  Ground control to Major Tom." | truncatewords: 3 }}
liquidjs -> " Ground control..."
Ruby     -> "Ground control to..."

So the result is one word short and carries a stray leading space. This shows up on CMS/HTML-derived text and {% capture %} output, which routinely start with whitespace.

trimStart rather than trim, because Ruby's awk-mode split(" ", n+1) discards leading whitespace but leaves trailing whitespace alone - {{ "1 2 3 " | truncatewords: 5 }} returns "1 2 3 " in Ruby, and trim would drop that space. Both test rows below cover that.

number_of_words is unaffected: it already calls .trim() at string.ts:199 before splitting.

Verification

Oracle measured against the liquid gem 5.13.0 on ruby:3.3:

input words Ruby liquidjs before
" Ground control to Major Tom." 3 "Ground control to..." " Ground control..."
"\t one two three four" 2 "one two..." " one..."
"1 2 3 " 5 "1 2 3 " "1 2 3 "

TZ=Asia/Shanghai npm test (the command test.yml runs): 89 suites, 1644 tests, all passing. npm run lint exit 0.

Reverting the one-line change fails the first new test (Expected: "Ground control to..." / Received: " Ground control..."); using trim() instead of trimStart() fails the second (Expected: "1 2 3 " / Received: "1 2 3").

I left the existing {{ "1 2 3" | truncatewords: 3 }} -> '1 2 3...' row alone; Ruby returns "1 2 3" there, but that ellipsis policy is pinned by your own test and is a separate question from this one.

I used Claude Code while working on this; the change and the tests are mine and I have been through them.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-15T03:46:16.081920Z a0d0349 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@harttle
harttle merged commit 65e200e into harttle:master Sep 20, 2026
14 checks passed
@harttle harttle added the bug label Sep 20, 2026
@harttle

harttle commented Sep 20, 2026

Copy link
Copy Markdown
Owner

@all-contributors please add @youdie006 for code

@allcontributors

Copy link
Copy Markdown
Contributor

@harttle

I've put up a pull request to add @youdie006! 🎉

github-actions Bot pushed a commit that referenced this pull request Oct 1, 2026
# [10.30.0](v10.29.0...v10.30.0) (2026-10-01)

### Bug Fixes

* **base64_encode:** encode large input in browser builds ([#969](#969)) ([8aace78](8aace78)), closes [#967](#967)
* **date:** compute %C and %y from the numeric year ([#961](#961)) ([e68514f](e68514f))
* **date:** compute %U and %W week numbers like strftime ([#958](#958)) ([0587ca8](0587ca8))
* trim custom characters by Unicode code point ([#955](#955)) ([3fe244f](3fe244f))
* **truncatewords:** ignore leading whitespace when counting words ([#954](#954)) ([65e200e](65e200e))
* **url_decode:** keep %2B as a literal plus when decoding ([#939](#939)) ([9af92f5](9af92f5))

### Features

* await promises returned by Drop.valueOf() ([#948](#948)) ([#953](#953)) ([#959](#959)) ([7dfa76b](7dfa76b))
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 10.30.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants