diff --git a/src/Serverless.js b/src/Serverless.js index fd5b96aad..b7b246fed 100644 --- a/src/Serverless.js +++ b/src/Serverless.js @@ -155,7 +155,7 @@ class Serverless { return {}; } - async render() { + async getOutput() { if (this.dir.startsWith("/var/task/")) { process.chdir(this.dir); } @@ -220,9 +220,17 @@ class Serverless { // TODO (@zachleat) https://github.com/11ty/eleventy/issues/1957 this.deleteEnvironmentVariables(); + return json.filter((entry) => { + return entry.inputPath === inputPath; + }); + } + + async render() { + let json = await this.getOutput(); + if (!json.length) { let err = new Error( - `Couldn’t find any generated output from Eleventy (URL path parameters: ${JSON.stringify( + `Couldn’t find any generated output from Eleventy (Input path: ${inputPath}, URL path parameters: ${JSON.stringify( pathParams )}).` ); @@ -230,17 +238,7 @@ class Serverless { throw err; } - for (let entry of json) { - if (entry.inputPath === inputPath) { - return entry.content; - } - } - - // Log to Serverless Function output - console.log(json); - throw new Error( - `Couldn’t find any matching output from Eleventy for ${inputPath} (${json.length} pages rendered).` - ); + return json[0].content; } } diff --git a/src/Template.js b/src/Template.js index 07c88d9e4..3dc6f6074 100755 --- a/src/Template.js +++ b/src/Template.js @@ -3,6 +3,8 @@ const os = require("os"); const path = require("path"); const normalize = require("normalize-path"); const isPlainObject = require("lodash/isPlainObject"); +const lodashGet = require("lodash/get"); +const lodashSet = require("lodash/set"); const { DateTime } = require("luxon"); const TemplateData = require("./TemplateData"); @@ -805,6 +807,15 @@ class Template extends TemplateContent { return content; } + retrieveDataForJsonOutput(data, selectors) { + let filtered = {}; + for (let selector of selectors) { + let value = lodashGet(data, selector); + lodashSet(filtered, selector, value); + } + return filtered; + } + async generateMapEntry(mapEntry, to) { return Promise.all( mapEntry._pages.map(async (page) => { @@ -824,6 +835,16 @@ class Template extends TemplateContent { content: content, }; + if ( + this.config.dataFilterSelectors && + this.config.dataFilterSelectors.size > 0 + ) { + obj.data = this.retrieveDataForJsonOutput( + page.data, + this.config.dataFilterSelectors + ); + } + if (to === "ndjson") { let jsonString = JSON.stringify(obj); this.logger.toStream(jsonString + os.EOL); diff --git a/src/UserConfig.js b/src/UserConfig.js index b938702b2..0c0ed8d0f 100644 --- a/src/UserConfig.js +++ b/src/UserConfig.js @@ -77,6 +77,7 @@ class UserConfig { this._pluginExecution = false; this.useTemplateCache = true; + this.dataFilterSelectors = new Set(); } versionCheck(expected) { @@ -770,6 +771,7 @@ class UserConfig { plugins: this.plugins, useTemplateCache: this.useTemplateCache, precompiledCollections: this.precompiledCollections, + dataFilterSelectors: this.dataFilterSelectors, }; } }