@bitovi/n8n-nodes-markitdown
Version:
n8n node to process files with Markitdown
121 lines • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Markitdown = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const fs_extra_1 = require("fs-extra");
const tmp_promise_1 = require("tmp-promise");
const child_process_1 = require("child_process");
const util_1 = require("util");
const execPromise = (0, util_1.promisify)(child_process_1.exec);
async function checkMarkitdownAvailability(node) {
try {
await execPromise('markitdown --version');
return 'markitdown';
}
catch (error) {
try {
const { stdout } = await execPromise('which markitdown 2>/dev/null || find /usr -name markitdown 2>/dev/null | head -1');
const markitdownPath = stdout.trim();
if (markitdownPath) {
await execPromise(`${markitdownPath} --version`);
return markitdownPath;
}
}
catch (findError) {
}
throw new n8n_workflow_1.NodeOperationError(node, 'markitdown command not found. Please ensure Python and markitdown are installed:\n' +
'1. Install Python 3: https://python.org\n' +
'2. Install markitdown: pip install markitdown\n' +
'3. Ensure markitdown is in your PATH\n' +
'Original error: ' + error.message);
}
}
class Markitdown {
constructor() {
this.description = {
displayName: 'Markitdown',
name: 'markitdown',
icon: 'file:microsoft.svg',
group: ['transform'],
version: 1,
description: 'Convert any file into markdown',
defaults: {
name: 'Markitdown',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Input Binary Field',
name: 'inputBinaryField',
type: 'string',
default: 'data',
required: true,
description: 'Name of the binary property containing the file to process',
}
],
};
}
async execute() {
let markitdownCommand;
try {
markitdownCommand = await checkMarkitdownAvailability(this.getNode());
}
catch (error) {
throw error;
}
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const inputBinaryField = this.getNodeParameter('inputBinaryField', i);
const binaryData = this.helpers.assertBinaryData(i, inputBinaryField);
const inputTmpFile = await (0, tmp_promise_1.file)({
prefix: 'n8n-markitdown-input-',
postfix: binaryData.fileName
});
let uploadData;
if (binaryData.id) {
console.log('This file has an ID, fetching binary data from n8n storage.');
uploadData = await this.helpers.getBinaryStream(binaryData.id);
}
else {
uploadData = Buffer.from(binaryData.data, n8n_workflow_1.BINARY_ENCODING);
}
await fs_extra_1.promises.writeFile(inputTmpFile.path, uploadData);
const outputTmpFile = await (0, tmp_promise_1.file)({
prefix: 'n8n-markitdown-output-',
postfix: '.md',
});
const command = `${markitdownCommand} "${inputTmpFile.path}" -o "${outputTmpFile.path}"`.trim();
await execPromise(command);
const outputContent = await fs_extra_1.promises.readFile(outputTmpFile.path, 'utf-8');
const newItem = {
json: {
data: outputContent
},
binary: {},
};
returnData.push(newItem);
await Promise.all([
inputTmpFile.cleanup(),
outputTmpFile.cleanup()
]);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.Markitdown = Markitdown;
//# sourceMappingURL=Markitdown.node.js.map