/** * 小说内容清洗工具 * 移除原始网站信息、广告、干扰元素 */ export const contentCleaner = { // 主清洗函数 clean(content) { if (!content) return '' // 执行清洗流水线 return this.pipeline(content, [ this.removeWebsiteInfo, this.removeAds, this.removeHtmlTags, this.normalizeSpaces, this.fixParagraphs, this.removeSpecialChars ]) }, // 清洗流水线 pipeline(content, processors) { return processors.reduce((result, processor) => { return processor(result) }, content) }, // 移除网站信息 removeWebsiteInfo(content) { const patterns = [ /最新网址[::]?\s*[a-z0-9.-]+/gi, /www\.[a-z0-9]+\.[a-z]{2,}/gi, /请?收藏本站:https?:\/\/[^\s]+/gi, /请?记住本站域名:\w+\.\w+/gi, /(电脑版|手机版)?访问:m?\.\w+\.\w+/gi, /【[^】]{0,10}更新快[^】]{0,10}】/g ] return patterns.reduce((result, pattern) => { return result.replace(pattern, '') }, content) }, // 移除广告文本 removeAds(content) { const adKeywords = [ '广告', '推广', '推荐票', '月票', 'QQ群', '微信号', 'VIP章节', '正版订阅', '盗版', '笔趣阁', '天才一秒记住', '本站地址', '手机用户' ] const adPattern = new RegExp( `[【((]?(${adKeywords.join('|')})[^))】]*[))】]?`, 'gi' ) return content.replace(adPattern, '') }, // 移除HTML标签 removeHtmlTags(content) { return content .replace(//g, '\n') // 保留换行 .replace(/ | /g, ' ') // 替换空格实体 .replace(/<[^>]+>/g, '') // 移除所有HTML标签 }, // 标准化空格 normalizeSpaces(content) { return content .replace(/\s+/g, ' ') // 合并连续空格 .replace(/^\s+|\s+$/g, '') // 移除首尾空格 }, // 修复段落格式 fixParagraphs(content) { return content .split('\n') .map(line => line.trim()) .filter(line => line.length > 0) .join('\n\n') }, // 移除特殊字符 removeSpecialChars(content) { const specialChars = [ '\u200b', // 零宽空格 '\ufeff', // BOM头 '\u202a', '\u202b', '\u202c', '\u202d', '\u202e' // 方向控制符 ] const pattern = new RegExp(`[${specialChars.join('')}]`, 'g') return content.replace(pattern, '') } }