diff --git a/plugins/vietnamese/SangTacViet/content-normalization.ts b/plugins/vietnamese/SangTacViet/content-normalization.ts new file mode 100644 index 00000000..83b12377 --- /dev/null +++ b/plugins/vietnamese/SangTacViet/content-normalization.ts @@ -0,0 +1,99 @@ +/* eslint-disable no-useless-escape */ + +import { decodeHtmlEntities, encodeHtmlEntities } from '@libs/utils'; +import { decodeGlyphs } from './glyph-decode'; + +// ── Content normalization ───────────────── +function looksLikeInterlinear(text: string): boolean { + return /]*\b(?:t|v|p)\s*=\s*['"][^'"]*['"][^>]*>/i.test(text); +} + +function normalizeInterlinear(raw: string): string { + let t = raw.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); + t = t.replace(/\s*/gi, '\n').replace(/<\/br\s*>/gi, '\n'); + t = t.replace( + /<\/i>\s*\n+\s*(?=[,.;:!?%\)\]\}\u3002\uff0c\u3001\uff01\uff1f\uff1b\uff1a\u201d\u2019\u300d\u300f\u3011\u300b])/gi, + '', + ); + const marker = '__STV_GAP__'; + t = t.replace(/<\/i>\s*' + marker + ']*>([\s\S]*?)<\/i>/gi, '$1'); + t = t.replace( + /<\/?(p|div|article|section|li|tr|h[1-6]|blockquote|ul|ol)[^>]*>/gi, + '\n', + ); + t = t.replace(/<[^>]+>/g, '').replace(/[<>]/g, ''); + t = decodeHtmlEntities(t); + t = t.replace(new RegExp(marker, 'g'), ' '); + t = t.replace(/[\t\f\v ]+/g, ' '); + t = t.replace( + / +([,.;:!?%\)\]\}\u3002\uff0c\u3001\uff01\uff1f\uff1b\uff1a\u201d\u2019\u300d\u300f\u3011\u300b])/g, + '$1', + ); + t = t.replace( + /\n+([,.;:!?%\)\]\}\u3002\uff0c\u3001\uff01\uff1f\uff1b\uff1a\u201d\u2019\u300d\u300f\u3011\u300b])/g, + '$1', + ); + t = t + .replace(/[ ]*\n[ ]*/g, '\n') + .replace(/\n{3,}/g, '\n\n') + .trim(); + return t ? t.replace(/\n/g, '') : ''; +} + +export function normalizeChapterHtml(host: string, raw: string): string { + let text = raw || ''; + if (!text) return ''; + const h = host.toLowerCase(); + + if (h === 'fanqie') { + text = text.replace(/]*>[\s\S]*?<\/header>/gi, ''); + text = text.replace(/