@bookshop/eleventy-engine
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
{"processes":{"011d0d69-06b2-45bd-93b9-ab93600dd2b2":{"parent":"61bf4f38-97a3-40f9-9309-898eba242ee1","children":[]},"61bf4f38-97a3-40f9-9309-898eba242ee1":{"parent":null,"children":["011d0d69-06b2-45bd-93b9-ab93600dd2b2","cfd31dca-d288-4137-bf2c-84d495fb9613","eea9a715-05e7-446a-a4a9-690afc045b2e"]},"cfd31dca-d288-4137-bf2c-84d495fb9613":{"parent":"61bf4f38-97a3-40f9-9309-898eba242ee1","children":[]},"eea9a715-05e7-446a-a4a9-690afc045b2e":{"parent":"61bf4f38-97a3-40f9-9309-898eba242ee1","children":[]}},"files":{},"externalIds":{}} | ||
{"processes":{"1631c81d-e30f-4951-98b8-ccd26a899a12":{"parent":"cc063831-6155-4108-adbf-afd4819cea7f","children":[]},"38fa0277-f35d-4e53-a2a4-9ff63acf8b8f":{"parent":"cc063831-6155-4108-adbf-afd4819cea7f","children":[]},"60720d0a-7571-4109-9ca8-1e4a1c53cf29":{"parent":"cc063831-6155-4108-adbf-afd4819cea7f","children":[]},"cc063831-6155-4108-adbf-afd4819cea7f":{"parent":null,"children":["1631c81d-e30f-4951-98b8-ccd26a899a12","38fa0277-f35d-4e53-a2a4-9ff63acf8b8f","60720d0a-7571-4109-9ca8-1e4a1c53cf29"]}},"files":{},"externalIds":{}} |
@@ -1,2 +0,2 @@ | ||
import { Liquid } from 'liquidjs'; | ||
import { Liquid, Context } from 'liquidjs'; | ||
import translateLiquid from './translateLiquid.js'; | ||
@@ -108,2 +108,22 @@ | ||
async eval(str, props = {}) { | ||
try { | ||
const ctx = new Context(); | ||
if (Array.isArray(props)) { | ||
props.forEach(p => ctx.push(p)); | ||
} else { | ||
ctx.push(props); | ||
} | ||
const [,value, index] = str.match(/^(.*?)(?:\[(\d+)\])?$/); | ||
let result = await this.liquid.evalValue(value, ctx); | ||
if (index && typeof result === 'object' && !Array.isArray(result)) { | ||
result = Object.entries(result); | ||
} | ||
return index ? result[index] : result; | ||
} catch(e) { | ||
console.error(`Error evaluating ${str}`, e); | ||
return ''; | ||
} | ||
} | ||
loader() { | ||
@@ -110,0 +130,0 @@ // esbuild loader if required |
@@ -20,1 +20,42 @@ import test from 'ava'; | ||
}); | ||
test("eval value", async t => { | ||
const val = await engine.eval(`test`, {test: 'bookshop'}); | ||
t.is(val, 'bookshop'); | ||
}); | ||
test("eval array", async t => { | ||
const val = await engine.eval(`test`, {test: ['book','shop']}); | ||
t.deepEqual(val, ['book','shop']); | ||
}); | ||
test("eval array index", async t => { | ||
const val = await engine.eval(`test[0]`, {test: ['book','shop']}); | ||
t.is(val, 'book'); | ||
}); | ||
test("eval obj", async t => { | ||
const val = await engine.eval(`test`, {test: {'book':'shop'}}); | ||
t.deepEqual(val, {'book': 'shop'}); | ||
}); | ||
test("eval obj index", async t => { | ||
const val = await engine.eval(`test[0]`, {test: {'book':'shop'}}); | ||
t.deepEqual(val, ['book', 'shop']); | ||
}); | ||
test("eval range", async t => { | ||
const val = await engine.eval(`(0..4)[2]`); | ||
t.is(val, 2); | ||
}); | ||
test("eval deep", async t => { | ||
const val = await engine.eval(`test.inner[0]`, {test: {inner: ['bookshop']}}); | ||
t.is(val, 'bookshop'); | ||
}); | ||
test("eval scopes", async t => { | ||
const scopes = [{test: 'jekyll', other: 'eleventy'}, {test: 'hugo'}]; | ||
t.is(await engine.eval(`test`, scopes), 'hugo'); | ||
t.is(await engine.eval(`other`, scopes), 'eleventy'); | ||
}); |
{ | ||
"name": "@bookshop/eleventy-engine", | ||
"version": "2.0.6", | ||
"packageManager": "yarn@3.0.0", | ||
"version": "2.0.7", | ||
"description": "Bookshop frontend Eleventy renderer", | ||
@@ -25,2 +26,3 @@ "type": "module", | ||
"dependencies": { | ||
"@bookshop/helpers": "2.0.7", | ||
"esbuild": "^0.11.23", | ||
@@ -27,0 +29,0 @@ "liquidjs": "9.28.0", |
18411
11.83%379
15.9%4
33.33%+ Added
+ Added