35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const filePath = path.join(__dirname, 'bài viết', 'Trang chủ - Bệnh Viện Đa Khoa Quốc Tế S.I.S Cần Thơ.html');
|
|
const content = fs.readFileSync(filePath, 'utf8');
|
|
|
|
// Extract images
|
|
const imgRegex = /<img[^>]+src="([^">]+)"/g;
|
|
let match;
|
|
const images = new Set();
|
|
while ((match = imgRegex.exec(content)) !== null) {
|
|
images.add(match[1]);
|
|
}
|
|
|
|
// Extract headings and paragraphs
|
|
const textRegex = /<(h1|h2|h3|p)[^>]*>(.*?)<\/\1>/gs;
|
|
const texts = [];
|
|
while ((match = textRegex.exec(content)) !== null) {
|
|
const tag = match[1];
|
|
const innerHtml = match[2].trim();
|
|
// Strip inner tags
|
|
const plainText = innerHtml.replace(/<[^>]+>/g, '').trim();
|
|
if (plainText.length > 10) {
|
|
texts.push(`[${tag.toUpperCase()}] ${plainText}`);
|
|
}
|
|
}
|
|
|
|
const result = {
|
|
images: Array.from(images).filter(src => src.indexOf('data:image') === -1 && src.indexOf('.svg') === -1),
|
|
texts: texts
|
|
};
|
|
|
|
fs.writeFileSync('extracted_content.json', JSON.stringify(result, null, 2));
|
|
console.log('Done extraction. Found', result.images.length, 'images and', result.texts.length, 'text blocks.');
|