btrz-liquid-templates 1.6.0 → 1.8.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/docs/index.md +29 -2
- package/package.json +1 -1
- package/src/escp.js +24 -0
- package/src/httpimg.js +10 -9
- package/src/index.js +2 -0
- package/test/index-test.js +20 -0
package/docs/index.md
CHANGED
|
@@ -148,7 +148,7 @@ Small condensed fonts
|
|
|
148
148
|
|
|
149
149
|
* ## lineSpacing
|
|
150
150
|
|
|
151
|
-
Line spacing to 1/
|
|
151
|
+
Line spacing to 1/9 of an inch
|
|
152
152
|
|
|
153
153
|
```liquid
|
|
154
154
|
{% raw%} {% lineSpacing %} {% endraw %}
|
|
@@ -156,12 +156,39 @@ Line spacing to 1/6 of an inch
|
|
|
156
156
|
|
|
157
157
|
* ## lineSpacing2
|
|
158
158
|
|
|
159
|
-
Line spacing to 1/
|
|
159
|
+
Line spacing to 1/6 of an inch
|
|
160
160
|
|
|
161
161
|
```liquid
|
|
162
162
|
{% raw%} {% lineSpacing2 %} {% endraw %}
|
|
163
163
|
```
|
|
164
164
|
|
|
165
|
+
* ## lineSpacingX
|
|
166
|
+
|
|
167
|
+
Sets the line spacing to n/216 inch
|
|
168
|
+
|
|
169
|
+
```liquid
|
|
170
|
+
{% raw%} {% lineSpacingX value %} {% endraw %}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
#### Parameters
|
|
174
|
+
|
|
175
|
+
| name | range | required | default |
|
|
176
|
+
|------|------------|----------|---------|
|
|
177
|
+
| pos | 0 <= value <= 255 | Y | |
|
|
178
|
+
|
|
179
|
+
* value: Number of vertical dots to insert between lines. For example, a value of 24 sets the line spacing to 1/9 inch.
|
|
180
|
+
|
|
181
|
+
#### Common Values (for reference) 9-Pin ESC/P
|
|
182
|
+
|
|
183
|
+
| Value | Inches | Description |
|
|
184
|
+
| ----- | ------ | --------------------- |
|
|
185
|
+
| 6 | 1/36 | Very tight spacing |
|
|
186
|
+
| 12 | 1/18 | Tight spacing |
|
|
187
|
+
| 24 | 1/9 | Standard compact text |
|
|
188
|
+
| 36 | 1/6 | Default line spacing |
|
|
189
|
+
| 48 | 2/9 | Loose spacing |
|
|
190
|
+
| 72 | 1/3 | Very loose spacing |
|
|
191
|
+
|
|
165
192
|
* ## escNewLine
|
|
166
193
|
|
|
167
194
|
New line
|
package/package.json
CHANGED
package/src/escp.js
CHANGED
|
@@ -30,6 +30,29 @@ function lineSpacing2(engine) {
|
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
function lineSpacingX(engine) {
|
|
34
|
+
this.registerTag("lineSpacingX", {
|
|
35
|
+
parse: function(tagToken, remainTokens) {
|
|
36
|
+
try {
|
|
37
|
+
const args = (tagToken.args || "").split(" ");
|
|
38
|
+
this.spacingValue = args[0] || 0;
|
|
39
|
+
} catch (err) {
|
|
40
|
+
this.spacingValue = args[0] || 0;
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
render: async function(ctx) {
|
|
44
|
+
try {
|
|
45
|
+
const spacing = this.spacingValue < 0 ? 0 : this.spacingValue > 255 ? 255 : this.spacingValue;
|
|
46
|
+
const value = String.fromCharCode(spacing);
|
|
47
|
+
const escLineSpacingX = "\x1B\x33" + value;
|
|
48
|
+
return escLineSpacingX;
|
|
49
|
+
} catch (e) {
|
|
50
|
+
return "PNA";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
33
56
|
function fontSelection(engine) {
|
|
34
57
|
this.registerTag("fontSelection", {
|
|
35
58
|
parse: function(tagToken, remainTokens) {
|
|
@@ -144,6 +167,7 @@ module.exports = {
|
|
|
144
167
|
escInitQzTray,
|
|
145
168
|
lineSpacing,
|
|
146
169
|
lineSpacing2,
|
|
170
|
+
lineSpacingX,
|
|
147
171
|
fontSelection,
|
|
148
172
|
escBold,
|
|
149
173
|
escCancelBold,
|
package/src/httpimg.js
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
const axios = require("axios");
|
|
2
2
|
|
|
3
|
-
function getPrefix(url) {
|
|
4
|
-
const extention = url.split(".").pop();
|
|
5
|
-
if (extention.toLowerCase() === "png") {
|
|
6
|
-
return "data:image/png;base64,";
|
|
7
|
-
}
|
|
8
|
-
return "data:image/jpeg;base64,";
|
|
9
|
-
}
|
|
10
|
-
|
|
11
3
|
function HttpImg(engine) {
|
|
12
4
|
this.registerTag("httpImg", {
|
|
13
5
|
parse: function(tagToken, remainTokens) {
|
|
@@ -19,7 +11,16 @@ function HttpImg(engine) {
|
|
|
19
11
|
const response = await axios.get(url, {
|
|
20
12
|
responseType: "arraybuffer"
|
|
21
13
|
});
|
|
22
|
-
|
|
14
|
+
if (response && response.headers && response.headers["content-type"]) {
|
|
15
|
+
const contentType = response.headers["content-type"];
|
|
16
|
+
if (contentType.startsWith("image/png")) {
|
|
17
|
+
return `data:image/png;base64,${Buffer.from(response.data, "binary").toString("base64")}`;
|
|
18
|
+
} else if (contentType.startsWith("image/jpeg")) {
|
|
19
|
+
return `data:image/jpeg;base64,${Buffer.from(response.data, "binary").toString("base64")}`;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//default to a placeholder image if the content type is not recognized
|
|
23
|
+
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mP8P4PhPwMVAeOogaMGjho4auCogSPVQACqOzPNQ/wwqAAAAABJRU5ErkJggg==';
|
|
23
24
|
} catch (e) {
|
|
24
25
|
console.log(e);
|
|
25
26
|
return "";
|
package/src/index.js
CHANGED
|
@@ -17,6 +17,7 @@ const {
|
|
|
17
17
|
escInitQzTray,
|
|
18
18
|
lineSpacing,
|
|
19
19
|
lineSpacing2,
|
|
20
|
+
lineSpacingX,
|
|
20
21
|
fontSelection,
|
|
21
22
|
escBold,
|
|
22
23
|
escCancelBold,
|
|
@@ -98,6 +99,7 @@ module.exports = {
|
|
|
98
99
|
engine.plugin(escInitQzTray);
|
|
99
100
|
engine.plugin(lineSpacing);
|
|
100
101
|
engine.plugin(lineSpacing2);
|
|
102
|
+
engine.plugin(lineSpacingX);
|
|
101
103
|
engine.plugin(fontSelection);
|
|
102
104
|
engine.plugin(escBold);
|
|
103
105
|
engine.plugin(escCancelBold);
|
package/test/index-test.js
CHANGED
|
@@ -198,6 +198,26 @@ and some more here`,
|
|
|
198
198
|
});
|
|
199
199
|
});
|
|
200
200
|
|
|
201
|
+
|
|
202
|
+
it("should not load non image urls", async () => {
|
|
203
|
+
const tpl = require("../src/index");
|
|
204
|
+
const template = `{
|
|
205
|
+
"content": [
|
|
206
|
+
{
|
|
207
|
+
"image" : "{% httpImg 'https://www.google.com' %}"
|
|
208
|
+
}
|
|
209
|
+
]
|
|
210
|
+
}`;
|
|
211
|
+
const documentDefinition = await tpl.processToObj(template, data);
|
|
212
|
+
expect(documentDefinition).to.be.eql({
|
|
213
|
+
"content": [
|
|
214
|
+
{
|
|
215
|
+
"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAH0lEQVR42mP8P4PhPwMVAeOogaMGjho4auCogSPVQACqOzPNQ/wwqAAAAABJRU5ErkJggg=="
|
|
216
|
+
}
|
|
217
|
+
]
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
201
221
|
it("should apply tag after translations", async () => {
|
|
202
222
|
const tpl = require("../src/index");
|
|
203
223
|
const template = `{
|