50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import fs from 'fs';
|
|
|
|
const filePath = process.argv[2];
|
|
if (!filePath) {
|
|
console.error('Usage: node clean-html.mjs <file-path>');
|
|
process.exit(1);
|
|
}
|
|
|
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
|
|
// 1. Remove <title> tags but keep content
|
|
content = content.replace(/<title>(.*?)<\/title>/gs, '# $1');
|
|
|
|
// 2. Convert <callout emoji="..."> to blockquote
|
|
content = content.replace(/<callout emoji="([^"]+)">\s*/g, '> 📌 ');
|
|
content = content.replace(/<\/callout>/g, '');
|
|
|
|
// 3. Remove complex grid/column structures with embedded images
|
|
// These are usually image galleries that we can't display properly in markdown
|
|
content = content.replace(/<grid>.*?<\/grid>/gs, (match) => {
|
|
// Try to extract any alt text or descriptions
|
|
const imgMatches = match.matchAll(/alt="([^"]+)"/g);
|
|
const descriptions = [];
|
|
for (const m of imgMatches) {
|
|
descriptions.push(m[1]);
|
|
}
|
|
|
|
if (descriptions.length > 0) {
|
|
return '\n> 📷 [图片集]\n\n';
|
|
}
|
|
return '';
|
|
});
|
|
|
|
// 4. Remove any remaining HTML tags (but preserve content where possible)
|
|
content = content.replace(/<figure[^>]*>.*?<\/figure>/gs, '');
|
|
content = content.replace(/<source[^>]*\/?>/g, '');
|
|
content = content.replace(/<column[^>]*>/g, '');
|
|
content = content.replace(/<\/column>/g, '');
|
|
|
|
// 5. Clean up excessive blank lines (more than 2 consecutive)
|
|
content = content.replace(/\n{4,}/g, '\n\n\n');
|
|
|
|
// 6. Fix any escaped underscores in URLs
|
|
content = content.replace(/\\_/g, '_');
|
|
|
|
fs.writeFileSync(filePath, content, 'utf-8');
|
|
console.log(`✅ Cleaned HTML tags from: ${filePath}`);
|