UNPKG

n8n-nodes-document-generator

Version:

This node creates dynamic content for documents or emails with Handlebars templates

174 lines 7.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentGenerator = void 0; const fumanchu_1 = require("@jaredwray/fumanchu"); class DocumentGenerator { constructor() { this.description = { displayName: 'DocumentGenerator', name: 'documentGenerator', icon: 'file:DocumentGenerator.svg', group: ['transform'], version: 1, subtitle: '', description: 'Render data using a Handlebars template', defaults: { name: 'DocumentGenerator', }, inputs: ['main'], outputs: ['main'], credentials: [], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', options: [ { name: 'Render Template', value: 'render', description: 'Render a text template', action: 'Render a text template', }, ], default: 'render', noDataExpression: true, required: true, }, { displayName: 'Render All Items with One Template', name: 'oneTemplate', type: 'boolean', default: false, description: 'Whether to render all input items using the sample template.\nSyntax: {{#each items}}{{columnname}}{{/each}}.\nOtherwise, every item has its own template', displayOptions: { show: { operation: ['render'], }, }, }, { displayName: 'Use a Template String', name: 'useTemplateString', type: 'boolean', default: true, description: 'Whether to render all input items using a template String or a template URL', displayOptions: { show: { operation: ['render'], }, }, }, { displayName: 'Template String', name: 'template', type: 'string', required: true, typeOptions: { rows: 5, alwaysOpenEditWindow: true, }, displayOptions: { show: { useTemplateString: [true], }, }, default: '', placeholder: '{{handlebars template}}', description: 'The template string to use for rendering. Please check the <a href="https://handlebarsjs.com/guide/expressions.html#basic-usage">official page</a> for Handlebars syntax.', }, { displayName: 'Template URL', name: 'templateURL', type: 'string', required: true, displayOptions: { show: { useTemplateString: [false], }, }, default: '', placeholder: 'https://mydomain.com/emails/template.html', description: 'The template URL to use for rendering. Please check the <a href="https://handlebarsjs.com/guide/expressions.html#basic-usage">official page</a> for Handlebars syntax.', }, { displayName: 'Define a Custom Output Key', name: 'customOutputKey', type: 'boolean', default: false, description: 'Whether to define a custom output key instead of the default "text" property', displayOptions: { show: { operation: ['render'], }, }, }, { displayName: 'Output Key', name: 'outputKey', type: 'string', required: true, displayOptions: { show: { customOutputKey: [true], }, }, default: '', placeholder: 'text', description: 'The output property name where we save rendered text', } ], }; } async execute() { const items = this.getInputData(); const returnData = []; const newItemBinary = {}; const operation = this.getNodeParameter('operation', 0); const oneTemplate = this.getNodeParameter('oneTemplate', 0); const customOutputKey = this.getNodeParameter('customOutputKey', 0); const useTemplateString = this.getNodeParameter('useTemplateString', 0); let template = ''; if (useTemplateString) { template = this.getNodeParameter('template', 0); } else { const templateURL = this.getNodeParameter('templateURL', 0); template = await this.helpers.request(templateURL); } (0, fumanchu_1.helpers)({ handlebars: fumanchu_1.handlebars }, {}); const templateHelper = fumanchu_1.handlebars.compile(template); let key = 'text'; if (customOutputKey) { key = this.getNodeParameter('outputKey', 0); } if (oneTemplate) { var cleanedItems = items.map(function (item) { return item.json; }); let newItemJson = {}; newItemJson[key] = templateHelper({ items: cleanedItems }); returnData.push({ json: newItemJson }); } else { for (let i = 0; i < items.length; i++) { let item = items[i]; if (operation === 'render') { let newItemJson = {}; var rendered = templateHelper(item.json); newItemJson[key] = rendered; returnData.push({ json: newItemJson, pairedItem: { item: i, }, binary: Object.keys(newItemBinary).length === 0 ? undefined : newItemBinary, }); } } } return this.prepareOutputData(returnData); } } exports.DocumentGenerator = DocumentGenerator; //# sourceMappingURL=DocumentGenerator.node.js.map