|
1 | 1 | import conditions from '../../src/transform/liquid/conditions';
|
2 | 2 |
|
| 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 | + |
3 | 27 | describe('Conditions', () => {
|
4 | 28 | describe('location', () => {
|
5 | 29 | test('Should works for if only', () => {
|
@@ -143,6 +167,56 @@ describe('Conditions', () => {
|
143 | 167 | ),
|
144 | 168 | ).toEqual('1. list item 1\n\n' + `${' '.repeat(4)}Test\n`);
|
145 | 169 | });
|
| 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 | + }); |
146 | 220 | });
|
147 | 221 |
|
148 | 222 | describe('Conditions', () => {
|
|
0 commit comments