@diplodoc/liquid 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/lib/frontmatter.d.ts +6 -0
- package/lib/frontmatter.js +80 -0
- package/lib/frontmatter.js.map +1 -0
- package/lib/getObject.d.ts +2 -0
- package/lib/getObject.js +14 -0
- package/lib/getObject.js.map +1 -0
- package/lib/liquid/conditions.d.ts +6 -0
- package/lib/liquid/conditions.js +243 -0
- package/lib/liquid/conditions.js.map +1 -0
- package/lib/liquid/cycles.d.ts +4 -0
- package/lib/liquid/cycles.js +154 -0
- package/lib/liquid/cycles.js.map +1 -0
- package/lib/liquid/errors.d.ts +3 -0
- package/lib/liquid/errors.js +13 -0
- package/lib/liquid/errors.js.map +1 -0
- package/lib/liquid/evaluation.d.ts +4 -0
- package/lib/liquid/evaluation.js +143 -0
- package/lib/liquid/evaluation.js.map +1 -0
- package/lib/liquid/filters.d.ts +9 -0
- package/lib/liquid/filters.js +22 -0
- package/lib/liquid/filters.js.map +1 -0
- package/lib/liquid/index.d.ts +15 -0
- package/lib/liquid/index.js +132 -0
- package/lib/liquid/index.js.map +1 -0
- package/lib/liquid/legacyConditions.d.ts +4 -0
- package/lib/liquid/legacyConditions.js +174 -0
- package/lib/liquid/legacyConditions.js.map +1 -0
- package/lib/liquid/lexical.d.ts +16 -0
- package/lib/liquid/lexical.js +83 -0
- package/lib/liquid/lexical.js.map +1 -0
- package/lib/liquid/services/argv.d.ts +16 -0
- package/lib/liquid/services/argv.js +17 -0
- package/lib/liquid/services/argv.js.map +1 -0
- package/lib/liquid/sourceMap.d.ts +19 -0
- package/lib/liquid/sourceMap.js +51 -0
- package/lib/liquid/sourceMap.js.map +1 -0
- package/lib/liquid/substitutions.d.ts +2 -0
- package/lib/liquid/substitutions.js +56 -0
- package/lib/liquid/substitutions.js.map +1 -0
- package/lib/liquid/utils.d.ts +8 -0
- package/lib/liquid/utils.js +24 -0
- package/lib/liquid/utils.js.map +1 -0
- package/lib/log.d.ts +18 -0
- package/lib/log.js +54 -0
- package/lib/log.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 YANDEX LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
[](https://www.npmjs.org/package/@diplodoc/liquid)
|
|
2
|
+
|
|
3
|
+
[@diplodoc/liquid](https://www.npmjs.com/package/@diplodoc/liquid) is a small and fast implementation of basic liquid syntax.
|
|
4
|
+
|
|
5
|
+
## Installation {#install}
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm i @diplodoc/liquid
|
|
9
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare type FrontMatter = {
|
|
2
|
+
[key: string]: unknown;
|
|
3
|
+
metadata?: Record<string, unknown>[];
|
|
4
|
+
};
|
|
5
|
+
export declare const extractFrontMatter: (fileContent: string, filePath?: string) => [FrontMatter, string];
|
|
6
|
+
export declare const composeFrontMatter: (frontMatter: FrontMatter, strippedContent: string) => string;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.composeFrontMatter = exports.extractFrontMatter = void 0;
|
|
7
|
+
const js_yaml_1 = require("js-yaml");
|
|
8
|
+
const ts_dedent_1 = require("ts-dedent");
|
|
9
|
+
const cloneDeepWith_1 = __importDefault(require("lodash/cloneDeepWith"));
|
|
10
|
+
const log_1 = require("./log");
|
|
11
|
+
const SEP = '---';
|
|
12
|
+
/**
|
|
13
|
+
* Temporary workaround to enable parsing YAML metadata from potentially
|
|
14
|
+
* Liquid-aware source files
|
|
15
|
+
* @param content Input string which could contain Liquid-style substitution syntax (which clashes with YAML
|
|
16
|
+
* object syntax)
|
|
17
|
+
* @returns String with `{}` escaped, ready to be parsed with `js-yaml`
|
|
18
|
+
*/
|
|
19
|
+
const escapeLiquid = (content) => content.replace(/{{/g, '(({{').replace(/}}/g, '}}))');
|
|
20
|
+
/**
|
|
21
|
+
* Inverse of a workaround defined above.
|
|
22
|
+
* @see `escapeLiquidSubstitutionSyntax`
|
|
23
|
+
* @param escapedContent Input string with `{}` escaped with backslashes
|
|
24
|
+
* @returns Unescaped string
|
|
25
|
+
*/
|
|
26
|
+
const unescapeLiquid = (escapedContent) => escapedContent.replace(/\(\({{/g, '{{').replace(/}}\)\)/g, '}}');
|
|
27
|
+
const matchMetadata = (fileContent) => {
|
|
28
|
+
if (!fileContent.startsWith(SEP)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const closeStart = fileContent.indexOf('\n' + SEP, SEP.length);
|
|
32
|
+
const closeEnd = fileContent.indexOf('\n', closeStart + 1);
|
|
33
|
+
if (closeStart === -1) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return [fileContent.slice(SEP.length, closeStart).trim(), fileContent.slice(closeEnd + 1)];
|
|
37
|
+
};
|
|
38
|
+
const duplicateKeysCompatibleLoad = (yaml, filePath) => {
|
|
39
|
+
try {
|
|
40
|
+
return (0, js_yaml_1.load)(yaml);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
if (e instanceof js_yaml_1.YAMLException) {
|
|
44
|
+
const duplicateKeysDeprecationWarning = (0, ts_dedent_1.dedent) `
|
|
45
|
+
In ${filePath !== null && filePath !== void 0 ? filePath : '(unknown)'}: Encountered a YAML parsing exception when processing file metadata: ${e.reason}.
|
|
46
|
+
It's highly possible the input file contains duplicate mapping keys.
|
|
47
|
+
Will retry processing with necessary compatibility flags.
|
|
48
|
+
Please note that this behaviour is DEPRECATED and WILL be removed in a future version
|
|
49
|
+
without further notice, so the build WILL fail when supplied with YAML-incompatible meta.
|
|
50
|
+
`;
|
|
51
|
+
log_1.log.warn(duplicateKeysDeprecationWarning);
|
|
52
|
+
return (0, js_yaml_1.load)(yaml, { json: true });
|
|
53
|
+
}
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const extractFrontMatter = (fileContent, filePath) => {
|
|
58
|
+
const matches = matchMetadata(fileContent);
|
|
59
|
+
if (matches) {
|
|
60
|
+
const [metadata, strippedContent] = matches;
|
|
61
|
+
return [
|
|
62
|
+
(0, cloneDeepWith_1.default)(duplicateKeysCompatibleLoad(escapeLiquid(metadata), filePath), (v) => (typeof v === 'string' ? unescapeLiquid(v) : undefined)),
|
|
63
|
+
strippedContent,
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
return [{}, fileContent];
|
|
67
|
+
};
|
|
68
|
+
exports.extractFrontMatter = extractFrontMatter;
|
|
69
|
+
const composeFrontMatter = (frontMatter, strippedContent) => {
|
|
70
|
+
const dumped = (0, js_yaml_1.dump)(frontMatter, { lineWidth: -1 }).trim();
|
|
71
|
+
// This empty object check is a bit naive
|
|
72
|
+
// The other option would be to check if all own fields are `undefined`,
|
|
73
|
+
// since we exploit passing in `undefined` to remove a field quite a bit
|
|
74
|
+
if (dumped === '{}') {
|
|
75
|
+
return strippedContent;
|
|
76
|
+
}
|
|
77
|
+
return `${SEP}\n${dumped}\n${SEP}\n${strippedContent}`;
|
|
78
|
+
};
|
|
79
|
+
exports.composeFrontMatter = composeFrontMatter;
|
|
80
|
+
//# sourceMappingURL=frontmatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frontmatter.js","sourceRoot":"","sources":["../src/transform/frontmatter.ts"],"names":[],"mappings":";;;;;;AAAA,qCAAkD;AAClD,yCAAiC;AACjC,yEAAiD;AAEjD,+BAA0B;AAO1B,MAAM,GAAG,GAAG,KAAK,CAAC;AAElB;;;;;;GAMG;AACH,MAAM,YAAY,GAAG,CAAC,OAAe,EAAU,EAAE,CAC7C,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAE1D;;;;;GAKG;AACH,MAAM,cAAc,GAAG,CAAC,cAAsB,EAAU,EAAE,CACtD,cAAc,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AAErE,MAAM,aAAa,GAAG,CAAC,WAAmB,EAAE,EAAE;IAC1C,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,EAA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AAa,EACT,2BAA2B,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAgB,EAC5E,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CACjE;YACD,eAAe;SAClB,CAAC;KACL;IAED,OAAO,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;AAC7B,CAAC,CAAC;AAnBW,QAAA,kBAAkB,sBAmB7B;AAEK,MAAM,kBAAkB,GAAG,CAAC,WAAwB,EAAE,eAAuB,EAAE,EAAE;IACpF,MAAM,MAAM,GAAG,IAAA,cAAI,EAAC,WAAW,EAAE,EAAC,SAAS,EAAE,CAAC,CAAC,EAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAEzD,yCAAyC;IACzC,wEAAwE;IACxE,wEAAwE;IACxE,IAAI,MAAM,KAAK,IAAI,EAAE;QACjB,OAAO,eAAe,CAAC;KAC1B;IAED,OAAO,GAAG,GAAG,KAAK,MAAM,KAAK,GAAG,KAAK,eAAe,EAAE,CAAC;AAC3D,CAAC,CAAC;AAXW,QAAA,kBAAkB,sBAW7B"}
|
package/lib/getObject.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = function getObject(path, obj, fallback) {
|
|
3
|
+
const queue = path.split('.');
|
|
4
|
+
let box = obj;
|
|
5
|
+
while (queue.length) {
|
|
6
|
+
const step = queue.shift();
|
|
7
|
+
if (!Object.prototype.hasOwnProperty.call(box, step)) {
|
|
8
|
+
return fallback || undefined;
|
|
9
|
+
}
|
|
10
|
+
box = box[step];
|
|
11
|
+
}
|
|
12
|
+
return box;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=getObject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getObject.js","sourceRoot":"","sources":["../src/transform/getObject.ts"],"names":[],"mappings":";AACA,iBAAS,SAAS,SAAS,CAAC,IAAY,EAAE,GAAwB,EAAE,QAAc;IAC9E,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE9B,IAAI,GAAG,GAAG,GAAG,CAAC;IACd,OAAO,KAAK,CAAC,MAAM,EAAE;QACjB,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,EAAY,CAAC;QAErC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE;YAClD,OAAO,QAAQ,IAAI,SAAS,CAAC;SAChC;QAED,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;KACnB;IAED,OAAO,GAAG,CAAC;AACf,CAAC,CAAC"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const chalk_1 = require("chalk");
|
|
6
|
+
const log_1 = require("../log");
|
|
7
|
+
const evaluation_1 = require("./evaluation");
|
|
8
|
+
const lexical_1 = require("./lexical");
|
|
9
|
+
const sourceMap_1 = require("./sourceMap");
|
|
10
|
+
const legacyConditions_1 = __importDefault(require("./legacyConditions"));
|
|
11
|
+
function resourcemap(source, ifTag, ifCon, api) {
|
|
12
|
+
const [sourseStartLine, sourceEndLine] = [
|
|
13
|
+
(0, sourceMap_1.getLineNumber)(source, ifTag.start + 1),
|
|
14
|
+
(0, sourceMap_1.getLineNumber)(source, ifTag.end - 1),
|
|
15
|
+
];
|
|
16
|
+
if (sourseStartLine === sourceEndLine || ifTag === ifCon) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const linesTotal = source.split('\n').length;
|
|
20
|
+
const { getSourceMapValue, moveLines, removeLines } = api;
|
|
21
|
+
let offsetRestLines;
|
|
22
|
+
if (ifCon) {
|
|
23
|
+
const [resultStartLine, resultEndLine] = [
|
|
24
|
+
(0, sourceMap_1.getLineNumber)(source, ifCon.start),
|
|
25
|
+
(0, sourceMap_1.getLineNumber)(source, ifCon.end),
|
|
26
|
+
];
|
|
27
|
+
// Move condition's content to the top
|
|
28
|
+
const offsetContentLines = sourseStartLine - resultStartLine;
|
|
29
|
+
moveLines({
|
|
30
|
+
start: resultStartLine,
|
|
31
|
+
end: resultEndLine,
|
|
32
|
+
offset: offsetContentLines,
|
|
33
|
+
withReplace: true,
|
|
34
|
+
});
|
|
35
|
+
// Remove the rest lines of the condition block
|
|
36
|
+
removeLines({ start: sourseStartLine, end: resultStartLine - 1 });
|
|
37
|
+
removeLines({ start: resultEndLine + 1, end: sourceEndLine });
|
|
38
|
+
// Calculate an offset of the rest lines
|
|
39
|
+
offsetRestLines = getSourceMapValue(resultEndLine) - sourceEndLine;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// Remove the whole condition block
|
|
43
|
+
removeLines({ start: sourseStartLine, end: sourceEndLine });
|
|
44
|
+
// Calculate offset of the rest lines
|
|
45
|
+
offsetRestLines = sourseStartLine - sourceEndLine - 1;
|
|
46
|
+
}
|
|
47
|
+
// Offset the rest lines
|
|
48
|
+
moveLines({ start: sourceEndLine + 1, end: linesTotal, offset: offsetRestLines });
|
|
49
|
+
}
|
|
50
|
+
function headLinebreak(raw) {
|
|
51
|
+
const match = raw.match(/^([^{]+){.*/);
|
|
52
|
+
return match ? match[1] : '';
|
|
53
|
+
}
|
|
54
|
+
function tailLinebreak(raw) {
|
|
55
|
+
const match = raw.match(/.*}(\s*\n)$/);
|
|
56
|
+
return match ? match[1] : '';
|
|
57
|
+
}
|
|
58
|
+
function trimResult(content, ifTag, ifCon) {
|
|
59
|
+
if (!ifCon) {
|
|
60
|
+
const head = headLinebreak(ifTag.rawStart);
|
|
61
|
+
const tail = tailLinebreak(ifTag.rawEnd);
|
|
62
|
+
let rest = head + tail;
|
|
63
|
+
if (rest !== head && rest !== tail) {
|
|
64
|
+
// We have extra line break, if condition was placed on individual line
|
|
65
|
+
rest = rest.replace('\n', '');
|
|
66
|
+
}
|
|
67
|
+
return ifTag.isBlock ? '\n' : rest;
|
|
68
|
+
}
|
|
69
|
+
content = content.substring(ifCon.start, ifCon.end);
|
|
70
|
+
if (ifTag.isBlock) {
|
|
71
|
+
return trimBlockResult(content, ifCon);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return trimInlineResult(content, ifTag);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function trimBlockResult(content, ifCon) {
|
|
78
|
+
const head = headLinebreak(ifCon.rawStart);
|
|
79
|
+
if (head) {
|
|
80
|
+
content = '\n' + content;
|
|
81
|
+
}
|
|
82
|
+
const tail = tailLinebreak(ifCon.rawEnd);
|
|
83
|
+
if (tail) {
|
|
84
|
+
content = content + '\n';
|
|
85
|
+
}
|
|
86
|
+
return content;
|
|
87
|
+
}
|
|
88
|
+
function trimInlineResult(content, ifTag) {
|
|
89
|
+
const head = headLinebreak(ifTag.rawStart);
|
|
90
|
+
if (head) {
|
|
91
|
+
content = head + content;
|
|
92
|
+
}
|
|
93
|
+
const tail = tailLinebreak(ifTag.rawEnd);
|
|
94
|
+
if (tail) {
|
|
95
|
+
content = content + tail;
|
|
96
|
+
}
|
|
97
|
+
return content;
|
|
98
|
+
}
|
|
99
|
+
class IfTag {
|
|
100
|
+
constructor() {
|
|
101
|
+
this.conditions = [];
|
|
102
|
+
}
|
|
103
|
+
get start() {
|
|
104
|
+
if (!this.conditions.length) {
|
|
105
|
+
return -1;
|
|
106
|
+
}
|
|
107
|
+
const first = this.conditions[0];
|
|
108
|
+
return first.start - first.rawStart.length;
|
|
109
|
+
}
|
|
110
|
+
get end() {
|
|
111
|
+
if (!this.conditions.length) {
|
|
112
|
+
return -1;
|
|
113
|
+
}
|
|
114
|
+
const last = this.conditions[this.conditions.length - 1];
|
|
115
|
+
return last.end + last.rawEnd.length;
|
|
116
|
+
}
|
|
117
|
+
get rawStart() {
|
|
118
|
+
if (!this.conditions.length) {
|
|
119
|
+
return '';
|
|
120
|
+
}
|
|
121
|
+
const first = this.conditions[0];
|
|
122
|
+
return first.rawStart;
|
|
123
|
+
}
|
|
124
|
+
get rawEnd() {
|
|
125
|
+
if (!this.conditions.length) {
|
|
126
|
+
return '';
|
|
127
|
+
}
|
|
128
|
+
const last = this.conditions[this.conditions.length - 1];
|
|
129
|
+
return last.rawEnd;
|
|
130
|
+
}
|
|
131
|
+
get isBlock() {
|
|
132
|
+
const first = this.conditions[0];
|
|
133
|
+
const last = this.conditions[this.conditions.length - 1];
|
|
134
|
+
return tailLinebreak(first.rawStart) && headLinebreak(last.rawEnd);
|
|
135
|
+
}
|
|
136
|
+
*[Symbol.iterator]() {
|
|
137
|
+
for (const condition of this.conditions) {
|
|
138
|
+
yield condition;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
openCondition(raw, expr, start) {
|
|
142
|
+
this.closeCondition(raw, start);
|
|
143
|
+
this.conditions.push({
|
|
144
|
+
rawStart: raw,
|
|
145
|
+
start: start + raw.length,
|
|
146
|
+
expr,
|
|
147
|
+
});
|
|
148
|
+
return start + raw.length - tailLinebreak(raw).length;
|
|
149
|
+
}
|
|
150
|
+
closeCondition(raw, end) {
|
|
151
|
+
const condition = this.conditions[this.conditions.length - 1];
|
|
152
|
+
if (condition) {
|
|
153
|
+
condition.rawEnd = raw;
|
|
154
|
+
condition.end = end;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function inlineConditions(content, ifTag, vars, strict) {
|
|
159
|
+
let ifCon = null;
|
|
160
|
+
for (const condition of ifTag) {
|
|
161
|
+
const value = (0, evaluation_1.evalExp)(condition.expr, vars, strict);
|
|
162
|
+
if (condition.expr && value === evaluation_1.NoValue) {
|
|
163
|
+
return {
|
|
164
|
+
result: content,
|
|
165
|
+
// Fix offset for next matches.
|
|
166
|
+
// There can be some significant linebreak and spaces.
|
|
167
|
+
lastIndex: ifTag.end - tailLinebreak(ifTag.rawEnd).length,
|
|
168
|
+
ifCon: ifTag,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (!condition.expr || value) {
|
|
172
|
+
ifCon = condition;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const start = content.slice(0, ifTag.start);
|
|
177
|
+
const end = content.slice(ifTag.end);
|
|
178
|
+
const result = trimResult(content, ifTag, ifCon);
|
|
179
|
+
return {
|
|
180
|
+
result: start + result + end,
|
|
181
|
+
lastIndex: start.length + result.length - tailLinebreak(ifTag.rawEnd).length,
|
|
182
|
+
ifCon,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
module.exports = function conditions(input, vars, path, settings) {
|
|
186
|
+
if (settings === null || settings === void 0 ? void 0 : settings.useLegacyConditions) {
|
|
187
|
+
return (0, legacyConditions_1.default)(input, vars, path, settings);
|
|
188
|
+
}
|
|
189
|
+
const sourceMap = (settings === null || settings === void 0 ? void 0 : settings.sourceMap) || {};
|
|
190
|
+
const strict = (settings === null || settings === void 0 ? void 0 : settings.strict) || false;
|
|
191
|
+
const tagStack = [];
|
|
192
|
+
// Consumes all between curly braces
|
|
193
|
+
// and all closest upon to first linebreak before and after braces.
|
|
194
|
+
const R_LIQUID = /((?:\n[\t ]*)?{%-?([\s\S]*?)-?%}(?:[\t ]*\n)?)/g;
|
|
195
|
+
let match;
|
|
196
|
+
while ((match = R_LIQUID.exec(input)) !== null) {
|
|
197
|
+
if (!match[1]) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const tagMatch = match[2].trim().match(lexical_1.tagLine);
|
|
201
|
+
if (!tagMatch) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const [type, args] = tagMatch.slice(1);
|
|
205
|
+
switch (type) {
|
|
206
|
+
case 'if': {
|
|
207
|
+
const tag = new IfTag();
|
|
208
|
+
R_LIQUID.lastIndex = tag.openCondition(match[1], args, match.index);
|
|
209
|
+
tagStack.push(tag);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
case 'elsif':
|
|
213
|
+
case 'else': {
|
|
214
|
+
const tag = tagStack[tagStack.length - 1];
|
|
215
|
+
R_LIQUID.lastIndex = tag.openCondition(match[1], args, match.index);
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
case 'endif': {
|
|
219
|
+
const ifTag = tagStack.pop();
|
|
220
|
+
if (!ifTag) {
|
|
221
|
+
// TODO(3y3): make lint rule
|
|
222
|
+
log_1.log.error(`If block must be opened before close${path ? ` in ${(0, chalk_1.bold)(path)}` : ''}`);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
ifTag.closeCondition(match[1], match.index);
|
|
226
|
+
const { result, lastIndex, ifCon } = inlineConditions(input, ifTag, vars, strict);
|
|
227
|
+
resourcemap(input, ifTag, ifCon, (0, sourceMap_1.createSourceMapApi)(sourceMap));
|
|
228
|
+
R_LIQUID.lastIndex = lastIndex;
|
|
229
|
+
input = result;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
default:
|
|
233
|
+
// This is not condition.
|
|
234
|
+
// Step back last linebreaks to match them on next condition
|
|
235
|
+
R_LIQUID.lastIndex -= tailLinebreak(match[1]).length;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (tagStack.length !== 0) {
|
|
239
|
+
log_1.log.error(`Condition block must be closed${path ? ` in ${(0, chalk_1.bold)(path)}` : ''}`);
|
|
240
|
+
}
|
|
241
|
+
return input;
|
|
242
|
+
};
|
|
243
|
+
//# sourceMappingURL=conditions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditions.js","sourceRoot":"","sources":["../../src/transform/liquid/conditions.ts"],"names":[],"mappings":";;;;AAAA,iCAA2B;AAE3B,gCAA2B;AAE3B,6CAA8C;AAC9C,uCAAkC;AAClC,2CAA4E;AAC5E,0EAAkD;AASl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E,KAAK,EAAC,GAAG,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;gBAEhF,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAA,8BAAkB,EAAC,SAAS,CAAC,CAAC,CAAC;gBAEhE,QAAQ,CAAC,SAAS,GAAG,SAAS,CAAC;gBAC/B,KAAK,GAAG,MAAM,CAAC;gBAEf,MAAM;aACT;YACD;gBACI,yBAAyB;gBACzB,4DAA4D;gBAC5D,QAAQ,CAAC,SAAS,IAAI,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;SAC5D;KACJ;IAED,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;QACvB,SAAG,CAAC,KAAK,CAAC,iCAAiC,IAAI,CAAC,CAAC,CAAC,OAAO,IAAA,YAAI,EAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;KACjF;IAED,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const chalk_1 = require("chalk");
|
|
3
|
+
const log_1 = require("../log");
|
|
4
|
+
const evaluation_1 = require("./evaluation");
|
|
5
|
+
const lexical_1 = require("./lexical");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
const sourceMap_1 = require("./sourceMap");
|
|
8
|
+
const index_1 = require("./index");
|
|
9
|
+
function changeSourceMap({ firstLineNumber, lastLineNumber, resFirstLineNumber, resLastLineNumber, contentLinesTotal, linesTotal, sourceMap, }) {
|
|
10
|
+
if (!sourceMap) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const isInlineTag = firstLineNumber === lastLineNumber;
|
|
14
|
+
const { moveLines, removeLine } = (0, sourceMap_1.createSourceMapApi)(sourceMap);
|
|
15
|
+
if (isInlineTag || !resFirstLineNumber) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const offsetRestLines = contentLinesTotal - (lastLineNumber - firstLineNumber + 1);
|
|
19
|
+
// Move condition's content to the top
|
|
20
|
+
const offsetContentLines = firstLineNumber - resFirstLineNumber;
|
|
21
|
+
moveLines({
|
|
22
|
+
start: resFirstLineNumber,
|
|
23
|
+
end: resLastLineNumber - 1,
|
|
24
|
+
offset: offsetContentLines,
|
|
25
|
+
withReplace: true,
|
|
26
|
+
});
|
|
27
|
+
// Remove tags
|
|
28
|
+
removeLine(firstLineNumber);
|
|
29
|
+
removeLine(lastLineNumber);
|
|
30
|
+
// Offset the rest lines
|
|
31
|
+
moveLines({ start: lastLineNumber + 1, end: linesTotal, offset: offsetRestLines });
|
|
32
|
+
}
|
|
33
|
+
function inlineConditions({ forTag, vars, content, match, path, lastIndex, sourceMap, linesTotal, }) {
|
|
34
|
+
let res = '';
|
|
35
|
+
const firstLineNumber = (0, sourceMap_1.getLineNumber)(content, forTag.startPos);
|
|
36
|
+
const lastLineNumber = (0, sourceMap_1.getLineNumber)(content, lastIndex);
|
|
37
|
+
const forRawLastIndex = forTag.startPos + forTag.forRaw.length;
|
|
38
|
+
const contentLastIndex = match.index;
|
|
39
|
+
const forTemplate = content.substring(forRawLastIndex, contentLastIndex);
|
|
40
|
+
const resFirstLineNumber = (0, sourceMap_1.getLineNumber)(content, forRawLastIndex + 1);
|
|
41
|
+
const resLastLineNumber = (0, sourceMap_1.getLineNumber)(content, contentLastIndex + 1);
|
|
42
|
+
let collection = (0, evaluation_1.evalExp)(forTag.collectionName, vars);
|
|
43
|
+
if (!collection || !Array.isArray(collection)) {
|
|
44
|
+
collection = [];
|
|
45
|
+
log_1.log.error(`${(0, chalk_1.bold)(forTag.collectionName)} is undefined or not iterable`);
|
|
46
|
+
}
|
|
47
|
+
collection.forEach((item) => {
|
|
48
|
+
const newVars = Object.assign(Object.assign({}, vars), { [forTag.variableName]: item });
|
|
49
|
+
res += (0, index_1.liquidSnippet)(forTemplate, newVars, path).trimRight();
|
|
50
|
+
});
|
|
51
|
+
const contentLinesTotal = res.split('\n').length - 1;
|
|
52
|
+
changeSourceMap({
|
|
53
|
+
firstLineNumber,
|
|
54
|
+
lastLineNumber,
|
|
55
|
+
resFirstLineNumber,
|
|
56
|
+
resLastLineNumber,
|
|
57
|
+
linesTotal,
|
|
58
|
+
sourceMap,
|
|
59
|
+
contentLinesTotal,
|
|
60
|
+
});
|
|
61
|
+
const preparedLeftContent = (0, utils_1.getPreparedLeftContent)({
|
|
62
|
+
content,
|
|
63
|
+
tagStartPos: forTag.startPos,
|
|
64
|
+
tagContent: res,
|
|
65
|
+
});
|
|
66
|
+
let shift = 0;
|
|
67
|
+
if (res === '' &&
|
|
68
|
+
preparedLeftContent[preparedLeftContent.length - 1] === '\n' &&
|
|
69
|
+
content[lastIndex] === '\n') {
|
|
70
|
+
shift = 1;
|
|
71
|
+
}
|
|
72
|
+
if (res !== '') {
|
|
73
|
+
if (res[0] === ' ' || res[0] === '\n') {
|
|
74
|
+
res = res.substring(1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const leftPart = preparedLeftContent + res;
|
|
78
|
+
return {
|
|
79
|
+
result: leftPart + content.substring(lastIndex + shift),
|
|
80
|
+
idx: leftPart.length,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
module.exports = function cycles(originInput, vars, path, settings = {}) {
|
|
84
|
+
const { sourceMap } = settings;
|
|
85
|
+
const R_LIQUID = /({%-?([\s\S]*?)-?%})/g;
|
|
86
|
+
const FOR_SYNTAX = new RegExp(`(\\w+)\\s+in\\s+(${lexical_1.variable.source})`);
|
|
87
|
+
let match;
|
|
88
|
+
const tagStack = [];
|
|
89
|
+
let input = originInput;
|
|
90
|
+
let countSkippedInnerTags = 0;
|
|
91
|
+
let linesTotal = originInput.split('\n').length;
|
|
92
|
+
while ((match = R_LIQUID.exec(input)) !== null) {
|
|
93
|
+
if (!match[1]) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const tagMatch = match[2].trim().match(lexical_1.tagLine);
|
|
97
|
+
if (!tagMatch) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const [type, args] = tagMatch.slice(1);
|
|
101
|
+
switch (type) {
|
|
102
|
+
case 'for': {
|
|
103
|
+
if (tagStack.length) {
|
|
104
|
+
countSkippedInnerTags += 1;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
const matches = args.match(FOR_SYNTAX);
|
|
108
|
+
if (!matches) {
|
|
109
|
+
log_1.log.error(`Incorrect syntax in if condition${path ? ` in ${(0, chalk_1.bold)(path)}` : ''}`);
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
const [variableName, collectionName] = matches.slice(1);
|
|
113
|
+
tagStack.push({
|
|
114
|
+
item: args,
|
|
115
|
+
variableName,
|
|
116
|
+
collectionName,
|
|
117
|
+
startPos: match.index,
|
|
118
|
+
forRaw: match[1],
|
|
119
|
+
});
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case 'endfor': {
|
|
123
|
+
if (countSkippedInnerTags > 0) {
|
|
124
|
+
countSkippedInnerTags -= 1;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const forTag = tagStack.pop();
|
|
128
|
+
if (!forTag) {
|
|
129
|
+
log_1.log.error(`For block must be opened before close${path ? ` in ${(0, chalk_1.bold)(path)}` : ''}`);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
const { idx, result } = inlineConditions({
|
|
133
|
+
forTag,
|
|
134
|
+
vars,
|
|
135
|
+
content: input,
|
|
136
|
+
match,
|
|
137
|
+
path,
|
|
138
|
+
lastIndex: R_LIQUID.lastIndex,
|
|
139
|
+
sourceMap: sourceMap || {},
|
|
140
|
+
linesTotal,
|
|
141
|
+
});
|
|
142
|
+
R_LIQUID.lastIndex = idx;
|
|
143
|
+
input = result;
|
|
144
|
+
linesTotal = result.split('\n').length;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (tagStack.length !== 0) {
|
|
150
|
+
log_1.log.error(`For block must be closed${path ? ` in ${(0, chalk_1.bold)(path)}` : ''}`);
|
|
151
|
+
}
|
|
152
|
+
return input;
|
|
153
|
+
};
|
|
154
|
+
//# sourceMappingURL=cycles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cycles.js","sourceRoot":"","sources":["../../src/transform/liquid/cycles.ts"],"names":[],"mappings":";AAAA,iCAA2B;AAE3B,gCAA2B;AAE3B,6CAAqC;AACrC,uCAA4C;AAC5C,mCAA+C;AAC/C,2CAA8D;AAE9D,mCAAsC;AAYtC,SAAS,eAAe,CAAC,EACrB,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EACV,SAAS,GACH;IACN,IAAI,CAAC,SAAS,EAAE;QACZ,OAAO;KACV;IAED,MAAM,WAAW,GAAG,eAAe,KAAK,cAAc,CAAC;IACvD,MAAM,EAAC,SAAS,EAAE,UAAU,EAAC,GAAG,IAAA,8BAAkB,EAAC,SAAS,CAAC,CAAC;IAE9D,IAAI,WAAW,IAAI,CAAC,kBAAkB,EAAE;QACpC,OAAO;KACV;IAED,MAAM,eAAe,GAAG,iBAAiB,GAAG,CAAC,cAAc,GAAG,eAAe,GAAG,CAAC,CAAC,CAAC;IAEnF,sCAAsC;IACtC,MAAM,kBAAkB,GAAG,eAAe,GAAG,kBAAkB,CAAC;IAChE,SAAS,CAAC;QACN,KAAK,EAAE,kBAAkB;QACzB,GAAG,EAAE,iBAAiB,GAAG,CAAC;QAC1B,MAAM,EAAE,kBAAkB;QAC1B,WAAW,EAAE,IAAI;KACpB,CAAC,CAAC;IAEH,cAAc;IACd,UAAU,CAAC,eAAe,CAAC,CAAC;IAC5B,UAAU,CAAC,cAAc,CAAC,CAAC;IAE3B,wBAAwB;IACxB,SAAS,CAAC,EAAC,KAAK,EAAE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CAAC;KAC3E;IAED,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SkippedEvalError = void 0;
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
class SkippedEvalError extends Error {
|
|
6
|
+
constructor(message, exp) {
|
|
7
|
+
super();
|
|
8
|
+
this.name = 'SkippedEvalError';
|
|
9
|
+
this.message = `${message}: ${(0, chalk_1.bold)(exp)}`;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.SkippedEvalError = SkippedEvalError;
|
|
13
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/transform/liquid/errors.ts"],"names":[],"mappings":";;;AAAA,iCAA2B;AAE3B,MAAa,gBAAiB,SAAQ,KAAK;IACvC,YAAY,OAAe,EAAE,GAAW;QACpC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,GAAG,OAAO,KAAK,IAAA,YAAI,EAAC,GAAG,CAAC,EAAE,CAAC;IAC9C,CAAC;CACJ;AAPD,4CAOC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const NoValue: unique symbol;
|
|
2
|
+
export declare function evalExp(exp: string, scope: Record<string, unknown>, strict?: boolean): string[] | number[] | boolean | string | symbol | undefined | ((input: string) => number | string);
|
|
3
|
+
declare const _default: (exp: string, scope: Record<string, unknown>, strict?: boolean) => boolean;
|
|
4
|
+
export default _default;
|