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-detail-full-body-ui.ts

158 lines
5.0 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.

/**
* Detail page: show full body_text + separate OCR; no silent truncation.
*/
import fs from "fs";
const path = "src/app/(ops)/mails/[id]/page.tsx";
let src = fs.readFileSync(path, "utf8");
const old = ` <Collapse
items={[
{
key: "body",
label: "原始正文(排查用<E69FA5>?,
children: (
<pre
className="mono"
style={{
maxHeight: 280,
overflow: "auto",
margin: 0,
whiteSpace: "pre-wrap",
lineHeight: 1.5,
fontSize: 12,
}}
>
{mail.body_text ||
mail.ocr_text ||
"(无正<E697A0>?"}
</pre>
),
},
]}
/>`;
const neu = ` <Collapse
defaultActiveKey={
!(mail.body_text || "").trim() && (mail.ocr_text || "").trim()
? ["body"]
: undefined
}
items={[
{
key: "body",
label: \`原始正文(排查用)<EFBFBD>?\${(mail.body_text || "").length} 字\${
(mail.ocr_text || "").trim()
? \` · OCR \${(mail.ocr_text || "").length} 字\`
: ""
}\`,
children: (
<div>
<Typography.Paragraph
type="secondary"
style={{ marginBottom: 8, fontSize: 12 }}
>
入库原文完整保留,解<EFBC8C>?OCR 不会覆盖此字段。可滚动查看全部内容<E58685>?
</Typography.Paragraph>
<pre
className="mono"
style={{
maxHeight: 480,
overflow: "auto",
margin: 0,
whiteSpace: "pre-wrap",
wordBreak: "break-word",
lineHeight: 1.5,
fontSize: 12,
padding: 12,
background: "rgba(0,0,0,0.02)",
borderRadius: 6,
}}
>
{(mail.body_text || "").trim()
? mail.body_text
: "(无正<E697A0>?"}
</pre>
{(mail.ocr_text || "").trim() ? (
<>
<Typography.Text
strong
style={{ display: "block", marginTop: 16, marginBottom: 8 }}
>
OCR 文本(附件识别,与原文分开保存)·{" "}
{(mail.ocr_text || "").length} <20>?
</Typography.Text>
<pre
className="mono"
style={{
maxHeight: 320,
overflow: "auto",
margin: 0,
whiteSpace: "pre-wrap",
wordBreak: "break-word",
lineHeight: 1.5,
fontSize: 12,
padding: 12,
background: "rgba(0,0,0,0.02)",
borderRadius: 6,
}}
>
{mail.ocr_text}
</pre>
</>
) : null}
</div>
),
},
]}
/>`;
if (!src.includes('label: "原始正文(排查用<E69FA5>?')) {
console.error("collapse block missing");
process.exit(1);
}
// Replace by finding key markers
const start = src.indexOf('key: "body"');
const labelIdx = src.lastIndexOf("Collapse", start);
const collapseStart = src.lastIndexOf("<Collapse", start);
const endMarker = "TypeOverrideModal";
const end = src.indexOf(endMarker, start);
if (collapseStart < 0 || end < 0) {
console.error("bounds", collapseStart, end);
process.exit(1);
}
// find closing of Collapse before TypeOverrideModal
const collapseEnd = src.lastIndexOf("/>", end);
// better: match from <Collapse to /> that closes it
const slice = src.slice(collapseStart, end);
const closeRel = slice.indexOf("/>");
if (closeRel < 0) {
console.error("no close");
process.exit(1);
}
// Might be wrong close - find ` />\n\n <TypeOverride`
const m = src.slice(collapseStart).match(/^[\s\S]*?\n \/>\n\n <TypeOverrideModal/);
if (!m) {
// try without blank
const m2 = src.slice(collapseStart).match(/^[\s\S]*?\n \/>\r?\n\r?\n?\s*<TypeOverrideModal/);
if (!m2) {
console.error("pattern fail", JSON.stringify(src.slice(collapseStart, collapseStart + 200)));
process.exit(1);
}
src =
src.slice(0, collapseStart) +
neu +
"\n\n <TypeOverrideModal" +
src.slice(collapseStart + m2[0].length - "<TypeOverrideModal".length);
} else {
src =
src.slice(0, collapseStart) +
neu +
"\n\n <TypeOverrideModal" +
src.slice(collapseStart + m[0].length - "<TypeOverrideModal".length);
}
fs.writeFileSync(path, src);
console.log("detail body UI updated");