Skip to content

Commit f28373b

Browse files
committed
Two-step regex optimization
1 parent bb061b9 commit f28373b

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/theme/src/cli/utilities/theme-environment/liquid-tag-content.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,29 @@
2626
* @param tag - The Liquid tag to extract content from.
2727
*
2828
* @returns The tag content, or undefined if not found.
29-
*
3029
*/
3130
export function getLiquidTagContent(liquid: string, tag: 'javascript' | 'stylesheet' | 'schema'): string | undefined {
32-
const tagRE = new RegExp(`{%-?\\s*${tag}\\s*-?%}([^%]*){%-?\\s*end${tag}\\s*-?%}`)
33-
const match = liquid.match(tagRE)?.[1]
34-
return match
31+
const openTagRE = new RegExp(`{%-?\\s*${tag}\\s*-?%}`)
32+
const closeTagRE = new RegExp(`{%-?\\s*end${tag}\\s*-?%}`)
33+
34+
const openMatch = openTagRE.exec(liquid)
35+
36+
if (!openMatch) {
37+
return
38+
}
39+
40+
const startIndex = openMatch.index + openMatch[0].length
41+
const closeMatch = closeTagRE.exec(liquid)
42+
43+
if (!closeMatch) {
44+
return
45+
}
46+
47+
const endIndex = closeMatch.index
48+
49+
if (startIndex > endIndex) {
50+
return
51+
}
52+
53+
return liquid.slice(startIndex, endIndex)
3554
}

0 commit comments

Comments
 (0)