|
| 1 | +const fs = require('fs'); |
| 2 | +const { parse } = require('node-html-parser'); |
| 3 | +const { createTurndownService } = require('./turndown-config'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Generic function to generate markdown from HTML index pages. |
| 7 | + * |
| 8 | + * @param {Object} config - Configuration object |
| 9 | + * @param {string} config.htmlPath - Path to the HTML file (e.g., 'public/components/next/index.html') |
| 10 | + * @param {string} config.title - Title for the markdown file (e.g., 'Components Index') |
| 11 | + * @param {string} config.description - Description text (e.g., 'List of all Camel components') |
| 12 | + */ |
| 13 | +async function generateHtmlIndex(config) { |
| 14 | + const { htmlPath, title, description } = config; |
| 15 | + const mdPath = htmlPath.replace(/\.html$/, '.md'); |
| 16 | + |
| 17 | + try { |
| 18 | + // Check if file exists |
| 19 | + if (!fs.existsSync(htmlPath)) { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + const htmlContent = fs.readFileSync(htmlPath, 'utf8'); |
| 24 | + const root = parse(htmlContent); |
| 25 | + |
| 26 | + // Create turndown service |
| 27 | + const turndownService = createTurndownService(); |
| 28 | + |
| 29 | + // Extract only the main article content |
| 30 | + let mainContent = root.querySelector('article.doc') || |
| 31 | + root.querySelector('main') || |
| 32 | + root.querySelector('.article') || |
| 33 | + root.querySelector('article'); |
| 34 | + |
| 35 | + if (!mainContent) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Remove navigation elements |
| 40 | + const elementsToRemove = mainContent.querySelectorAll('nav, header, footer, .nav, .navbar, .toolbar'); |
| 41 | + elementsToRemove.forEach(el => el.remove()); |
| 42 | + |
| 43 | + // Remove anchor links |
| 44 | + const anchors = mainContent.querySelectorAll('a.anchor'); |
| 45 | + anchors.forEach(el => el.remove()); |
| 46 | + |
| 47 | + // Clean up table cells by unwrapping div.content and div.paragraph wrappers |
| 48 | + const tableCells = mainContent.querySelectorAll('td.tableblock, th.tableblock'); |
| 49 | + tableCells.forEach(cell => { |
| 50 | + let html = cell.innerHTML; |
| 51 | + // Unwrap <div class="content"><div class="paragraph"><p>...</p></div></div> |
| 52 | + html = html.replace(/<div class="content"><div class="paragraph">\s*<p>(.*?)<\/p>\s*<\/div><\/div>/gs, '$1'); |
| 53 | + // Unwrap <div class="content"><div id="..." class="paragraph"><p>...</p></div></div> |
| 54 | + html = html.replace(/<div class="content"><div[^>]*class="paragraph"[^>]*>\s*<p>(.*?)<\/p>\s*<\/div><\/div>/gs, '$1'); |
| 55 | + // Also handle simple <p class="tableblock">...</p> wrappers |
| 56 | + html = html.replace(/<p class="tableblock">(.*?)<\/p>/gs, '$1'); |
| 57 | + cell.set_content(html); |
| 58 | + }); |
| 59 | + |
| 60 | + // Convert to Markdown |
| 61 | + let markdown = turndownService.turndown(mainContent.innerHTML); |
| 62 | + |
| 63 | + // Update links to point to .md files instead of .html |
| 64 | + // Replace https://camel.apache.org/**/*.html with https://camel.apache.org/**/*.md |
| 65 | + markdown = markdown.replace(/(https:\/\/camel\.apache\.org\/[^)\s]*?)\.html/g, '$1.md'); |
| 66 | + // Replace relative links *.html with *.md |
| 67 | + markdown = markdown.replace(/\[([^\]]+)\]\(([^)]+?)\.html\)/g, '[$1]($2.md)'); |
| 68 | + |
| 69 | + // Add header if title and description provided |
| 70 | + if (title && description) { |
| 71 | + markdown = `# ${title}\n\n${description}\n\n${markdown}`; |
| 72 | + } |
| 73 | + |
| 74 | + // Write markdown file |
| 75 | + fs.writeFileSync(mdPath, markdown, 'utf8'); |
| 76 | + } catch (error) { |
| 77 | + console.error(`Error generating markdown for ${htmlPath}:`, error.message); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Generates markdown for all index files (HTML index pages). |
| 83 | + * This function processes all the index files specified in the configuration. |
| 84 | + */ |
| 85 | +async function generateAllIndexes() { |
| 86 | + console.log('\nGenerating markdown for all index files...'); |
| 87 | + |
| 88 | + const glob = require('glob'); |
| 89 | + |
| 90 | + // Define all HTML index files to process |
| 91 | + const htmlIndexes = [ |
| 92 | + { |
| 93 | + htmlPath: 'public/camel-k/next/index.html', |
| 94 | + title: 'Camel K Documentation Index', |
| 95 | + description: 'Index of Camel K documentation pages.' |
| 96 | + }, |
| 97 | + { |
| 98 | + htmlPath: 'public/camel-kafka-connector/next/index.html', |
| 99 | + title: 'Camel Kafka Connector Documentation Index', |
| 100 | + description: 'Index of Camel Kafka Connector documentation pages.' |
| 101 | + }, |
| 102 | + { |
| 103 | + htmlPath: 'public/camel-kamelets/next/index.html', |
| 104 | + title: 'Camel Kamelets Documentation Index', |
| 105 | + description: 'Index of Camel Kamelets documentation pages.' |
| 106 | + }, |
| 107 | + { |
| 108 | + htmlPath: 'public/camel-quarkus/next/index.html', |
| 109 | + title: 'Camel Quarkus Documentation Index', |
| 110 | + description: 'Index of Camel Quarkus documentation pages.' |
| 111 | + }, |
| 112 | + { |
| 113 | + htmlPath: 'public/camel-spring-boot/next/index.html', |
| 114 | + title: 'Camel Spring Boot Documentation Index', |
| 115 | + description: 'Index of Camel Spring Boot documentation pages.' |
| 116 | + }, |
| 117 | + { |
| 118 | + htmlPath: 'public/components/next/index.html', |
| 119 | + title: 'Components Index', |
| 120 | + description: 'Index of all Camel components.' |
| 121 | + }, |
| 122 | + { |
| 123 | + htmlPath: 'public/components/next/others/index.html', |
| 124 | + title: 'Other Components Index', |
| 125 | + description: 'Index of other Camel components.' |
| 126 | + }, |
| 127 | + { |
| 128 | + htmlPath: 'public/components/next/languages/index.html', |
| 129 | + title: 'Languages Index', |
| 130 | + description: 'Index of Camel expression and predicate languages.' |
| 131 | + }, |
| 132 | + { |
| 133 | + htmlPath: 'public/components/next/eips/index.html', |
| 134 | + title: 'Enterprise Integration Patterns Index', |
| 135 | + description: 'Index of Enterprise Integration Patterns (EIPs).' |
| 136 | + }, |
| 137 | + { |
| 138 | + htmlPath: 'public/components/next/dataformats/index.html', |
| 139 | + title: 'Data Formats Index', |
| 140 | + description: 'Index of Camel data formats.' |
| 141 | + }, |
| 142 | + { |
| 143 | + htmlPath: 'public/manual/index.html', |
| 144 | + title: 'User Manual Index', |
| 145 | + description: 'Index of Apache Camel user manual pages.' |
| 146 | + }, |
| 147 | + { |
| 148 | + htmlPath: 'public/manual/faq/index.html', |
| 149 | + title: 'FAQ Index', |
| 150 | + description: 'Frequently Asked Questions about Apache Camel.' |
| 151 | + }, |
| 152 | + { |
| 153 | + htmlPath: 'public/releases/index.html', |
| 154 | + title: 'Releases Index', |
| 155 | + description: 'Apache Camel version releases Index.' |
| 156 | + } |
| 157 | + ]; |
| 158 | + |
| 159 | + // Process all HTML indexes |
| 160 | + for (const config of htmlIndexes) { |
| 161 | + await generateHtmlIndex(config); |
| 162 | + } |
| 163 | + |
| 164 | + // Find all index.html files under public/releases/**/ |
| 165 | + console.log('\nGenerating markdown for all release index files...'); |
| 166 | + const releaseIndexFiles = glob.sync('public/releases/**/index.html'); |
| 167 | + console.log(`Found ${releaseIndexFiles.length} release index files to process`); |
| 168 | + |
| 169 | + // Process each release index file without custom title/description |
| 170 | + for (const htmlPath of releaseIndexFiles) { |
| 171 | + await generateHtmlIndex({ htmlPath }); |
| 172 | + } |
| 173 | + |
| 174 | + console.log('All index files generation complete'); |
| 175 | +} |
| 176 | + |
| 177 | +module.exports = { |
| 178 | + generateHtmlIndex, |
| 179 | + generateAllIndexes |
| 180 | +}; |
0 commit comments