From 05ab43bbb269abe97da0fefd0d49ff52feb13dfc Mon Sep 17 00:00:00 2001 From: "C. J. Tantay" Date: Sun, 30 Jun 2024 20:23:43 -0700 Subject: [PATCH] fix bugs --- lib/index.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/index.js b/lib/index.js index 58cf7ee0..1fac7fb8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -50,6 +50,7 @@ import { SassEngine } from './engines/sass.js'; * @property {string} layouts * @property {{[key: string]: (...args) => unknown}} [nunjucksVariables] * @property {{[key: string]: (...args) => unknown}} [nunjucksFilters] + * @property {function(string): function(Object.[]): Object.[]} nunjucksFilters.prepareCrosswalk * @property {{[key: string]: (...args) => unknown}} [nunjucksTags] * @property {{[key: string]: (...args) => unknown}} [minifyOptions] * @property {string} output @@ -213,15 +214,15 @@ export class Baker extends EventEmitter { // if an object of custom nunjucks filters was provided, add them now if (nunjucksFilters) { + if (crosswalkPath && nunjucksFilters.prepareCrosswalk) { + throw new Error('crosswalkPath and prepareCrosswalk cannot both be defined in nunjucksFilters'); + } + // // Use prepareCrosswalk from nunjucksFilters if available, otherwise check for crosswalkPath - // if (nunjucksFilters.prepareCrosswalk) { - // this.nunjucks.addCustomFilters(nunjucksFilters); - // } else if (crosswalkPath) { - // this.nunjucks.addCustomFilter( - // 'prepareCrosswalk', - // this.prepareCrosswalk(crosswalkPath) - // ); - // } + if (crosswalkPath && !nunjucksFilters.prepareCrosswalk) { + nunjucksFilters.prepareCrosswalk = this.prepareCrosswalk(crosswalkPath); + } + this.nunjucks.addCustomFilters(nunjucksFilters); } @@ -483,11 +484,21 @@ export class Baker extends EventEmitter { this.emit('bake:end'); } + /** + * Prepares a function to process crosswalk image paths. + * @param {string} crosswalkPath - The base path for crosswalk images. + * @returns {Function} A function that maps over data entries to update image paths. + */ prepareCrosswalk(/** @type {string} */ crosswalkPath) { - // set absolute path for crosswalk images + /** + * Processes data entries to set appropriate paths for images. + * @param {Object.[]} data - Array of objects, each containing image references. + * @returns {Object.[]} The data with updated image paths. + */ return function setCrosswalkPath( /** @type {Object.[]} */ data ) { + /** @type {Object.[]} */ data return data.map((d) => { const relativeStaticPath = this.getStaticPath( join(crosswalkPath, d.img)