Skip to main content
Glama

convert_markdown_to_docx

Convert Markdown files to professionally styled DOCX documents with preserved formatting. Supports tables, lists, headings, and themes like professional, academic, and modern. Automatically saves outputs to a specified directory.

Instructions

Enhanced Markdown to DOCX conversion with professional styling and theme support. Preserves formatting, supports tables, lists, headings, and inline formatting with beautiful Word document output. Output directory is controlled by OUTPUT_DIR environment variable. Files will be automatically saved to OUTPUT_DIR with auto-generated names.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
includeTableOfContentsNoGenerate table of contents
markdownPathYesMarkdown file path to convert
preserveStylesNoPreserve Markdown formatting and styles
themeNoTheme to applyprofessional

Implementation Reference

  • Core handler method in MarkdownToDocxConverter class that executes the Markdown to DOCX conversion: reads input file, parses Markdown, generates styled DOCX document using docx library, handles output and metadata.
    async convertMarkdownToDocx( inputPath: string, options: MarkdownToDocxOptions = {} ): Promise<MarkdownToDocxResult> { try { this.options = { preserveStyles: true, theme: 'default', includeTableOfContents: false, debug: false, ...options, }; if (this.options.debug) { console.log('🚀 开始 Markdown 到 DOCX 转换...'); console.log('📄 输入文件:', inputPath); console.log('🎨 使用主题:', this.options.theme); } // 读取 Markdown 文件 const markdownContent = await fs.readFile(inputPath, 'utf-8'); // 解析 Markdown 内容 const parsedElements = await this.parseMarkdown(markdownContent); // 生成 DOCX 文档 const docxDocument = await this.generateDocxDocument(parsedElements); // 生成文档缓冲区 const docxBuffer = await Packer.toBuffer(docxDocument); // 保存文件(如果指定了输出路径) let docxPath: string | undefined; if (this.options.outputPath) { const { validateAndSanitizePath } = require('../security/securityConfig'); const allowedPaths = [process.cwd()]; const validatedPath = validateAndSanitizePath(this.options.outputPath, allowedPaths); if (validatedPath) { docxPath = validatedPath; await fs.writeFile(validatedPath, docxBuffer); } if (this.options.debug) { console.log('✅ DOCX 文件已保存:', docxPath); } } // 分析内容统计 const stats = this.analyzeContent(parsedElements); if (this.options.debug) { console.log('📊 转换统计:', stats); console.log('✅ Markdown 转换完成'); } return { success: true, content: docxBuffer, docxPath, metadata: { originalFormat: 'markdown', targetFormat: 'docx', stylesPreserved: this.options.preserveStyles ?? false, theme: this.options.theme ?? 'default', converter: 'markdown-to-docx-converter', contentLength: docxBuffer.length, ...stats, }, }; } catch (error: any) { console.error('❌ Markdown 转换失败:', error.message); return { success: false, error: error.message, }; } }
  • Input schema defining options for the conversion tool such as styles preservation, themes, custom styles, output path.
    interface MarkdownToDocxOptions { preserveStyles?: boolean; theme?: 'default' | 'professional' | 'academic' | 'modern'; includeTableOfContents?: boolean; customStyles?: { fontSize?: number; fontFamily?: string; lineSpacing?: number; margins?: { top?: number; bottom?: number; left?: number; right?: number; }; }; outputPath?: string; debug?: boolean; }
  • Exported wrapper function serving as the primary MCP tool handler entry point for 'convert_markdown_to_docx'.
    export async function convertMarkdownToDocx( inputPath: string, options: MarkdownToDocxOptions = {} ): Promise<MarkdownToDocxResult> { const converter = new MarkdownToDocxConverter(); return await converter.convertMarkdownToDocx(inputPath, options); }
  • Registration/mapping of tool name 'convert_markdown_to_docx' in direct conversions for markdown/md to docx formats.
    private directConversions: Record<string, Record<string, string>> = { docx: { pdf: 'convert_docx_to_pdf', html: 'convert_document', markdown: 'convert_document', md: 'convert_document', txt: 'convert_document', }, markdown: { html: 'convert_markdown_to_html', docx: 'convert_markdown_to_docx', pdf: 'convert_markdown_to_html', // 需要两步:md->html->pdf txt: 'convert_document', }, md: { html: 'convert_markdown_to_html', docx: 'convert_markdown_to_docx', pdf: 'convert_markdown_to_html', // 需要两步:md->html->pdf txt: 'convert_document', }, html: { markdown: 'convert_html_to_markdown', md: 'convert_html_to_markdown', docx: 'convert_document', txt: 'convert_document', pdf: 'convert_document', // 需要外部工具 }, pdf: { txt: 'read_document', html: 'read_document', // 读取后写入 markdown: 'read_document', md: 'read_document', docx: 'read_document', }, txt: { html: 'write_document', markdown: 'write_document', md: 'write_document', docx: 'write_document', pdf: 'write_document', }, };
  • Schema/parameter definition for 'convert_markdown_to_docx' tool in conversion planning, specifying theme and preserveStyles parameters.
    case 'convert_markdown_to_docx': params.theme = request.requirements?.theme ?? 'professional'; params.preserveStyles = request.requirements?.preserveStyles !== false; break;

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Tele-AI/doc-ops-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server