UNPKG

n8n-nodes-docxtotext

Version:

n8n node to convert DOCX files to text using mammoth

130 lines 5.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocxToText = void 0; const n8n_workflow_1 = require("n8n-workflow"); const mammoth = __importStar(require("mammoth")); class DocxToText { constructor() { this.description = { displayName: 'DOCX to Text', name: 'docxToText', icon: 'file:docx-to-text.svg', group: ['input'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Convert DOCX files to formatted text', defaults: { name: 'DOCX to Text', }, inputs: ["main"], outputs: ["main"], properties: [ { displayName: 'Binary Property', name: 'binaryPropertyName', type: 'string', default: 'data', required: true, description: 'Name of the binary property which contains the DOCX file', }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Option', default: {}, options: [ { displayName: 'Include Filename', name: 'includeFilename', type: 'boolean', default: true, description: 'Whether to include the filename in the output', }, ], }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const length = items.length; const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0); const options = this.getNodeParameter('options', 0, {}); for (let i = 0; i < length; i++) { try { const item = items[i]; const binaryData = item.binary; if (binaryData === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No binary data exists on item!', { itemIndex: i }); } if (!binaryData[binaryPropertyName]) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" does not exists on item!`, { itemIndex: i }); } const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName); const result = await mammoth.extractRawText({ buffer: binaryDataBuffer }); const text = result.value; const messages = result.messages; const newItem = { json: { text, messages, }, }; if (options.includeFilename && binaryData[binaryPropertyName].fileName) { const fullFilename = binaryData[binaryPropertyName].fileName; const lastDotIndex = fullFilename.lastIndexOf('.'); if (lastDotIndex > 0) { newItem.json.filename = fullFilename.substring(0, lastDotIndex); newItem.json.fileExtension = fullFilename.substring(lastDotIndex + 1).toLowerCase(); } else { newItem.json.filename = fullFilename; } } if (item.json !== undefined) { newItem.json = { ...item.json, ...newItem.json }; } returnData.push(newItem); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, }, }); continue; } throw error; } } return this.prepareOutputData(returnData); } } exports.DocxToText = DocxToText; //# sourceMappingURL=DocxToText.node.js.map