Skip to content

Commit

Permalink
fix: eleventy crashes when passing complex data to bookshop components
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Apr 1, 2022
1 parent d94cf3d commit 98610b4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
9 changes: 6 additions & 3 deletions javascript-modules/engines/eleventy-engine/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,16 @@ export class Engine {
console.warn(`[eleventy-engine] No component found for ${name}`);
return "";
}
logger?.log?.(`Going to render ${name}`);
logger?.log?.(`Going to render ${name}, with source:`);
logger?.log?.(source);
source = translateLiquid(source);
logger?.log?.(`Rewritten the template for ${name}`);
logger?.log?.(`Rewritten the template for ${name} to:`);
logger?.log?.(source);
if (!globals || typeof globals !== "object") globals = {};
props = this.injectInfo({ ...globals, ...props });
logger?.log?.(`Rendered ${name}`);
target.innerHTML = await this.liquid.parseAndRender(source || "", props);
logger?.log?.(`Rendered ${name} as:`);
logger?.log?.(target.innerHTML);
}

async eval(str, props = {}) {
Expand Down
9 changes: 6 additions & 3 deletions javascript-modules/engines/hugo-engine/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ export class Engine {
console.warn(`[hugo-engine] No component found for ${name}`);
return "";
}
logger?.log?.(`Going to render ${name}`);
logger?.log?.(`Going to render ${name}, with source:`);
logger?.log?.(source);
// TODO: this template already exists on the other side of the wasm bounary
source = translateTextTemplate(source, {});
logger?.log?.(`Rewritten the template for ${name}`);
logger?.log?.(`Rewritten the template for ${name} to:`);
logger?.log?.(source);
if (!globals || typeof globals !== "object") globals = {};
props = { ...globals, ...props };

Expand All @@ -161,8 +163,9 @@ export class Engine {
logger?.log?.(`Failed to render ${output}`);
console.error(output);
} else {
logger?.log?.(`Rendered ${name}`);
target.innerHTML = output;
logger?.log?.(`Rendered ${name} as:`);
logger?.log?.(target.innerHTML);
}
}

Expand Down
9 changes: 6 additions & 3 deletions javascript-modules/engines/jekyll-engine/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,16 @@ export class Engine {
console.warn(`[jekyll-engine] No component found for ${name}`);
return "";
}
logger?.log?.(`Going to render ${name}`);
logger?.log?.(`Going to render ${name}, with source:`);
logger?.log?.(source);
source = translateLiquid(source, {});
logger?.log?.(`Rewritten the template for ${name}`);
logger?.log?.(`Rewritten the template for ${name} to:`);
logger?.log?.(source);
if (!globals || typeof globals !== "object") globals = {};
props = this.injectInfo({ ...globals, include: props });
target.innerHTML = await this.liquid.parseAndRender(source || "", props);
logger?.log?.(`Rendered ${name}`);
logger?.log?.(`Rendered ${name} as:`);
logger?.log?.(target.innerHTML);
}

async eval(str, props = [{}]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const path = require("path");
const fs = require("fs");
const normalizePath = require("normalize-path");
const { version } = require("../package.json");
const { configure } = require('safe-stable-stringify');

const stringify = configure({ deterministic: false });

const { Tokenizer } = LiquidJS;

Expand All @@ -17,10 +20,10 @@ const getIncludeKey = (name) => {

// TODO: Use forloop.name once 11ty uses liquidjs >=9.28.0
const contextHunt = (ctx, hash, index) => {
let h = JSON.stringify(hash);
let h = stringify(hash);
for (let [k, v] of Object.entries(ctx.getAll())) {
if (!Array.isArray(v)) continue;
if (JSON.stringify(v[index]) === h) {
if (stringify(v[index]) === h) {
return k;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"dependencies": {
"@bookshop/helpers": "2.6.1",
"normalize-path": "^3.0.0"
"normalize-path": "^3.0.0",
"safe-stable-stringify": "^2.3.1"
},
"engines": {
"node": ">=14.16"
Expand Down
3 changes: 2 additions & 1 deletion javascript-modules/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ __metadata:
ava: ^3.15.0
normalize-path: ^3.0.0
nyc: ^15.1.0
safe-stable-stringify: ^2.3.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -7624,7 +7625,7 @@ resolve@~1.1.7:
languageName: node
linkType: hard

"safe-stable-stringify@npm:^2.3.0":
"safe-stable-stringify@npm:^2.3.0, safe-stable-stringify@npm:^2.3.1":
version: 2.3.1
resolution: "safe-stable-stringify@npm:2.3.1"
checksum: a0a0bad0294c3e2a9d1bf3cf2b1096dfb83c162d09a5e4891e488cce082120bd69161d2a92aae7fc48255290f17700decae9c89a07fe139794e61b5c8b411377
Expand Down

0 comments on commit 98610b4

Please sign in to comment.