Skip to content

Commit 5aba59f

Browse files
committed
add useLegacyConditions
1 parent 0a7da00 commit 5aba59f

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

src/transform/liquid/conditions.ts

+49-6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ function tailLinebreak(raw: string) {
7676
return match ? match[1] : '';
7777
}
7878

79+
function legacyTrimResult(content: string, ifTag: IfTag, ifCon: IfCondition | null) {
80+
if (!ifCon) {
81+
return ifTag.isBlock ? '\n' : '';
82+
}
83+
84+
content = content.substring(ifCon.start, ifCon.end);
85+
86+
const head = headLinebreak(ifCon.rawStart);
87+
if (head) {
88+
content = (ifTag.isBlock ? '\n' : head) + content;
89+
}
90+
91+
const tail = tailLinebreak(ifCon.rawEnd);
92+
if (tail) {
93+
content = content + (ifTag.isBlock ? '\n' : tail);
94+
}
95+
96+
return content;
97+
}
98+
7999
function trimResult(content: string, ifTag: IfTag, ifCon: IfCondition | null) {
80100
if (!ifCon) {
81101
const head = headLinebreak(ifTag.rawStart);
@@ -208,6 +228,7 @@ function inlineConditions(
208228
ifTag: IfTag,
209229
vars: Record<string, unknown>,
210230
strict: boolean,
231+
useLegacyConditions: boolean,
211232
) {
212233
let ifCon = null;
213234

@@ -232,13 +253,22 @@ function inlineConditions(
232253

233254
const start = content.slice(0, ifTag.start);
234255
const end = content.slice(ifTag.end);
235-
const result = trimResult(content, ifTag, ifCon);
256+
let result;
257+
if (useLegacyConditions) {
258+
result = legacyTrimResult(content, ifTag, ifCon);
259+
} else {
260+
result = trimResult(content, ifTag, ifCon);
261+
}
236262

237-
return {
263+
const r = {
238264
result: start + result + end,
239-
lastIndex: start.length + result.length - tailLinebreak(ifTag.rawEnd).length,
265+
lastIndex: start.length + result.length,
240266
ifCon,
241267
};
268+
if (useLegacyConditions) {
269+
r.lastIndex -= tailLinebreak(ifTag.rawEnd).length;
270+
}
271+
return r;
242272
}
243273

244274
export = function conditions(
@@ -248,15 +278,20 @@ export = function conditions(
248278
settings?: {
249279
sourceMap: Record<number, number>;
250280
strict?: boolean;
281+
useLegacyConditions?: boolean;
251282
},
252283
) {
253284
const sourceMap = settings?.sourceMap || {};
254285
const strict = settings?.strict || false;
286+
const useLegacyConditions = settings?.useLegacyConditions || false;
255287
const tagStack: IfTag[] = [];
256288

257289
// Consumes all between curly braces
258290
// and all closest upon to first linebreak before and after braces.
259-
const R_LIQUID = /((?:\n[\t ]*)?{%-?([\s\S]*?)-?%}(?:[\t ]*\n)?)/g;
291+
let R_LIQUID = /((?:\n[^\n{]*)?{%-?([\s\S]*?)-?%}(?:\s*\n)?)/g;
292+
if (useLegacyConditions) {
293+
R_LIQUID = /((?:\n[\t ]*)?{%-?([\s\S]*?)-?%}(?:[\t ]*\n)?)/g;
294+
}
260295

261296
let match;
262297
while ((match = R_LIQUID.exec(input)) !== null) {
@@ -301,7 +336,13 @@ export = function conditions(
301336

302337
ifTag.closeCondition(match[1], match.index);
303338

304-
const {result, lastIndex, ifCon} = inlineConditions(input, ifTag, vars, strict);
339+
const {result, lastIndex, ifCon} = inlineConditions(
340+
input,
341+
ifTag,
342+
vars,
343+
strict,
344+
useLegacyConditions,
345+
);
305346

306347
resourcemap(input, ifTag, ifCon, createSourceMapApi(sourceMap));
307348

@@ -313,7 +354,9 @@ export = function conditions(
313354
default:
314355
// This is not condition.
315356
// Step back last linebreaks to match them on next condition
316-
R_LIQUID.lastIndex -= tailLinebreak(match[1]).length;
357+
if (useLegacyConditions) {
358+
R_LIQUID.lastIndex -= tailLinebreak(match[1]).length;
359+
}
317360
}
318361
}
319362

src/transform/liquid/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function liquid<
8080
conditions = true,
8181
substitutions = true,
8282
conditionsInCode = false,
83+
useLegacyConditions = false,
8384
keepNotVar = false,
8485
withSourceMap,
8586
} = settings || {};
@@ -89,6 +90,7 @@ function liquid<
8990
conditions,
9091
substitutions,
9192
conditionsInCode,
93+
useLegacyConditions,
9294
keepNotVar,
9395
withSourceMap,
9496
});
@@ -116,7 +118,7 @@ function liquid<
116118

117119
if (conditions) {
118120
const strict = conditions === 'strict';
119-
output = applyConditions(output, vars, path, {sourceMap, strict});
121+
output = applyConditions(output, vars, path, {sourceMap, strict, useLegacyConditions});
120122
}
121123

122124
if (substitutions) {

src/transform/liquid/services/argv.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type ArgvSettings = {
55
substitutions?: boolean;
66
keepNotVar?: boolean;
77
withSourceMap?: boolean;
8+
useLegacyConditions?: boolean;
89
};
910

1011
let _argv: ArgvSettings;

0 commit comments

Comments
 (0)