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.
52 lines
1.6 KiB
52 lines
1.6 KiB
import fs from "fs";
|
|
|
|
const path = "src/utils/mail-body-text.ts";
|
|
let src = fs.readFileSync(path, "utf8");
|
|
const start = src.indexOf("export function resolveParsedMailBody");
|
|
if (start < 0) throw new Error("missing");
|
|
|
|
const neu = `export function resolveParsedMailBody(parsed: {
|
|
text?: string | false | null;
|
|
html?: string | false | null;
|
|
}): string {
|
|
const text =
|
|
typeof parsed.text === "string" ? parsed.text.trim() : "";
|
|
const htmlRaw =
|
|
typeof parsed.html === "string" ? parsed.html : "";
|
|
const fromHtml = htmlRaw ? htmlToPlainText(htmlRaw) : "";
|
|
|
|
if (!text && !fromHtml) return "";
|
|
if (!text) return fromHtml;
|
|
if (!fromHtml) return text;
|
|
|
|
// Prefer longer side so troubleshooting body is not truncated
|
|
if (fromHtml.length > text.length + 40) return fromHtml;
|
|
if (text.length > fromHtml.length + 40) return text;
|
|
|
|
const textHead = text.slice(0, Math.min(48, text.length));
|
|
if (textHead && fromHtml.includes(textHead) && fromHtml.length >= text.length) {
|
|
return fromHtml;
|
|
}
|
|
const htmlHead = fromHtml.slice(0, Math.min(48, fromHtml.length));
|
|
if (htmlHead && text.includes(htmlHead) && text.length >= fromHtml.length) {
|
|
return text;
|
|
}
|
|
|
|
// Both long and not nested: keep both parts
|
|
if (
|
|
text.length > 80 &&
|
|
fromHtml.length > 80 &&
|
|
!fromHtml.includes(textHead) &&
|
|
!text.includes(htmlHead)
|
|
) {
|
|
return \`\${text}\\n\\n---\\n\\n\${fromHtml}\`;
|
|
}
|
|
|
|
return fromHtml.length >= text.length ? fromHtml : text;
|
|
}
|
|
`;
|
|
|
|
src = src.slice(0, start) + neu;
|
|
fs.writeFileSync(path, src);
|
|
console.log("resolve ok, len", src.length);
|