UNPKG

@iflow-mcp/doc-tools-mcp

Version:

Word 文档处理 MCP 服务器 - 基于 TypeScript 的文档处理工具

229 lines 8.94 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DocumentService = void 0; const docx_1 = require("docx"); const mammoth = __importStar(require("mammoth")); const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); class DocumentService { static instance; constructor() { } static getInstance() { if (!DocumentService.instance) { DocumentService.instance = new DocumentService(); } return DocumentService.instance; } // 创建新文档 async createDocument(filePath, options) { try { const doc = new docx_1.Document({ sections: [{ properties: {}, children: [new docx_1.Paragraph({ text: '', style: 'Normal' })] }], title: options?.title, subject: options?.subject, creator: options?.author, keywords: options?.keywords?.join(','), }); await docx_1.Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true, data: { filePath } }; } catch (error) { const err = error; return { success: false, error: `创建文档失败: ${err.message}` }; } } // 打开文档 async openDocument(filePath) { try { const buffer = await fs.readFile(filePath); const result = await mammoth.extractRawText({ buffer }); return { success: true, data: { content: result.value } }; } catch (error) { const err = error; return { success: false, error: `打开文档失败: ${err.message}` }; } } // 添加段落 async addParagraph(filePath, options) { try { const buffer = await fs.readFile(filePath); const doc = new docx_1.Document({ sections: [{ properties: {}, children: [ new docx_1.Paragraph({ text: options.text, style: options.style, alignment: options.alignment, }), ], }], }); await docx_1.Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true }; } catch (error) { const err = error; return { success: false, error: `添加段落失败: ${err.message}` }; } } // 添加表格 async addTable(filePath, options) { try { const doc = new docx_1.Document({ sections: [{ children: [ new docx_1.Table({ rows: Array(options.rows).fill(0).map((_, rowIndex) => { return new docx_1.TableRow({ children: Array(options.cols).fill(0).map((_, colIndex) => { const text = options.data?.[rowIndex]?.[colIndex] || ''; return new docx_1.TableCell({ children: [new docx_1.Paragraph({ text })], }); }), }); }), }), ], }], }); await docx_1.Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true }; } catch (error) { const err = error; return { success: false, error: `添加表格失败: ${err.message}` }; } } // 获取文档信息 async getDocumentInfo(filePath) { try { const stats = await fs.stat(filePath); const buffer = await fs.readFile(filePath); const result = await mammoth.extractRawText({ buffer }); const info = { title: path.basename(filePath), author: 'Unknown', subject: '', keywords: [], pageCount: Math.ceil(result.value.length / 3000), // 粗略估计 wordCount: result.value.split(/\s+/).length, created: stats.birthtime, modified: stats.mtime, }; return { success: true, data: info }; } catch (error) { const err = error; return { success: false, error: `获取文档信息失败: ${err.message}` }; } } // 查找和替换文本 async searchAndReplace(filePath, options) { try { const buffer = await fs.readFile(filePath); const result = await mammoth.extractRawText({ buffer }); let content = result.value; const searchRegex = new RegExp(options.searchText, `g${options.matchCase ? '' : 'i'}`); const matches = content.match(searchRegex) || []; content = content.replace(searchRegex, options.replaceText); // 注意:这里需要重新创建文档,因为 mammoth 主要用于读取 const doc = new docx_1.Document({ sections: [{ children: [new docx_1.Paragraph({ text: content })], }], }); await docx_1.Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true, data: { matchCount: matches.length, preview: content.substring(0, 200) + '...', }, }; } catch (error) { const err = error; return { success: false, error: `查找替换失败: ${err.message}` }; } } // 设置页面边距 async setPageMargins(filePath, margins) { try { const doc = new docx_1.Document({ sections: [{ properties: { page: { margin: { top: margins.top || 1440, // 默认 1 英寸 (1440 twips) right: margins.right || 1440, bottom: margins.bottom || 1440, left: margins.left || 1440, }, }, }, children: [], }], }); await docx_1.Packer.toBuffer(doc).then((buffer) => { return fs.writeFile(filePath, buffer); }); return { success: true }; } catch (error) { const err = error; return { success: false, error: `设置页面边距失败: ${err.message}` }; } } } exports.DocumentService = DocumentService; //# sourceMappingURL=DocumentService.js.map