UNPKG

n8n-nodes-docx-extractor

Version:
111 lines 4.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DocxExtractor = void 0; const n8n_workflow_1 = require("n8n-workflow"); const mammoth_1 = require("mammoth"); const turndown_1 = __importDefault(require("turndown")); class DocxExtractor { constructor() { this.description = { displayName: "Docx Extractor", name: "docxExtractor", icon: "file:icon.svg", group: ["transform"], version: 1, description: "Extract DOCX files to other formats", defaults: { name: "Docx Extractor" }, inputs: ["main"], outputs: ["main"], properties: [ { displayName: "Input Binary Field", name: "inputField", type: "string", default: "data", required: true, description: "The name of the input field containing the file data to be processed", }, { displayName: "Output Format", name: "outputFormat", type: "options", options: [ { name: "HTML", value: "html" }, { name: "TXT", value: "txt" }, { name: "Markdown", value: "markdown" }, { name: "All", value: "all" }, ], default: "all", description: "The format to convert the DOCX file into", }, ], }; } async execute() { var _a; const items = this.getInputData(); const output = []; const turndownService = new turndown_1.default(); const inputField = this.getNodeParameter("inputField", 0); const outputFormat = this.getNodeParameter("outputFormat", 0); for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { const item = items[itemIndex]; const binaryData = (_a = item.binary) === null || _a === void 0 ? void 0 : _a[inputField]; if (!binaryData) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `No binary data found in field "${inputField}" for item ${itemIndex + 1}`); } if (binaryData.mimeType !== "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unsupported MIME type: ${binaryData.mimeType}. Expected DOCX file.`); } try { const buffer = await this.helpers.getBinaryDataBuffer(itemIndex, inputField); let html; let textContent; let markdown; switch (outputFormat) { case "html": html = (await (0, mammoth_1.convertToHtml)({ buffer })).value; break; case "txt": textContent = (await (0, mammoth_1.extractRawText)({ buffer })).value; break; case "markdown": html = (await (0, mammoth_1.convertToHtml)({ buffer })).value; markdown = turndownService.turndown(html); break; case "all": html = (await (0, mammoth_1.convertToHtml)({ buffer })).value; textContent = (await (0, mammoth_1.extractRawText)({ buffer })).value; markdown = turndownService.turndown(html); break; default: break; } output.push({ json: { html: html, textContent: textContent, markdown: markdown, }, pairedItem: { item: itemIndex } }); } catch (error) { if (this.continueOnFail()) { output.push({ json: { error: error.message }, pairedItem: { item: itemIndex } }); } else { throw error; } } } return this.prepareOutputData(output); } } exports.DocxExtractor = DocxExtractor; //# sourceMappingURL=DocxExtractor.node.js.map