|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Reads suggested-tools.md and injects generated HTML into each page. |
| 4 | + * |
| 5 | + * Each page must contain marker comments: |
| 6 | + * <!-- SUGGESTED_TOOLS_START --> ... <!-- SUGGESTED_TOOLS_END --> |
| 7 | + * |
| 8 | + * The home page gets a standalone <section>, while tool sub-pages get |
| 9 | + * inline links appended to their existing "Related tools:" footer line. |
| 10 | + */ |
| 11 | + |
| 12 | +const fs = require('fs'); |
| 13 | +const path = require('path'); |
| 14 | + |
| 15 | +const ROOT = path.resolve(__dirname, '..'); |
| 16 | +const MD_PATH = path.join(ROOT, 'suggested-tools.md'); |
| 17 | + |
| 18 | +// Map section names in the markdown to their HTML file paths and render mode. |
| 19 | +const PAGE_MAP = { |
| 20 | + home: { file: 'index.html', mode: 'section' }, |
| 21 | + formatter: { file: 'formatter/index.html', mode: 'inline' }, |
| 22 | + 'og-image': { file: 'og-image/index.html', mode: 'inline' }, |
| 23 | +}; |
| 24 | + |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | +// Parse the markdown |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +function parseMarkdown(src) { |
| 29 | + const sections = {}; |
| 30 | + let current = null; |
| 31 | + |
| 32 | + for (const raw of src.split('\n')) { |
| 33 | + const line = raw.trim(); |
| 34 | + |
| 35 | + // H2 heading = page key |
| 36 | + const h2 = line.match(/^##\s+(.+)$/); |
| 37 | + if (h2) { |
| 38 | + current = h2[1].trim().toLowerCase(); |
| 39 | + sections[current] = []; |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + // Markdown link inside a list item |
| 44 | + if (current && /^-\s+\[/.test(line)) { |
| 45 | + const match = line.match(/\[([^\]]+)\]\(([^)]+)\)/); |
| 46 | + if (match) { |
| 47 | + sections[current].push({ name: match[1], url: match[2] }); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return sections; |
| 53 | +} |
| 54 | + |
| 55 | +// --------------------------------------------------------------------------- |
| 56 | +// Render HTML for each mode |
| 57 | +// --------------------------------------------------------------------------- |
| 58 | +function renderSection(tools) { |
| 59 | + if (tools.length === 0) return ''; |
| 60 | + const items = tools |
| 61 | + .map(t => |
| 62 | + ` <li><a href="${t.url}" target="_blank" rel="noopener">${t.name} <span class="external-icon" aria-hidden="true">↗</span></a></li>` |
| 63 | + ) |
| 64 | + .join('\n'); |
| 65 | + return [ |
| 66 | + ' <section class="suggested-tools">', |
| 67 | + ' <h2 class="section-heading">Suggested Tools</h2>', |
| 68 | + ' <ul class="suggested-list">', |
| 69 | + items, |
| 70 | + ' </ul>', |
| 71 | + ' </section>', |
| 72 | + ].join('\n'); |
| 73 | +} |
| 74 | + |
| 75 | +function renderInline(tools) { |
| 76 | + if (tools.length === 0) return ''; |
| 77 | + return tools |
| 78 | + .map(t => |
| 79 | + ` <a href="${t.url}" target="_blank" rel="noopener">${t.name}</a>` |
| 80 | + ) |
| 81 | + .join(' ·\n'); |
| 82 | +} |
| 83 | + |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +// Inject between markers |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | +const START = '<!-- SUGGESTED_TOOLS_START -->'; |
| 88 | +const END = '<!-- SUGGESTED_TOOLS_END -->'; |
| 89 | + |
| 90 | +function inject(html, rendered) { |
| 91 | + const startIdx = html.indexOf(START); |
| 92 | + const endIdx = html.indexOf(END); |
| 93 | + if (startIdx === -1 || endIdx === -1) { |
| 94 | + return null; // markers not found |
| 95 | + } |
| 96 | + return ( |
| 97 | + html.slice(0, startIdx + START.length) + |
| 98 | + '\n' + rendered + '\n' + |
| 99 | + html.slice(endIdx) |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +// --------------------------------------------------------------------------- |
| 104 | +// Main |
| 105 | +// --------------------------------------------------------------------------- |
| 106 | +const md = fs.readFileSync(MD_PATH, 'utf-8'); |
| 107 | +const sections = parseMarkdown(md); |
| 108 | + |
| 109 | +let changed = 0; |
| 110 | + |
| 111 | +for (const [key, config] of Object.entries(PAGE_MAP)) { |
| 112 | + const tools = sections[key]; |
| 113 | + if (!tools || tools.length === 0) { |
| 114 | + console.log(` skip ${config.file} (no tools listed under "${key}")`); |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + const filePath = path.join(ROOT, config.file); |
| 119 | + const html = fs.readFileSync(filePath, 'utf-8'); |
| 120 | + |
| 121 | + const rendered = config.mode === 'section' |
| 122 | + ? renderSection(tools) |
| 123 | + : renderInline(tools); |
| 124 | + |
| 125 | + const result = inject(html, rendered); |
| 126 | + if (result === null) { |
| 127 | + console.error(` ERROR ${config.file}: missing ${START} / ${END} markers`); |
| 128 | + process.exit(1); |
| 129 | + } |
| 130 | + |
| 131 | + fs.writeFileSync(filePath, result, 'utf-8'); |
| 132 | + console.log(` wrote ${config.file} (${tools.length} tools)`); |
| 133 | + changed++; |
| 134 | +} |
| 135 | + |
| 136 | +console.log(`\nDone — updated ${changed} file(s).`); |
0 commit comments