|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url); |
| 6 | +const __dirname = path.dirname(__filename); |
| 7 | + |
| 8 | +const BUILD_DIR = path.resolve(__dirname, '../build'); |
| 9 | + |
| 10 | +/** |
| 11 | + * String-based cleanup function for removing LLM button text from markdown content. |
| 12 | + */ |
| 13 | +function removeLlmButtonsFromString(markdownContent) { |
| 14 | + if (!markdownContent) return markdownContent; |
| 15 | + |
| 16 | + let cleaned = markdownContent; |
| 17 | + |
| 18 | + // Remove LLM button text patterns |
| 19 | + cleaned = cleaned.replace(/View as MarkdownCopy for LLM/g, ''); |
| 20 | + cleaned = cleaned.replace(/View as Markdown/g, ''); |
| 21 | + cleaned = cleaned.replace(/Copy for LLM/g, ''); |
| 22 | + |
| 23 | + // Remove lines that only contain LLM button text |
| 24 | + cleaned = cleaned.replace(/^[^\n]*View as Markdown[^\n]*$/gm, ''); |
| 25 | + cleaned = cleaned.replace(/^[^\n]*Copy for LLM[^\n]*$/gm, ''); |
| 26 | + |
| 27 | + // Clean up excessive whitespace and empty lines |
| 28 | + cleaned = cleaned.replace(/\n{3,}/g, '\n\n'); // Replace 3+ newlines with 2 |
| 29 | + cleaned = cleaned.replace(/^\s+$/gm, ''); // Remove lines that are only whitespace |
| 30 | + cleaned = cleaned.replace(/\n\s*\n\s*\n/g, '\n\n'); // Clean up multiple empty lines |
| 31 | + |
| 32 | + return cleaned.trim(); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Recursively find and process all .md files in the build directory. |
| 37 | + */ |
| 38 | +async function processMarkdownFiles(dir) { |
| 39 | + try { |
| 40 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 41 | + |
| 42 | + for (const entry of entries) { |
| 43 | + const fullPath = path.join(dir, entry.name); |
| 44 | + |
| 45 | + if (entry.isDirectory()) { |
| 46 | + // Recursively process subdirectories |
| 47 | + await processMarkdownFiles(fullPath); |
| 48 | + } else if (entry.name.endsWith('.md')) { |
| 49 | + // Process markdown files |
| 50 | + console.log(`Processing: ${fullPath}`); |
| 51 | + |
| 52 | + try { |
| 53 | + const content = await fs.readFile(fullPath, 'utf8'); |
| 54 | + const cleanedContent = removeLlmButtonsFromString(content); |
| 55 | + |
| 56 | + if (content !== cleanedContent) { |
| 57 | + await fs.writeFile(fullPath, cleanedContent, 'utf8'); |
| 58 | + console.log(` ✓ Cleaned: ${fullPath}`); |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + console.error(` ✗ Error processing ${fullPath}:`, error.message); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + console.error(`Error reading directory ${dir}:`, error.message); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Main function to clean up markdown files. |
| 72 | + */ |
| 73 | +async function cleanupMarkdownFiles() { |
| 74 | + console.log('Starting markdown cleanup...'); |
| 75 | + |
| 76 | + if (!await fs.stat(BUILD_DIR).catch(() => false)) { |
| 77 | + console.error(`Build directory not found: ${BUILD_DIR}`); |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | + |
| 81 | + await processMarkdownFiles(BUILD_DIR); |
| 82 | + console.log('Markdown cleanup completed!'); |
| 83 | +} |
| 84 | + |
| 85 | +// Run the cleanup if this script is executed directly |
| 86 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 87 | + cleanupMarkdownFiles().catch(console.error); |
| 88 | +} |
| 89 | + |
| 90 | +export { cleanupMarkdownFiles, removeLlmButtonsFromString }; |
0 commit comments