Skip to main content
Glama

convert_markdown_to_html

Convert Markdown files to HTML with customizable themes, table of contents, and custom CSS. Outputs are saved automatically to a specified directory for streamlined document processing.

Instructions

Enhanced Markdown to HTML conversion with beautiful styling and theme support. Supports GitHub, Academic, Modern, and Default themes with complete style preservation. 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
customCSSNoAdditional custom CSS styles
includeTableOfContentsNoGenerate table of contents
markdownPathYesMarkdown file path to convert
themeNoTheme to applygithub

Implementation Reference

  • The exported entry point function for the convert_markdown_to_html tool, which instantiates the converter class and delegates to its main convertMarkdownToHtml method.
    export async function convertMarkdownToHtml( inputPath: string, options: MarkdownToHtmlOptions = {} ): Promise<MarkdownConversionResult> { const converter = new MarkdownToHtmlConverter(); return await converter.convertMarkdownToHtml(inputPath, options); }
  • Core handler logic within the MarkdownToHtmlConverter class that performs the actual Markdown to HTML conversion using marked library, applies themes, generates complete HTML documents with CSS, handles file I/O, and returns structured results.
    async convertMarkdownToHtml( inputPath: string, options: MarkdownToHtmlOptions = {} ): Promise<MarkdownConversionResult> { try { this.options = { preserveStyles: true, theme: 'default', includeTableOfContents: false, enableSyntaxHighlighting: false, standalone: true, debug: false, ...options, }; if (this.options.debug) { console.log('🚀 开始 Markdown 到 HTML 转换...'); console.log('📄 输入文件:', inputPath); console.log('🎨 使用主题:', this.options.theme); } // 读取 Markdown 文件 const markdownContent = await fs.readFile(inputPath, 'utf-8'); // 配置 marked this.configureMarked(); // 转换为 HTML const htmlContent = marked.parse(markdownContent); // 分析内容统计 const stats = this.analyzeContent(htmlContent); // 生成完整的 HTML 文档 const completeHtml = this.generateCompleteHtml(htmlContent); // 保存文件(如果指定了输出路径) let htmlPath: string | undefined; if (this.options.outputPath) { const { validateAndSanitizePath } = require('../security/securityConfig'); // 移除路径限制,允许访问任意目录(与index.ts中的validatePath函数保持一致) htmlPath = validateAndSanitizePath(this.options.outputPath, []); await fs.writeFile(htmlPath, completeHtml, 'utf-8'); if (this.options.debug) { console.log('✅ HTML 文件已保存:', htmlPath); } } if (this.options.debug) { console.log('📊 转换统计:', stats); console.log('✅ Markdown 转换完成'); } return { success: true, content: completeHtml, htmlPath, metadata: { originalFormat: 'markdown', targetFormat: 'html', stylesPreserved: this.options.preserveStyles ?? false, theme: this.options.theme ?? 'default', converter: 'markdown-to-html-converter', contentLength: completeHtml.length, ...stats, }, }; } catch (error: any) { console.error('❌ Markdown 转换失败:', error.message); return { success: false, error: error.message, }; } }
  • Input schema defining options for the Markdown to HTML conversion tool, including theme selection, style preservation, TOC inclusion, and output configuration.
    interface MarkdownToHtmlOptions { preserveStyles?: boolean; theme?: 'default' | 'github' | 'academic' | 'modern'; includeTableOfContents?: boolean; enableSyntaxHighlighting?: boolean; customCSS?: string; outputPath?: string; standalone?: boolean; debug?: boolean; }
  • Output schema for the conversion result, including success status, generated HTML content/path, metadata statistics, and error handling.
    interface MarkdownConversionResult { success: boolean; content?: string; htmlPath?: string; cssPath?: string; metadata?: { originalFormat: string; targetFormat: string; stylesPreserved: boolean; theme: string; converter: string; contentLength: number; headingsCount: number; linksCount: number; imagesCount: number; }; error?: string; }
  • Helper mapping in conversion planner that associates the tool name 'convert_markdown_to_html' with its description for planning purposes.
    convert_markdown_to_html: '将Markdown文档转换为HTML格式,应用主题样式',
  • Helper defining input parameter name 'markdownPath' for the 'convert_markdown_to_html' tool.
    convert_markdown_to_html: 'markdownPath',
  • Helper case in parameter generation switch that sets default parameters like theme='github' for the convert_markdown_to_html tool.
    case 'convert_markdown_to_html':

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