Skip to content

Commit ceba8e6

Browse files
grgcnnrascorbic
andauthored
fix(storyblok): update splitfilters in storyblok provider to not split on colons inside focal() filter (#171)
* update splitfilters in storyblok provider to not split on colons inside focal() filter * move regex out of fn to top of scope. * tidy whitespace * Fix splitfilters in Storyblok provider Update the splitfilters logic in the Storyblok provider to prevent splitting on colons within the focal() filter. --------- Co-authored-by: Matt Kane <m@mk.gg>
1 parent 394ee12 commit ceba8e6

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

.changeset/rude-ads-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"unpic": patch
3+
---
4+
5+
fix(storyblok): update splitfilters in storyblok provider to not split on colons inside focal() filter

src/providers/storyblok.test.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ const OLD_BASE_URL =
88

99
Deno.test("Storyblok Image CDN - extract", async (t) => {
1010
await t.step("should extract operations from new URL format", () => {
11-
const url = `${NEW_BASE_URL}/m/400x300/filters:format(webp)`;
11+
const url = `${NEW_BASE_URL}/m/400x300/filters:format(webp):focal(150x100:250x200)`;
1212
const result = extract(url);
1313
assertEquals(result, {
1414
src: NEW_BASE_URL,
1515
operations: {
1616
width: 400,
1717
height: 300,
1818
format: "webp",
19-
filters: {},
19+
filters: {
20+
focal: "150x100:250x200",
21+
},
2022
},
2123
});
2224
});
@@ -47,6 +49,8 @@ Deno.test("Storyblok Image CDN - extract", async (t) => {
4749
},
4850
});
4951
});
52+
53+
5054
});
5155

5256
Deno.test("Storyblok Image CDN - generate", async (t) => {
@@ -88,6 +92,17 @@ Deno.test("Storyblok Image CDN - generate", async (t) => {
8892
});
8993
assertEquals(result, `${NEW_BASE_URL}/m/-400x-300`);
9094
});
95+
96+
await t.step("should generate URL with focal point", () => {
97+
const result = generate(NEW_BASE_URL, {
98+
width: 400,
99+
height: 300,
100+
filters:{
101+
focal: "150x150:250x250"
102+
}
103+
});
104+
assertEquals(result, `${NEW_BASE_URL}/m/400x300/filters:focal(150x150:250x250)`);
105+
});
91106
});
92107

93108
Deno.test("Storyblok Image CDN - transform", async (t) => {
@@ -96,10 +111,13 @@ Deno.test("Storyblok Image CDN - transform", async (t) => {
96111
width: 500,
97112
height: 400,
98113
format: "webp",
114+
filters:{
115+
focal: "150x150:250x250"
116+
}
99117
});
100118
assertEquals(
101119
result,
102-
`${NEW_BASE_URL}/m/500x400/filters:format(webp)`,
120+
`${NEW_BASE_URL}/m/500x400/filters:focal(150x150:250x250):format(webp)`,
103121
);
104122
});
105123

@@ -124,3 +142,5 @@ Deno.test("Storyblok Image CDN - transform", async (t) => {
124142
);
125143
});
126144
});
145+
146+

src/providers/storyblok.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const storyBlokAssets =
1616
const storyBlokImg2 =
1717
/^(?<modifiers>\/(?<crop>\d+x\d+:\d+x\d+)?\/?(?<resize>(?<flipx>\-)?(?<width>\d+)x(?<flipy>\-)?(?<height>\d+))?\/?(filters\:(?<filters>[^\/]+))?\/?)?(?<id>\/f\/.+)$/;
1818

19+
// This regex selects every colon(:) that is not inside parentheses. So that focal(150x150:250x250) is not split into two filters.
20+
const filterSplitterRegex = /:(?![^(]*\))/;
21+
1922
export interface StoryblokOperations extends Operations {
2023
crop?: string;
2124
filters?: Record<string, string>;
@@ -27,8 +30,9 @@ const splitFilters = (filters: string): Record<string, string> => {
2730
if (!filters) {
2831
return {};
2932
}
33+
3034
return Object.fromEntries(
31-
filters.split(":").map((filter) => {
35+
filters.split(filterSplitterRegex).map((filter) => {
3236
if (!filter) return [];
3337
const [key, value] = filter.split("(");
3438
return [key, value.replace(")", "")];

0 commit comments

Comments
 (0)