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
| Name | Required | Description | Default |
|---|---|---|---|
| customCSS | No | Additional custom CSS styles | |
| includeTableOfContents | No | Generate table of contents | |
| markdownPath | Yes | Markdown file path to convert | |
| theme | No | Theme to apply | github |
Input Schema (JSON Schema)
{
"properties": {
"customCSS": {
"description": "Additional custom CSS styles",
"type": "string"
},
"includeTableOfContents": {
"default": false,
"description": "Generate table of contents",
"type": "boolean"
},
"markdownPath": {
"description": "Markdown file path to convert",
"type": "string"
},
"theme": {
"default": "github",
"description": "Theme to apply",
"enum": [
"default",
"github",
"academic",
"modern"
],
"type": "string"
}
},
"required": [
"markdownPath"
],
"type": "object"
}
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':