You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mail-yubao/scripts/patch-preserve-body-text.ts

76 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* Stop pipeline from overwriting bodyText with HTML-normalized / OCR-merged text.
* Parse still uses a local normalized body; OCR stays in ocrText only.
*/
import fs from "fs";
const path = "src/services/parse/pipeline.ts";
let src = fs.readFileSync(path, "utf8");
const oldNorm = ` const subject = mail.subject?.trim() ? mail.subject : "(无主<E697A0>?";
// 历史库可能存了原<E4BA86>?HTML;解析前先归一成纯文本
let body = plainTextFromMaybeHtml(mail.bodyText || "");
if (body && body !== (mail.bodyText || "").trim()) {
await prisma.mailMessage.update({
where: { id: mailId },
data: { bodyText: body },
});
}`;
const neuNorm = ` const subject = mail.subject?.trim() ? mail.subject : "(无主<E697A0>?";
// 解析用归一化正文;绝不回写 bodyText(排查用原文一字不落)
const bodyStored = mail.bodyText || "";
let body = plainTextFromMaybeHtml(bodyStored) || bodyStored;`;
if (!src.includes(oldNorm)) {
const oldCrlf = oldNorm.replace(/\n/g, "\r\n");
if (!src.includes(oldCrlf)) {
console.error("norm block missing");
process.exit(1);
}
src = src.replace(oldCrlf, neuNorm.replace(/\n/g, "\r\n"));
} else {
src = src.replace(oldNorm, neuNorm);
}
const oldOcr = ` body = body.trim()
? \`\${body}\\n\\n\${ocr.text}\`
: ocr.text;
await prisma.mailMessage.update({
where: { id: mailId },
data: { ocrText: ocr.text, bodyText: body },
});`;
const neuOcr = ` // OCR 只写<E58FAA>?ocrText,不污染 bodyText
if (!body.trim()) body = ocr.text;
else body = \`\${body}\\n\\n\${ocr.text}\`;
await prisma.mailMessage.update({
where: { id: mailId },
data: { ocrText: ocr.text },
});`;
if (!src.includes("ocrText: ocr.text, bodyText: body")) {
// try already patched
if (!src.includes("OCR 只写<E58FAA>?ocrText")) {
console.error("ocr block missing", src.includes("bodyText: body"));
process.exit(1);
}
} else {
src = src.replace(
/body = body\.trim\(\)\s*\?\s*`\$\{body\}\\n\\n\$\{ocr\.text\}`\s*:\s*ocr\.text;\s*await prisma\.mailMessage\.update\(\{\s*where: \{ id: mailId \},\s*data: \{ ocrText: ocr\.text, bodyText: body \},\s*\}\);/s,
`// OCR only in ocrText <20>?never overwrite bodyText
if (!body.trim()) body = ocr.text;
else body = \`\${body}\\n\\n\${ocr.text}\`;
await prisma.mailMessage.update({
where: { id: mailId },
data: { ocrText: ocr.text },
});`,
);
}
fs.writeFileSync(path, src);
console.log("pipeline body preserve ok", {
hasStored: src.includes("bodyStored"),
ocrOnly: src.includes("ocrText: ocr.text }") || src.includes("data: { ocrText: ocr.text }"),
});