Skip to content

Commit 668c1d4

Browse files
committed
fix: Fix liquid conditions inside notes
1 parent e29b376 commit 668c1d4

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/transform/liquid/conditions.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ function tailLinebreak(raw: string) {
7676

7777
function trimResult(content: string, ifTag: IfTag, ifCon: IfCondition | null) {
7878
if (!ifCon) {
79-
return ifTag.isBlock ? '\n' : '';
79+
const head = headLinebreak(ifTag.rawStart);
80+
const tail = tailLinebreak(ifTag.rawEnd);
81+
return ifTag.isBlock ? '\n' : head + tail;
8082
}
8183

8284
content = content.substring(ifCon.start, ifCon.end);

test/liquid/conditions.test.ts

+74
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import conditions from '../../src/transform/liquid/conditions';
22

3+
const getPadX = (string: string) => {
4+
const match = /^(\s+)/.exec(string);
5+
const pad = (match && match[1]) || '';
6+
7+
return pad.length || 0;
8+
};
9+
10+
export function trim(string: string | TemplateStringsArray): string {
11+
const strings = ([] as string[]).concat(string as string) as string[];
12+
let lines: string[] = ([] as string[]).concat(...strings.map((string) => string.split('\n')));
13+
14+
let pad: number;
15+
if (lines[0].trim() === '') {
16+
pad = getPadX(lines[1]);
17+
lines = lines.slice(1);
18+
} else {
19+
pad = getPadX(lines[0]);
20+
}
21+
22+
lines = lines.map((line) => line.slice(pad));
23+
24+
return lines.join('\n').trim();
25+
}
26+
327
describe('Conditions', () => {
428
describe('location', () => {
529
test('Should works for if only', () => {
@@ -143,6 +167,56 @@ describe('Conditions', () => {
143167
),
144168
).toEqual('1. list item 1\n\n' + `${' '.repeat(4)}Test\n`);
145169
});
170+
171+
test('Condition inside the note block (at start)', () => {
172+
expect(
173+
conditions(
174+
trim`
175+
{% note alert %}
176+
177+
{% if locale == 'ru' %}You can't use the public geofence names.{% endif %}Test
178+
179+
{% endnote %}
180+
`,
181+
{},
182+
'',
183+
{
184+
sourceMap: {},
185+
},
186+
),
187+
).toEqual(trim`
188+
{% note alert %}
189+
190+
Test
191+
192+
{% endnote %}
193+
`);
194+
});
195+
196+
test('Condition inside the note block (at end)', () => {
197+
expect(
198+
conditions(
199+
trim`
200+
{% note alert %}
201+
202+
Test{% if locale == 'ru' %}You can't use the public geofence names.{% endif %}
203+
204+
{% endnote %}
205+
`,
206+
{},
207+
'',
208+
{
209+
sourceMap: {},
210+
},
211+
),
212+
).toEqual(trim`
213+
{% note alert %}
214+
215+
Test
216+
217+
{% endnote %}
218+
`);
219+
});
146220
});
147221

148222
describe('Conditions', () => {

0 commit comments

Comments
 (0)