UNPKG

scai

Version:

> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.

91 lines (90 loc) 3.88 kB
import chalk from 'chalk'; function isTopOrBottomNoise(line) { const trimmed = line.trim(); if (/^```(?:\w+)?$/.test(trimmed)) return true; if (/^<!--.*-->$/.test(trimmed)) return true; const lower = trimmed.toLowerCase(); if (!trimmed.startsWith('//') && !trimmed.startsWith('/*')) { return [ /^i\s/i, /^here/, /^this/, /^the following/, /^below/, /^in this/, /^we have/, /the code above/, /ensures that/, /it handles/, /used to/, /note that/, /example/, /summary/, /added comments/, ].some(pattern => pattern.test(lower)); } return false; } export const cleanupModule = { name: 'cleanup', description: 'Remove markdown fences and fluff from top/bottom of each chunk with colored logging', async run(input) { // Normalize line endings to \n to avoid issues with \r\n let content = input.content.replace(/\r\n/g, '\n'); let lines = content.split('\n'); // --- CLEAN TOP --- // Remove noise lines before the first triple tick or end while (lines.length && (lines[0].trim() === '' || isTopOrBottomNoise(lines[0]))) { if (/^```(?:\w+)?$/.test(lines[0].trim())) break; // Stop if opening fence found console.log(chalk.red(`[cleanupModule] Removing noise from top:`), chalk.yellow(`"${lines[0].trim()}"`)); lines.shift(); } // If opening fence found at top, find matching closing fence if (lines.length && /^```(?:\w+)?$/.test(lines[0].trim())) { console.log(chalk.red(`[cleanupModule] Found opening fenced block at top.`)); // Remove opening fence line lines.shift(); // Find closing fence index let closingIndex = -1; for (let i = 0; i < lines.length; i++) { if (/^```(?:\w+)?$/.test(lines[i].trim())) { closingIndex = i; break; } } if (closingIndex !== -1) { console.log(chalk.red(`[cleanupModule] Found closing fenced block at line ${closingIndex + 1}, removing fence lines.`)); // Remove closing fence line lines.splice(closingIndex, 1); } else { console.log(chalk.yellow(`[cleanupModule] No closing fenced block found, only removed opening fence.`)); } // NO removal of noise lines after fenced block here (to keep new comments intact) } // --- CLEAN BOTTOM --- // If closing fence found at bottom, remove only that triple tick line if (lines.length && /^```(?:\w+)?$/.test(lines[lines.length - 1].trim())) { console.log(chalk.red(`[cleanupModule] Removing closing fenced block line at bottom.`)); lines.pop(); } // Remove noise lines after closing fence (now bottom) while (lines.length && (lines[lines.length - 1].trim() === '' || isTopOrBottomNoise(lines[lines.length - 1]))) { console.log(chalk.red(`[cleanupModule] Removing noise from bottom after fenced block:`), chalk.yellow(`"${lines[lines.length - 1].trim()}"`)); lines.pop(); } // --- FINAL CLEANUP: REMOVE ANY LINGERING TRIPLE TICK LINES ANYWHERE --- lines = lines.filter(line => { const trimmed = line.trim(); if (/^```(?:\w+)?$/.test(trimmed)) { console.log(chalk.red(`[cleanupModule] Removing lingering triple tick line:`), chalk.yellow(`"${line}"`)); return false; } return true; }); return { content: lines.join('\n').trim() }; } };