Skip to content

Commit

Permalink
fix: Fix liquid conditions inside notes
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed May 17, 2024
1 parent e29b376 commit 668c1d4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/transform/liquid/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ function tailLinebreak(raw: string) {

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

content = content.substring(ifCon.start, ifCon.end);
Expand Down
74 changes: 74 additions & 0 deletions test/liquid/conditions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import conditions from '../../src/transform/liquid/conditions';

const getPadX = (string: string) => {
const match = /^(\s+)/.exec(string);
const pad = (match && match[1]) || '';

return pad.length || 0;
};

export function trim(string: string | TemplateStringsArray): string {
const strings = ([] as string[]).concat(string as string) as string[];
let lines: string[] = ([] as string[]).concat(...strings.map((string) => string.split('\n')));

let pad: number;
if (lines[0].trim() === '') {
pad = getPadX(lines[1]);
lines = lines.slice(1);
} else {
pad = getPadX(lines[0]);
}

lines = lines.map((line) => line.slice(pad));

return lines.join('\n').trim();
}

describe('Conditions', () => {
describe('location', () => {
test('Should works for if only', () => {
Expand Down Expand Up @@ -143,6 +167,56 @@ describe('Conditions', () => {
),
).toEqual('1. list item 1\n\n' + `${' '.repeat(4)}Test\n`);
});

test('Condition inside the note block (at start)', () => {
expect(
conditions(
trim`
{% note alert %}
{% if locale == 'ru' %}You can't use the public geofence names.{% endif %}Test
{% endnote %}
`,
{},
'',
{
sourceMap: {},
},
),
).toEqual(trim`
{% note alert %}
Test
{% endnote %}
`);
});

test('Condition inside the note block (at end)', () => {
expect(
conditions(
trim`
{% note alert %}
Test{% if locale == 'ru' %}You can't use the public geofence names.{% endif %}
{% endnote %}
`,
{},
'',
{
sourceMap: {},
},
),
).toEqual(trim`
{% note alert %}
Test
{% endnote %}
`);
});
});

describe('Conditions', () => {
Expand Down

0 comments on commit 668c1d4

Please sign in to comment.