docx-writer.ts
1 import { Document, Packer, Paragraph, TextRun } from "docx"; 2 3 export interface CreateDOCXOptions { 4 title?: string; 5 subject?: string; 6 creator?: string; 7 keywords?: string[]; 8 description?: string; 9 } 10 11 /** 12 * Create simple DOCX from plain text content 13 * This is the main function called by write_file tool 14 */ 15 export async function createSimpleDOCX( 16 content: string, 17 options: CreateDOCXOptions = {} 18 ): Promise<Buffer> { 19 // Split content into paragraphs by newlines 20 const lines = content.split("\n"); 21 22 // Create paragraphs from each line 23 const paragraphs = lines.map( 24 (line) => 25 new Paragraph({ 26 children: [new TextRun(line || " ")], // Use space for empty lines to preserve spacing 27 }) 28 ); 29 30 // Create document with metadata 31 const doc = new Document({ 32 creator: options.creator || "vulcan-file-ops", 33 title: options.title, 34 subject: options.subject, 35 keywords: options.keywords?.join(", "), 36 description: options.description, 37 sections: [ 38 { 39 properties: {}, 40 children: paragraphs, 41 }, 42 ], 43 }); 44 45 // Convert to buffer 46 return Buffer.from(await Packer.toBuffer(doc)); 47 }