Skip to content

Commit 71ae935

Browse files
committed
fix: save liquid conditions on true
1 parent ec61154 commit 71ae935

File tree

8 files changed

+230
-16
lines changed

8 files changed

+230
-16
lines changed

src/commands/translate/commands/extract.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {ok} from 'node:assert';
66
import {join, resolve} from 'node:path';
77
import {pick} from 'lodash';
88
import {asyncify, eachLimit} from 'async';
9-
import liquid from '@diplodoc/transform/lib/liquid';
109
// @ts-ignore
1110
import {Xliff} from '@diplodoc/translation/lib/experiment/xliff/xliff';
1211

@@ -214,7 +213,7 @@ export type PipelineParameters = {
214213
};
215214

216215
function pipeline(params: PipelineParameters) {
217-
const {input, output, source, target, vars, useExperimentalParser, schema} = params;
216+
const {input, output, source, target, useExperimentalParser, schema} = params;
218217
const inputRoot = resolve(input);
219218
const outputRoot = resolve(output);
220219

@@ -230,14 +229,6 @@ function pipeline(params: PipelineParameters) {
230229
return join(outputRoot, targetPath);
231230
};
232231

233-
if (Object.keys(vars).length && typeof content === 'string') {
234-
content = liquid(content, vars, inputPath, {
235-
conditions: 'strict',
236-
substitutions: false,
237-
cycles: false,
238-
});
239-
}
240-
241232
const {schemas, ajvOptions} = await resolveSchemas({
242233
content,
243234
path,

src/commands/translate/utils/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ export function configDefaults() {
194194
template: {
195195
enabled: true,
196196
features: {
197-
conditions: true,
198-
substitutions: true,
197+
conditions: 'strict',
198+
substitutions: false,
199199
},
200200
scopes: {
201201
code: false,

src/core/markdown/MarkdownService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type MarkdownServiceConfig = {
2929
enabled: boolean;
3030
features: {
3131
substitutions: boolean;
32-
conditions: boolean;
32+
conditions?: boolean | 'strict';
3333
};
3434
scopes: {
3535
code: boolean;
@@ -396,6 +396,7 @@ export class MarkdownService {
396396
conditions: this.config.template.features.conditions,
397397
conditionsInCode: this.config.template.scopes.code,
398398
keepNotVar: this.config.outputFormat === 'md',
399+
keepConditionSyntaxOnTrue: this.options.mode === 'translate',
399400
},
400401
options: {
401402
disableLiquid: !this.config.template.enabled,
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {LoaderContext} from '../loader';
22

3-
import {liquidSnippet} from '@diplodoc/liquid';
3+
import {liquidDocument, liquidSnippet} from '@diplodoc/liquid';
44

55
export function templateContent(this: LoaderContext, rawContent: string) {
66
const {vars, options} = this;
@@ -10,7 +10,13 @@ export function templateContent(this: LoaderContext, rawContent: string) {
1010
return rawContent;
1111
}
1212

13-
const content = liquidSnippet.call(this, rawContent, vars, this.sourcemap);
13+
let content;
14+
15+
if (this.mode === 'translate') {
16+
content = liquidDocument.call(this, rawContent, vars);
17+
} else {
18+
content = liquidSnippet.call(this, rawContent, vars, this.sourcemap);
19+
}
1420

1521
return content;
1622
}

tests/e2e/__snapshots__/translation.spec.ts.snap

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,60 @@ exports[`Translate command > filter files on extract with extra vars option > fi
18111811
]"
18121812
`;
18131813

1814+
exports[`Translate command > remove falsy liquid conditions structures > filelist 1`] = `
1815+
"[
1816+
"index.md.skl",
1817+
"index.md.xliff"
1818+
]"
1819+
`;
1820+
1821+
exports[`Translate command > remove falsy liquid conditions structures 1`] = `
1822+
"%%%0%%%
1823+
<!-- [missed file](./missed.md) -->
1824+
1825+
1826+
1827+
1828+
#### %%%1%%%
1829+
1830+
{% if list contains "item" %}
1831+
1832+
%%%2%%%
1833+
1834+
{% endif %}
1835+
1836+
#### %%%3%%%
1837+
1838+
"
1839+
`;
1840+
1841+
exports[`Translate command > remove falsy liquid conditions structures 2`] = `
1842+
"<?xml version="1.0" encoding="UTF-8"?>
1843+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
1844+
<file original="file.ext" source-language="ru-RU" target-language="es-ES" datatype="markdown">
1845+
<header>
1846+
<skeleton>
1847+
<external-file href="file.skl"></external-file>
1848+
</skeleton>
1849+
</header>
1850+
<body>
1851+
<trans-unit id="0">
1852+
<source xml:space="preserve"><g ctype="link" equiv-text="[{{text}}](./exists.md)" id="g-1" x-begin="[" x-end="](./exists.md)">existing file</g></source>
1853+
</trans-unit>
1854+
<trans-unit id="1">
1855+
<source xml:space="preserve">Standalone contains condition</source>
1856+
</trans-unit>
1857+
<trans-unit id="2">
1858+
<source xml:space="preserve">#### List</source>
1859+
</trans-unit>
1860+
<trans-unit id="3">
1861+
<source xml:space="preserve">Inline contains condition</source>
1862+
</trans-unit>
1863+
</body>
1864+
</file>
1865+
</xliff>"
1866+
`;
1867+
18141868
exports[`Translate command > removes no-translate directive and leaves content as is > filelist 1`] = `
18151869
"[
18161870
"index.md.skl",
@@ -2077,6 +2131,105 @@ exports[`Translate command > removes no-translate directive and leaves content a
20772131
</xliff>"
20782132
`;
20792133

2134+
exports[`Translate command > save truthy liquid conditions structures > filelist 1`] = `
2135+
"[
2136+
"index.md.skl",
2137+
"index.md.xliff"
2138+
]"
2139+
`;
2140+
2141+
exports[`Translate command > save truthy liquid conditions structures 1`] = `
2142+
"%%%0%%%
2143+
<!-- [missed file](./missed.md) -->
2144+
{% if prod == true %}%%%1%%%{% endif %}
2145+
2146+
{% if inner == true %}%%%2%%%{% endif %}
2147+
2148+
{% if prod == true %}%%%3%%%{% endif %}{% endif %}
2149+
2150+
{% if prod == true %}
2151+
2152+
{% if list contains "item" %}
2153+
2154+
%%%4%%%
2155+
2156+
{% if item == true %}%%%5%%% %%%6%%% {% endif %}
2157+
2158+
%%%7%%%
2159+
2160+
{% endif %}
2161+
2162+
{% endif %}
2163+
2164+
#### %%%8%%%
2165+
2166+
{% if list contains "item" %}
2167+
2168+
%%%9%%%
2169+
2170+
{% endif %}
2171+
2172+
#### %%%10%%%
2173+
2174+
{% if prod == true %}
2175+
2176+
%%%11%%% {% endif %}
2177+
2178+
{% endif %}
2179+
"
2180+
`;
2181+
2182+
exports[`Translate command > save truthy liquid conditions structures 2`] = `
2183+
"<?xml version="1.0" encoding="UTF-8"?>
2184+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
2185+
<file original="file.ext" source-language="ru-RU" target-language="es-ES" datatype="markdown">
2186+
<header>
2187+
<skeleton>
2188+
<external-file href="file.skl"></external-file>
2189+
</skeleton>
2190+
</header>
2191+
<body>
2192+
<trans-unit id="0">
2193+
<source xml:space="preserve"><g ctype="link" equiv-text="[{{text}}](./exists.md)" id="g-1" x-begin="[" x-end="](./exists.md)">existing file</g></source>
2194+
</trans-unit>
2195+
<trans-unit id="1">
2196+
<source xml:space="preserve">Test text</source>
2197+
</trans-unit>
2198+
<trans-unit id="2">
2199+
<source xml:space="preserve">inner test text</source>
2200+
</trans-unit>
2201+
<trans-unit id="3">
2202+
<source xml:space="preserve">Test text<x ctype="liquid_Literal" equiv-text="{% if inner == true %}" id="x-1"/>inner test text</source>
2203+
</trans-unit>
2204+
<trans-unit id="4">
2205+
<source xml:space="preserve">#### List</source>
2206+
</trans-unit>
2207+
<trans-unit id="5">
2208+
<source xml:space="preserve">1.</source>
2209+
</trans-unit>
2210+
<trans-unit id="6">
2211+
<source xml:space="preserve">Item</source>
2212+
</trans-unit>
2213+
<trans-unit id="7">
2214+
<source xml:space="preserve">Some text</source>
2215+
</trans-unit>
2216+
<trans-unit id="8">
2217+
<source xml:space="preserve">Standalone contains condition</source>
2218+
</trans-unit>
2219+
<trans-unit id="9">
2220+
<source xml:space="preserve">#### List</source>
2221+
</trans-unit>
2222+
<trans-unit id="10">
2223+
<source xml:space="preserve">Inline contains condition</source>
2224+
</trans-unit>
2225+
<trans-unit id="11">
2226+
<source xml:space="preserve">#### List <x ctype="liquid_Literal" equiv-text="{% if list contains &quot;item&quot; %}" id="x-2"/> sub text</source>
2227+
</trans-unit>
2228+
</body>
2229+
</file>
2230+
</xliff>"
2231+
`;
2232+
20802233
exports[`Translate command > skip no-translate marked content > filelist 1`] = `
20812234
"[
20822235
"index.md.skl",

tests/e2e/translation.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,30 @@ describe('Translate command', () => {
108108
source: 'ru-RU',
109109
target: 'en-US',
110110
});
111+
112+
let conditionVars = {prod: true, inner: true, list: ['item']};
113+
generateMapTestTemplate(
114+
'save truthy liquid conditions structures',
115+
'mocks/translation/conditions',
116+
{
117+
subcommand: 'extract',
118+
source: 'ru-RU',
119+
target: 'es-ES',
120+
additionalArgs: `--vars ${JSON.stringify(conditionVars)}`,
121+
},
122+
false,
123+
);
124+
125+
conditionVars = {prod: false, inner: false, list: ['item']};
126+
generateMapTestTemplate(
127+
'remove falsy liquid conditions structures',
128+
'mocks/translation/conditions',
129+
{
130+
subcommand: 'extract',
131+
source: 'ru-RU',
132+
target: 'es-ES',
133+
additionalArgs: `--vars ${JSON.stringify(conditionVars)}`,
134+
},
135+
false,
136+
);
111137
});

tests/mocks/errors/extract-filtered-link/input/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
[second missed link](./filtered2.md)
66
[second missed link](./filtered2.md)
7-
[second missed link](./filtered3.md)
7+
[second missed link](./filtered3.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[existing file](./exists.md)
2+
<!-- [missed file](./missed.md) -->
3+
{% if prod == true %}Test text{% endif %}
4+
5+
{% if inner == true %}inner test text{% endif %}
6+
7+
{% if prod == true %}Test text{% if inner == true %}inner test text{% endif %}{% endif %}
8+
9+
{% if prod == true %}
10+
11+
{% if list contains "item" %}
12+
13+
#### List
14+
15+
{% if item == true %}1. Item {% endif %}
16+
17+
Some text
18+
19+
{% endif %}
20+
21+
{% endif %}
22+
23+
#### Standalone contains condition
24+
25+
{% if list contains "item" %}
26+
27+
#### List
28+
29+
{% endif %}
30+
31+
#### Inline contains condition
32+
33+
{% if prod == true %}
34+
35+
#### List {% if list contains "item" %} sub text {% endif %}
36+
37+
{% endif %}

0 commit comments

Comments
 (0)