const { marked } = require('marked'); const fs = require('fs'); const path = require('path'); // Configure marked options const options = { headerIds: true, gfm: true }; // Footer template const footer = ` `; // Function to convert markdown to HTML async function convertMarkdownToHtml(mdFilePath, outputPath) { try { // Read markdown file const markdown = await fs.promises.readFile(mdFilePath, 'utf8'); // Extract metadata from markdown (assuming front matter) const metadata = {}; const content = markdown.replace(/^---\n([\s\S]*?)\n---\n/, (_, frontMatter) => { frontMatter.split('\n').forEach(line => { const [key, ...valueParts] = line.split(':'); if (key && valueParts.length > 0) { metadata[key.trim()] = valueParts.join(':').trim(); } }); return ''; }); // Configure marked options for proper heading rendering const markedOptions = { headerIds: true, gfm: true, breaks: true, pedantic: false, smartLists: true, smartypants: true }; // Convert markdown to HTML const articleContent = marked.parse(content, markedOptions); // Calculate read time (rough estimate: 200 words per minute) const wordCount = content.trim().split(/\s+/).length; const readTime = Math.ceil(wordCount / 200); // Create full HTML document const html = ` ${metadata.title || 'Blog Post'} - Glenn Thompson

${metadata.title || 'Blog Post'}

${readTime} min read By ${metadata.author || 'Glenn Thompson'}
${metadata.tags ? `
${metadata.tags.split(',').map(tag => `${tag.trim()}` ).join('')}
` : ''}
${articleContent}
${footer}`; // Write HTML file const htmlPath = outputPath || mdFilePath.replace('.md', '.html'); await fs.promises.writeFile(htmlPath, html); console.log(`Converted ${mdFilePath} to ${htmlPath}`); } catch (error) { console.error('Error converting markdown to HTML:', error); process.exit(1); } } // If running from command line if (require.main === module) { const mdFilePath = process.argv[2]; const outputPath = process.argv[3]; if (!mdFilePath) { console.error('Please provide a markdown file path'); process.exit(1); } convertMarkdownToHtml(mdFilePath, outputPath); }