btrz-liquid-templates 1.7.0 → 1.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "btrz-liquid-templates",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "Extension and helpers for liquid templates to deal with our data and other things we need to encapsulate",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/html.js CHANGED
@@ -90,8 +90,27 @@ function Html(engine) {
90
90
  });
91
91
  }
92
92
 
93
+ function Unescape(engine) {
94
+ this.registerTag("unescape", {
95
+ parse: function(tagToken) {
96
+ this.item = tagToken.args;
97
+ },
98
+ render: async function(ctx) {
99
+ const str = await this.liquid.evalValue(this.item, ctx);
100
+
101
+ return(str || "")
102
+ .replace(/&/g, '&')
103
+ .replace(/&lt;/g, '<')
104
+ .replace(/&gt;/g, '>')
105
+ .replace(/&quot;/g, '"')
106
+ .replace(/&#39;/g, "'");
107
+ }
108
+ });
109
+ }
110
+
93
111
  module.exports = {
94
- Html
112
+ Html,
113
+ Unescape
95
114
  };
96
115
 
97
116
 
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
- return `${getPrefix(url)}${Buffer.from(response.data, "binary").toString("base64")}`;
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
@@ -2,7 +2,10 @@ const {Liquid} = require("liquidjs");
2
2
  const {Localizer} = require("./localizer.js");
3
3
  const {HorizontalLine} = require("./lines.js");
4
4
  const {Barcode} = require("./barcode.js");
5
- const {Html} = require("./html.js");
5
+ const {
6
+ Html,
7
+ Unescape
8
+ } = require("./html.js");
6
9
  const {Money, CurcySymbol, CurcyIso, CurcyName, MoneyReduce} = require("./money.js");
7
10
  const {DateF, TimeF, DateTime, HumanDate, HumanDateTime, ExpDate, HumanArrivalDateTime, HumanDepartureDateTime,
8
11
  DepartureDateTime, ArrivalDateTime, DepartureDate, ArrivalDate, DepartureTime, ArrivalTime, HumanArrivalDate,
@@ -52,6 +55,7 @@ function getEngine() {
52
55
  });
53
56
  engine.plugin(Localizer);
54
57
  engine.plugin(Html);
58
+ engine.plugin(Unescape);
55
59
  engine.plugin(HorizontalLine);
56
60
  engine.plugin(Barcode);
57
61
  engine.plugin(Money);
@@ -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 = `{
@@ -728,4 +748,20 @@ and some more here`,
728
748
  const documentDefinition = await tpl.processToEscp(template, data);
729
749
  expect(documentDefinition).to.be.eql(`\u001bia\u0000\u001b@\u001b3$\u001bM\u001bEShift location closure #: 5c9f8f8f8f8f8f8f8f8f8f8\n\u001bF\u001b\u000f\f`);
730
750
  });
751
+
752
+ it("should return a text with unescaped characters", async () => {
753
+ const tpl = require("../src/index");
754
+ data.escapedCharacters = "&lt; &gt; &amp; $";
755
+ const template = `{
756
+ "content": [
757
+ "{%- unescape escapedCharacters %}"
758
+ ]
759
+ }`;
760
+ const documentDefinition = await tpl.processToObj(template, data);
761
+ expect(documentDefinition).to.be.eql({
762
+ "content": [
763
+ "< > & $"
764
+ ]
765
+ });
766
+ });
731
767
  });