From 30354ae79c6c17e47a13e821b8494796045dbc87 Mon Sep 17 00:00:00 2001 From: MildlyMeticulous <302576729+MildlyMeticulous@users.noreply.github.com> Date: Tue, 28 Jul 2026 14:20:51 +0100 Subject: [PATCH] feat(filters): add squish filter --- docs/source/_data/sidebar.yml | 1 + docs/source/filters/squish.md | 18 ++++++++++++++++++ src/filters/string.ts | 6 ++++++ test/integration/filters/string.spec.ts | 20 ++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 docs/source/filters/squish.md diff --git a/docs/source/_data/sidebar.yml b/docs/source/_data/sidebar.yml index 97f80ed265..97e173f435 100644 --- a/docs/source/_data/sidebar.yml +++ b/docs/source/_data/sidebar.yml @@ -94,6 +94,7 @@ filters: sort: sort.html sort_natural: sort_natural.html split: split.html + squish: squish.html strip: strip.html strip_html: strip_html.html strip_newlines: strip_newlines.html diff --git a/docs/source/filters/squish.md b/docs/source/filters/squish.md new file mode 100644 index 0000000000..518c34b66a --- /dev/null +++ b/docs/source/filters/squish.md @@ -0,0 +1,18 @@ +--- +title: squish +--- + +{% since %}v10.28.0{% endsince %} + +Removes leading and trailing whitespace from a string, and replaces every run of whitespace inside it with a single space. + +Input +```liquid +{{ " Hello there, + Major Tom. " | squish }} +``` + +Output +```text +Hello there, Major Tom. +``` diff --git a/src/filters/string.ts b/src/filters/string.ts index d0c59708ed..84eb34dd88 100644 --- a/src/filters/string.ts +++ b/src/filters/string.ts @@ -128,6 +128,12 @@ export function strip_newlines (this: FilterImpl, v: string) { return str.replace(/\r?\n/gm, '') } +export function squish (this: FilterImpl, v: string) { + const str = stringify(v) + this.context.memoryLimit.use(str.length) + return str.replace(/\s+/g, ' ').trim() +} + export function capitalize (this: FilterImpl, str: string) { str = stringify(str) this.context.memoryLimit.use(str.length) diff --git a/test/integration/filters/string.spec.ts b/test/integration/filters/string.spec.ts index 1f0a93a33e..7f6d271f79 100644 --- a/test/integration/filters/string.spec.ts +++ b/test/integration/filters/string.spec.ts @@ -153,6 +153,26 @@ describe('filters/string', function () { '{{ string_with_newlines | strip_newlines }}', 'Hellothere') }) + describe('squish', function () { + it('should collapse whitespace between words', function () { + return test('{{ "Hello World!" | squish }}', 'Hello World!') + }) + it('should strip leading and trailing whitespace', function () { + return test('{{ " HelloWorld! " | squish }}', 'HelloWorld!') + }) + it('should treat newlines and tabs as whitespace', function () { + return test('{{ " \n\t\r\nHello \n\t World! \n" | squish }}', 'Hello World!') + }) + it('should return empty string for whitespace only', function () { + return test('{{ " \n\t " | squish }}', '') + }) + it('should stringify a number', function () { + return test('{{ 5 | squish }}', '5') + }) + it('should return empty string for undefined', function () { + return test('{{ nosuchthing | squish }}', '') + }) + }) describe('truncate', function () { it('should truncate when string too long', function () { return test('{{ "Ground control to Major Tom." | truncate: 20 }}',